#/usr/bin/perl


#-----------------------------------------------------------
my $rep="$ARGV[0]";
my $rubrique = "$ARGV[1]";
my %dico;
$rep=~ s/[\/]$//;

my $output1="./profondeur/$rubrique.xml";
if (!open (FILEOUT,">$output1")) { die "Impossible d'ouvrir $output1"};
print FILEOUT "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n";
print FILEOUT "<PARCOURS>\n";
print FILEOUT "<NOM>CYNDEL ET MANON</NOM>\n";
close (FILEOUT);

#----------------------------------------
&parcoursarborescencefichiers($rep);	
#----------------------------------------

open (FILEOUT,">>:encoding(utf-8)", $output1);
print FILEOUT "</PARCOURS>\n";
close(FILEOUT);
exit;


#----------------------------------------------
sub parcoursarborescencefichiers {
    my $path = shift(@_);
    opendir(DIR, $path) or die "can't open $path: $!\n";
    my @files = readdir(DIR);
    closedir(DIR);
    foreach my $file (@files) {
		next if $file =~ /^\.\.?$/;
		$file = $path."/".$file;   
		if (-d $file) {
			print "<NOUVEAU REPERTOIRE> ==> ",$file,"\n";
			&parcoursarborescencefichiers($file);	
			print "<FIN REPERTOIRE> ==> ",$file,"\n";
		}
		if (-f $file) {
			
            if ($file =~ /$rubrique.+\.txt$/) {
                
                print "<",$i++,"> ==> ",$file,"\n";

		print "<",$i++,"> ==> ",$file,"\n";
				open(FILE, "<:encoding(utf-8)", "$file");
		
				my$texte = "";
				my%dico;
                open (OUT, ">>:encoding(utf-8)", "./profondeur/$rubrique.txt");
                open (OUT2, ">>:encoding(utf-8)", "./profondeur/$rubrique.xml");
               
                my $texte="";
                while (my$ligne = <FILE>){
				chomp $ligne;
				$ligne =~ s/\r//g;
				$texte = $texte . $ligne;
				}
                close FILE;

                $texte =~ s/>\s+</></g;
                while ($texte =~m/Le Monde[^\|]+\|[^\|]+\|([^<]+)<article/g) {
                	my $article= $1;
			$article=~s/&lt;.+?&gt;//g;
			$article=~ s/^.+?(\s){21}//g;


                        if (!(exists $dico{$article})) {
                            $dico{$article} = 1; 

                            print OUT "$article\n";

                            #============================================================================================#

                           my $articletag = &etiquetage($article);

                            #============================================================================================#


                            print OUT2 "<item><article>$articletag</article></item>\n";
                                   
                        }
                }
                close OUT;
                close OUT2;
                
            }
        }
    }
}
#----------------------------------------------


sub etiquetage {

    my($a) =@_;


    #articles
    open(TMP,">:encoding(utf-8)","article.txt");
    print TMP $a;
    close (TMP);


    system("perl tokenise-utf8.pl article.txt|tree-tagger -token -lemma -no-unknown ~/sources/treetagger/lib/french-utf8.par > article_tag"); 
    system("perl treetagger2xml-utf8.pl article_tag utf-8");

    open(TMP2, "<:encoding(utf-8)","article_tag.xml");

    my $a_tag ="";  


    my $ligne = <TMP2>; 

    while (my $ligne = <TMP2>) {        
        chomp $ligne;
        $a_tag = $a_tag . $ligne;
    }

    close (TMP2);


    return($a_tag);
}