﻿#!/usr/bin/perl

use utf8;
binmode STDOUT,":utf8";

# Programme d'extraction de patron sur sortie Cordial
# Usage : perl pg.pl cordialFile patrons

#-----------------------------------------------------------#
#------------------0. étapes préliminaires------------------#
#-----------------------------------------------------------#

open(FIC,"<:encoding(utf8)",$ARGV[0]);
my $chaine="";
while (my $ligne=<FIC>) {

	#--------------------------------------------------------------#
	#------------------1. récupération des termes------------------#
	#--------------------------------------------------------------#
 
    chomp $ligne;
    if (($ligne=~/^([^\t]+)\t[^\t]+\t([^\t]+)$/) and ($ligne!~/PCTFORTE/)) {   
        my $f=$1;
        my $c=$2;
        $f=~s/ /#/g;
        $chaine = $chaine . $c."_".$f." ";
    }
    else {
	
		#---------------------------------------------------------------#
		#------------------2. récupération sur patrons------------------#
		#---------------------------------------------------------------#
        
        open(TERM,"<:encoding(utf8)",$ARGV[1]);
        while (my $terme=<TERM>) {
            chomp($terme);
            
            $terme=~s/([^ ]+)/$1_\[\^ \]+/g;
            
            while($chaine=~/($terme)/g) {
                
                my $correspondance=$&;
                $correspondance=~s/[A-Z]+_//g;
				$correspondance=~s/#/ /g;
                print $correspondance,"\n";
            }

            
        }
        close(TERM);
        $chaine="";
    }
}
print $chaine;
close(FIC);