#MODE D'EMPLOI : perl BAO3_extraction_patrosn.pl fichier_treetagger.xml PATRON

#--------------------------------------------------------------

#!/usr/bin/perl

#--------------------------------------------------------------

use utf8;
binmode STDOUT,":utf8";
my $corpus=shift @ARGV;
my @PATRON=@ARGV;
$patron_nom="";
for $cat (@PATRON) {
	$patron_nom.="$cat-"
}
open my $input, "<:encoding(utf-8)",$corpus;
my @LISTE=<$input>;
close($input);

while (my $ligne=shift @LISTE) {
	my $terme="";
	if ($ligne=~/<element><data type="type">$PATRON[0]<\/data><data type="lemma">[^<]+?<\/data><data type="string">([^<]+?)<\/data><\/element>/) {
		$terme=$terme.$1;
		my $longueur=1;
		my $indice=1;
		while (($LISTE[$indice-1]=~/<element><data type="type">($PATRON[$indice])<\/data><data type="lemma">[^<]+?<\/data><data type="string">([^<]+?)<\/data><\/element>/) and ($indice <= $#PATRON)) {
			$indice++;
			$terme.=" ".$2;
			$longueur++;
		}
		if ($longueur == $#PATRON + 1) { 
			$dicoPatron{$terme}++;
			$nbTerme++;
		}
	}	
}

open my $resultat,">:encoding(UTF-8)","extract_patron_$patron_nom.txt";
print $resultat "$nbTerme éléments trouvés\n";
foreach my $patron (sort {$dicoPatron{$b} <=> $dicoPatron{$a} } keys %dicoPatron) {
	print $resultat "$dicoPatron{$patron}\t$patron\n";
}

close($resultat);