Boîte à outils série 2 - L'étiquetage morpho-syntaxique
Nous effectuons plusieurs traitements sur les fichiers txt dans le
dossier RESULTS_2012, l'un avec un script perl qui utilise l'outil
d'étiquetage morpho-syntaxique TreeTagger et l'autre avec l'outil d'étiquetage morpho-syntaxique au sein du logiciel de correction orthographique et grammaticale Cordial, que voici résumés sous forme d'organigramme. Dans un deuxième temps les fichiers de sortie subissent une
transformation supplémentaire avec un script perl qui utilise TreeTagger pour nous fournir des fichiers au format
xml (voir le deuxième organigramme plus loin). Nous obtenons donc trois
types de sortie à la fin de cette série de traitements.
Comme nous l'avons évoqué à la fin de la boîte à outils 1, il a fallu
deux versions de fichiers d'entrée, une qui reste en utf_8 et une
autre avec des fichiers convertis en ISO-8859-1.
Voici le code du script perl script-tagging-with-treetagger-txt.pl.
Ceci est un petit script qui permet
de mettre en argument à Treetagger, non pas un fichier mais le
répertoire des résultats contenant tous les fichiers texte. Les résultats de ce traitement sont des fichiers au format SORTIE-rubrique_t.txt. Nous
étions très frustrées par le fait que l'installation de la version
Windows de Treetagger s'est avérée impossible sur une machine de 64 bits ! Notez qu'il faut mettre le chemin de Treetagger qui reflète son répertoire d'installation. Ce script utilise des commandes bash. Les fichiers d'entrée doivent obligatoirement être encodés en utf8.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | #!/usr/bin/perl
use warnings; use Time::Stopwatch;
################################################################
# MUST HAVE TREETAGGER INSTALLED !!
# WORKS ONLY IN BASH TERMINAL
# ADD YOUR $ADDRESS TO TREETAGGER AND KEEP COMMENTED OTHERS
################################################################
tie my $timer, 'Time::Stopwatch'; #start counting time
#printing usage if no argument set
if (!$ARGV[0]){ $0 =~ s/.*\\(.*)/$1/; #name of this script without path
print "\nUSAGE:\nperl $0 <directory containing results>\n"; exit; }
#openning the directory with extracted titles and description
$dir = $ARGV[0]; opendir(DIR, $dir) or die "I cannot open dir: $dir";
# ADD YOURS ADDRESS AND COMMENT THE OTHERS --- HERE ---
my $address = "~/../../usr/local/TreeTagger/"; #Karolina_linux
my $tokenize = $address."cmd/tokenise-utf8.pl"; my $treetagger = $address."bin/tree-tagger"; my $french = $address."lib/french-utf8.par";
while (my $file = readdir(DIR)) { #open directory
next unless ($file =~ m/[^(_t)]\.txt$/); #open each file.txt
my $file = $dir."/".$file; my $save_into = substr($file,0,-4)."_t.txt"; print "\ntagging of file : $file\n"; #run treetagger using terminal
system("perl $tokenize $file | $treetagger -lemma -token $french > $save_into"); }
closedir(DIR);
print "\n\n === DONE in $timer s ===\n"; #printing time
|
Les fichiers étiquetés sont au format SORTIE-rubrique_t.txt dont voici un exemple, SORTIE-livres_t.txt.
Voici une prise d'écran du dossier de sortie :
:
Les fichiers de sortie du script ci-dessus subissent une
transformation supplémentaire pour nous fournir des fichiers au format
xml (voir le deuxième organigramme en bas). Les fichiers de format
SORTIE-rubrique_t.txt sont regroupés dans un nouveau dossier avant le
lancement du script.
Voici le code du script perl script-tagging-with-treetagger-xml.pl qui est le "front end" du script perl qui suit.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | #!/usr/bin/perl
use warnings; use Time::Stopwatch;
################################################################
#
# requires
# tagged files by script-tagging-with-treetagger-txt.pl
# treetagger2xml-utf8.pl in same directory
#
################################################################
tie my $timer, 'Time::Stopwatch'; #start counting time
#printing usage if no argument set
if (!$ARGV[0]){ $0 =~ s/.*\\(.*)/$1/; #name of this script without path
print "\nUSAGE:\nperl $0 <directory containing results>\n"; exit; }
#opening the dir. with extracted titles and description
$dir = $ARGV[0]; opendir(DIR, $dir) or die "I cannot open dir: $dir";
while (my $file = readdir(DIR)) { #open directory
next unless ($file =~ m/_t\.txt$/); #open each file.txt
my $file = $dir."/".$file; print "\nprocessing to xml : $file\n"; #run treetagger using terminal
system("perl treetagger2xml-utf8.pl $file UTF-8"); }
closedir(DIR);
print "\n\n === DONE in $timer s ===\n"; #printing time
|
Voici le code du script perl treetagger2xml-utf8.pl qui doit être dans le même dossier que script-tagging-with-treetagger-xml.pl lors du lancement de ce dernier :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | #!/usr/bin/perl
use warnings; use Time::Stopwatch;
##############################!/usr/bin/perl
use Unicode::String qw(utf8);
################################################################
# Format d'entree : un texte étiqueté et lemmatisé par
# Treetagger et un format d'encodage
# Format de Sortie : le même texte au format xml (en utf-8)
################################################################
# Usage
$ChaineUsage="Usage : treetagger2xml-utf8.pl <Fichier> <encodage>\n"; if (@ARGV!=2) { die $ChaineUsage; }
&ouvre; &entete; &traitement; &fin; &ferme;
################################################################
# Récupération des arguments et ouverture des tampons
sub ouvre { $FichierEntree=$ARGV[0]; $encodage=$ARGV[1]; open(Entree,"<:encoding($encodage)",$FichierEntree); $FichierSortie=$FichierEntree . ".xml"; open(Sortie,">:encoding(utf-8)",$FichierSortie); }
# Entête de document XML
sub entete { print Sortie "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n"; print Sortie "<document>\n"; print Sortie "<article>\n"; }
# Traitement
sub traitement { while ($Ligne = <Entree>) { if (uc($encodage) ne "UTF-8") {utf8($Ligne);} if ($Ligne!~/\ô\¯\:\\ô\¯\:\\/) { # Remplacement des guillemets par <![CDATA["]]> (évite erreur d'interprétation XML)
$Ligne=~s/\"/<![CDATA[\"]]>/g; $Ligne=~s/([^\t]*)\t([^\t]*)\t(.*)/<element>\n <data type=\"type\">$2<\/data>\n <data type=\"lemma\">$3<\/data>\n <data type=\"string\">$1<\/data>\n<\/element>/g; $Ligne=~s/<unknown>/unknown/g; $Ligne=~s/<img .*>/img<\/data>/g; $Ligne=~s/<\/a>/unknown/g; $Ligne=~s/<a href.*>/url<\/data>/g; $Ligne=~s/&/ampersand/g; print Sortie $Ligne; } } } # Fin de fichier
sub fin { print Sortie "</article>\n"; print Sortie "</document>\n"; }
# Fermeture des tampons
sub ferme { close(Entree); close(Sortie); }###################################
#
# requires
# tagged files by script-tagging-with-treetagger-txt.pl
# treetagger2xml-utf8.pl in same directory
#
################################################################
tie my $timer, 'Time::Stopwatch'; #start counting time
#printing usage if no argument set
if (!$ARGV[0]){ $0 =~ s/.*\\(.*)/$1/; #name of this script without path
print "\nUSAGE:\nperl $0 <directory containing results>\n"; exit; }
#opening the dir. with extracted titles and description
$dir = $ARGV[0]; opendir(DIR, $dir) or die "I cannot open dir: $dir";
while (my $file = readdir(DIR)) { #open directory
next unless ($file =~ m/_t\.txt$/); #open each file.txt
my $file = $dir."/".$file; print "\nprocessing to xml : $file\n"; #run treetagger using terminal
system("perl treetagger2xml-utf8.pl $file UTF-8"); }
closedir(DIR);
print "\n\n === DONE in $timer s ===\n"; #printing time
|
À la sortie, nous avons des fichiers au format xml, qui ont le format SORTIE-rubrique_t.txt.xml (comme par exemple SORTIE-livres_t.txt.xml) dans un dossier qui maintenant ressemble à ceci :
L'étiquetage avec Cordial
Cette étape doit se faire "à la main" car il n'est pas possible de
l'intégrer dans un script perl. Le logiciel, payant, ne fonctionne que
sous Windows et ne marche que dans l'encodage ANSI. Vu l'impossibilité
de travailler avec des gros fichiers dans Cordial, nous avons fait un
traitement sur un échantillon du corpus et les traitements
suivants sur la sortie texte du traitement Treetagger. Voici un fichier exemple de sortie issu du traitement d'étiquetage de Cordial SORTIE-livres_ISO-8859-1.cnr. Nous l'affichons ici comme une capture d'écran car le fichier lui-même est assez grand.
Nous avons donc trois types de sortie à la fin de cette série de traitements.