#/usr/bin/perl use XML::RSS; my $rss=new XML::RSS; my $rep="$ARGV[0]"; # on s'assure que le nom du répertoire ne se termine pas par un "/" $rep=~ s/[\/]$//; my $rubrique = "$ARGV[1]"; my %redontant; open(FILEOUT1, ">:encoding(utf8)", "sortie_bao2_xmlrss_$rubrique.txt"); close FILEOUT1; open(FILEOUT2, ">:encoding(utf8)", "sortie_bao2_xmlrss_$rubrique.xml"); print FILEOUT2 "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"; print FILEOUT2 "<PARCOURS>\n"; print FILEOUT2 "<FILTRAGE>\n"; close FILEOUT2; #---------------------------------------- &parcoursarborescencefichiers($rep); # on lance la récursion.... et elle se terminera après examen de toute l'arborescence #---------------------------------------- open(FILEOUT2, ">>:encoding(utf8)", "sortie_bao2_xmlrss_$rubrique.xml"); print FILEOUT2 "</FILTRAGE>\n"; print FILEOUT2 "</PARCOURS>\n"; close FILEOUT2; 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 =~ /^\.\.?$/; #passer au prochain si $file = $path."/".$file; if (-d $file) { #si c'est un repertoire print "<NOUVEAU REPERTOIRE> ==> ",$file,"\n"; &parcoursarborescencefichiers($file); #recurse! print "<FIN REPERTOIRE> ==> ",$file,"\n"; } if (-f $file) { #si c'est un fichier # TRAITEMENT à réaliser sur chaque fichier # Insérer ici votre code (le filtreur) if ($file =~ m/$rubrique.+\.xml$/) { eval {$rss->parsefile($file); }; if( $@ ) { $@ =~ s/at \/.*?$//s; # remove module line number print STDERR "\nERROR in '$file':\n$@\n"; } else { open(FILEOUT1, ">>:encoding(utf8)", "sortie_bao2_xmlrss_$rubrique.txt"); open(FILEOUT2, ">>:encoding(utf8)", "sortie_bao2_xmlrss_$rubrique.xml"); foreach my $item (@{$rss->{'items'}}) { my $description=$item->{'description'}; my $titre=$item->{'title'}; $titre=~s/<[^>]+>//g; $description=~s/<[^>]+>//g; if ( !exists $redontant{$titre} ) { $redontant{$titre} = 1; my ($titre_propre, $description_propre) = &nettoyage($titre, $description); my ($titre_etiqutte, $description_etiquette) = &etiquettage($titre_propre, $description_propre); print FILEOUT1 "$titre_propre",".","\n"; print FILEOUT1 "$description_propre\n"; print FILEOUT2 "<item><titre>$titre_etiqutte</titre><description>$description_etiquette</description></item>\n"; } } close FILEOUT1; close FILEOUT2; } print "<",$i++,"> ==> ",$file,"\n"; } } } } sub nettoyage { # my $var1 = $_[0]; #my $var1 = shift(@_); #my $var2 = shift(@_); my ($a, $b) = @_; $a =~ s/&lt;.+?&gt;//g; $b =~ s/&lt;.+?&gt;//g; $a =~ s/&amp;/&/g; $b =~ s/&amp;/&/g; return $a, $b; } #---------------------------------------------- sub etiquettage { my ($x, $y) = @_; open (OUT1, ">:encoding(utf-8)", "titre_tmp_xmlrss.txt"); print OUT1 $x; close OUT1; system ("tokenise-utf8.pl titre_tmp_xmlrss.txt | tree-tagger.exe -lemma -token -no-unknown french-utf8.par > titre_tmp_xmlrss_etiq.txt"); system ("perl treetagger2xml-utf8.pl titre_tmp_xmlrss_etiq.txt utf8"); $/=undef; open (FIC1, "<:encoding(utf8)", "titre_tmp_xmlrss_etiq.txt.xml"); my $titre_retour = <FIC1>; $titre_retour =~ s/<\?xml version="1.0" encoding="utf-8" standalone="no"\?>//g; print $titre_retour; close FIC1; open (OUT2, ">:encoding(utf-8)", "description_tmp_xmlrss.txt"); print OUT2 $y; close OUT2; system ("tokenise-utf8.pl description_tmp_xmlrss.txt | tree-tagger.exe -lemma -token -no-unknown french-utf8.par > description_tmp_xmlrss_etiq.txt"); system ("perl treetagger2xml-utf8.pl description_tmp_xmlrss_etiq.txt utf8"); open (FIC2, "<:encoding(utf8)", "description_tmp_xmlrss_etiq.txt.xml"); my $description_retour = <FIC2>; $description_retour =~ s/<\?xml version="1.0" encoding="utf-8" standalone="no"\?>//g; print $description_retour; close FIC2; $/ = '\n'; #Puisqu'on a changé le comportement de $, on le rétablit. return $titre_retour, $description_retour; }