#!/usr/bin/perl
use utf8;
use Time::HiRes qw(gettimeofday tv_interval);
my $timepg = [gettimeofday];

my $filetalismane=$ARGV[0];
my $filetermino=$ARGV[1];

open my $fileT,"<:encoding(UTF-8)",$filetalismane;
open my $fileTer,"<:encoding(UTF-8)",$filetermino;
my @TERMINO=<$fileTer>;
close $fileTer;
my %dicotmp=(); #sert à accumuler les différents champs dans la phrase, on ne s'occupera après que des champs 0 et 2.
my %dicoPatron=();
my $nbTerme=0;
my $nbMotInPhrase=0;

open my $fileResuBrut, ">:encoding(UTF-8)","ResuBrut.txt";

while (my $ligne=<$fileT>) {
	
	if ($ligne!~/^\n/) {
		$ligne=~/(^\d+)\t(.+)$/;
		my $cle=$1;
		my $reste=$2;
		$nbMotInPhrase++;
		my @listereste=split(/\t/,$reste);
		$dicotmp{$cle}=\@listereste;
	}
	else { #dès qu'on rencontre une ligne vide: on extrait la termino.
		#------------------------
		# extraction termino....
		my $phrase=" ";
		my $longueur = scalar (keys %dicotmp);
		for (my $i=1;$i<=$longueur;$i++) {
			my $LISTE=$dicotmp{$i};
			my @listedeferencee = @$LISTE;
			my $mot=$listedeferencee[0]; #initialisation de la liste de N° de token 
			my $pos=$listedeferencee[2]; #initialisation de la liste de pos
			$phrase=$phrase.$mot."/".$pos." ";
		}
		foreach my $patron (@TERMINO) {
			my $patron2=$patron;
			chomp($patron2);
			$patron2=~s/([^ ]+)/\[\^ \]\+\/\\b$1/g;
			while ($phrase=~/(?=\s($patron2))/g) {
				my $terme=$1;
				$terme=~s/\/[^ ]+//g;
				print $fileResuBrut "$terme\n";
				$dicoPatron{$patron}->{$terme}++;
				$nbTerme++;
			}
		}
		%dicotmp=();
		$nbMotInPhrase=0;
	}
}
close($fileT);
close($fileResuBrut);

open my $fileResu,">:encoding(UTF-8)","PatronsExtracted.txt";
print $fileResu "$nbTerme éléments trouvés\n";
foreach my $patron (keys %dicoPatron) {
	print $fileResu "\nType de pattern: ".$patron." \n\n";
	print "\nType de pattern: ".$patron." \n\n";
	foreach my $terme (sort {$dicoPatron{$patron}->{$b} <=> $dicoPatron{$patron}->{$a} } keys %{$dicoPatron{$patron}}) {
		print $fileResu $dicoPatron{$patron}->{$terme}."\t".$terme."\n";
	}
}
print $fileResu "\nScript execution time: " . tv_interval($timepg) . " seconds.";
close($fileResu);