#!/usr/bin/perl

<<DOC;
prend en entrée un fichier issu de treetagger
et un fichier de patrons morphosyntaxiques et
extrait suites de tokens correspondant aux patrons morpho-syntaxiques
DOC

print "bonjour\n";
open(FIC, $ARGV[0]) or die "impossible ouvrir $ARGV[0] : $!";
print "choisis nom de fichier pour contenir termes extraits\n";
my $fic=<STDIN>;
open(FIC1, ">$fic");
my $i=0;
my $j=0;
my $k=0;
my @token=();
while (<FIC>)
{
my $ligne=$_;
chomp $ligne;
my @liste=split(/\t/,$ligne);
push(@token, $liste[$i++]." ");
push(@lemme, "$liste[$i++]" ." ");
push(@patron, "$liste[$i++]" ." ");
$i=0;
}
#on a 3 listes @token, @lemme, @patron
@sous_patron=();
while (defined($element_patron=shift(@patron)))
{
$element_patron=~s/\n//;
if ($element_patron !~ "PCTFORTE")
{
push(@sous_patron, $element_patron);
$j++;
next;
}

my @sous_token=@token[$k..$j-1];
&cherche_patron(@sous_patron);
@sous_patron=();
$k=$j+1;
#print "tape return pour continuer";
#my $reponse=<STDIN>;

$j++;
}
#=============================================
sub cherche_patron
{
my @liste=@_;
my $suite_de_patrons=join("",@liste);
$z=0;
$nb=0;
open(FIC, "$ARGV[1]");
while(<FIC>)
{
$nb=0;
$ligne=$_;
chomp $ligne;
#print "voici le patron traité : $ligne\n";
while ($ligne=~m/ /g) {$nb++};
$nb++;
while ($suite_de_patrons=~m/$ligne/g)
{
my $avant=substr ($suite_de_patrons, 0, pos($suite_de_patrons)-length($&));
while ($avant=~m/ /g) {$z++};
print FIC1 "@token[$k+$z..$k+$z+$nb-1]\n";
#print "tape sur return pour continuer\n";
#my $reponse=<STDIN>;
$z=0;
}
}
}