#!/usr/bin/perl
use utf8;
binmode STDOUT,":utf8";
# Programme d'extraction de patron sur sortie Cordial
# Usage : perl extraction_patron.pl sortie_cordial patrons
#----------------------------------
open(FIC,"<:encoding(utf8)",$ARGV[0]);
my $chaine="";

while (my $ligne=<FIC>) {
	# Lecture des lignes comportant Forme, Lemme, POS (on exclut la ponctuation)
    if (($ligne=~/^([^\t]+)\t[^\t]+\t([^\t]+)$/) and ($ligne!~/PCTFORTE/)) {   
        my $f=$1;	# Forme
        my $c=$2;	# POS
        $f=~s/ /#/g;
        $chaine = $chaine . $c."_".$f." ";
    }
    else {
        open(TERM,"<:encoding(utf8)",$ARGV[1]);
        while (my $terme=<TERM>) {
            chomp($terme);

            # transformer NCMP ADJMP en  NOM_([^ ]+) ADJ_([^ ]+)
            # pour ensuite cherche $terme dans $chaine
            $terme=~s/([^ ]+)/$1_\[\^ \]+/g;

            while($chaine=~/($terme)/g) {
                my $correspondance=$&; # $& = the string matched by the last successful pattern match
                $correspondance=~s/[A-Z]+_//g;
				$correspondance=~s/#/ /g;
                print $correspondance,"\n\n";
            }

            
        }
        close(TERM);
        #--------------------------------
        # a la fin
        $chaine="";
    }
}
print $chaine;
close(FIC);
