# usr/bin/perl

use strict;
use warnings;
use feature 'say';
use Cwd;

my @files; # container of all the target files

sub fileFinder{ # iter all the files under a fileholder with given filter
	opendir(DIR,$_[0]) or die "The Path is Not Accessible!\n";
	my @items=readdir(DIR);

	for my $item(@items){
		next if($item=~/^\./);
		$item=$_[0].'/'.$item;
		fileFinder($item) if(-d $item);
		push(@files,$item) if((-f $item) and ($item=~/3208.*xml/));
	}
}

sub dataExtractor{ # extract the infos avec given filter
	local undef $/;
	open(OUT,">>:encoding(utf8)",$_[0]) or die "File Open Failure!\n";

	for my $file(@files){
		open(FILE,"<:encoding(utf8)",$file) or die "File Open Failure!\n";
		my $text=<FILE>;

		while($text=~/<item>.*?<title>([^<]*)<\/title>.*?<description>([^<]*)<\/description>.*?<\/item>/gs){
			my $title=$1; my $description=$2;
			say OUT '& '.dataCleaner($title)."."; say OUT dataCleaner($description)."\n";
		}
	}
}

sub UDTagger{ # tag the extracted info with talismane
	my $dir=getcwd;
	my $talismane_dir='D:/Informatique/talismane-distribution-5.0.4-bin/';
	chdir($talismane_dir) or die "The directory is not accessible";
	my $command="java -Xmx1G -Dconfig.file=talismane-fr-5.0.4.conf -jar talismane-core-5.0.4.jar --analyse --sessionId=fr --encoding=UTF-8 --inFile=$_[0] --outFile=$_[1]";
	print(`$command`);
	chdir($dir) or die "The directory is not accessible";
}

sub govExtractor{ # extract the four pattern of govns
	doubleExtractor("../outPerl/newTagged.txt","\tNC\t","\tADJ\t","../outPerl/NounAdj.txt");
	doubleExtractor("../outPerl/newTagged.txt","\tADJ\t","\tNC\t","../outPerl/AdjNoun.txt");
	tripleExtractor("../outPerl/newTagged.txt","\tV","\tDET\t","\tNC\t","../outPerl/VerDetNoun.txt");
	quadraExtractor("../outPerl/newTagged.txt","\tNC\t","\tP|P+D\t","\tNC\t","\tP|P+D\t","../outPerl/NounPreNounPre.txt");
}

sub doubleExtractor{ # extract two govns
	open(FILE,"<:encoding(utf8)",$_[0]) or die "File Open Failure!\n";
	open(my $OUT,">>:encoding(utf8)",$_[3]) or die "File Open Failure!\n";
	my @data=<FILE>;
	for my $i (0..scalar(@data)-1){
		if($data[$i]=~/$_[1]/ and $data[$i+1]=~/$_[2]/){
			my @pat1=split(/\t/,$data[$i]);
			my @pat2=split(/\t/,$data[$i+1]);
			print $OUT ($pat1[1]." ".$pat2[1]."\n");
		}
	}
}

sub tripleExtractor{ # extract two govns
	open(FILE,"<:encoding(utf8)",$_[0]) or die "File Open Failure!\n";
	open(my $OUT,">>:encoding(utf8)",$_[4]) or die "File Open Failure!\n";
	my @data=<FILE>;
	for my $i (0..scalar(@data)-2){
		if($data[$i]=~/$_[1]/ and $data[$i+1]=~/$_[2]/ and $data[$i+2]=~/$_[3]/){
			my @pat1=split(/\t/,$data[$i]);
			my @pat2=split(/\t/,$data[$i+1]);
			my @pat3=split(/\t/,$data[$i+2]);
			print $OUT ($pat1[1]." ".$pat2[1]." ".$pat3[1]."\n");
		}
	}
}

sub quadraExtractor{ # extract two govns
	open(FILE,"<:encoding(utf8)",$_[0]) or die "File Open Failure!\n";
	open(my $OUT,">>:encoding(utf8)",$_[5]) or die "File Open Failure!\n";
	my @data=<FILE>;
	for my $i (0..scalar(@data)-2){
		if($data[$i]=~/$_[1]/ and $data[$i+1]=~/$_[2]/ and $data[$i+2]=~/$_[3]/ and $data[$i+3]=~/$_[4]/){
			my @pat1=split(/\t/,$data[$i]);
			my @pat2=split(/\t/,$data[$i+1]);
			my @pat3=split(/\t/,$data[$i+2]);
			my @pat4=split(/\t/,$data[$i+3]);
			print $OUT ($pat1[1]." ".$pat2[1]." ".$pat3[1]." ".$pat4[1]."\n");
		}
	}
}


sub dataCleaner{ # clean the extracted data
	my $res=$_[0];
	$res=~s/&#38;#39;/'/g; $res=~s/&#38;#34;/"/g;
	$res=~s/&#39;/'/g; $res=~s/&#34;/"/g;
	$res=~s/&#233;/é/g; $res=~s/&#234;/è/g;
	$res=~s/\.\s/\.\n/g;
	return $res;
}

sub main{ # to control the sequential execution
	fileFinder('D:\Informatique\Cours_TAL\M1S2\projet_encadre\projetFinal\2018');
	dataExtractor('../outPerl/newInfos.txt');
	UDTagger('D:\Informatique\Cours_TAL\M1S2\projet_encadre\projetFinal\outPerl\newInfos.txt','D:\Informatique\Cours_TAL\M1S2\projet_encadre\projetFinal\outPerl\newTagged.txt');
	govExtractor;
}
#main;