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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
#!/usr/bin/perl
use XML::Twig;
use File::Spec;
use encoding 'utf8';
#-----------------------------------------------------------
# MAIN
#-----------------------------------------------------------
#Déclaration des fichiers Xml:
my $FichierOutput = 'D:\\Memoire Mastere\\Script\\Script_final\\Script_Grapheme-phoneme\\output.xml';
my $FichierReglePhonetique = 'D:\\Memoire Mastere\\Script\\Script_final\\Script_Grapheme-phoneme\\RegleDePhonetisation.xml';
my $FichierBAseException = 'D:\\Memoire Mastere\\Script\\Script_final\\Script_Grapheme-phoneme\\BaseException.xml';
#Déclaration du fichier résultat
my $RepBase = 'D:\\Memoire Mastere\\Script\\Script_final\\Script_Grapheme-phoneme';
my $FicAraOut1 = File::Spec->catfile( $RepBase, 'outreglesHash.txt' );
open my $fh_Aran, '>', $FicAraOut1 or die "Impossible ouvrir fichier $FicAraOut1 en sortie\n";
print {$fh_Aran} "Grapheme :"," Phoneme : \n";
my $phoneme="";
my %exceptions;
my $graph;
open my $EXC, "<", $FichierBAseException or die "Ouverture impossible du fichier $FichierBAseException $! \n";
while (<$EXC>)
{
$graph = $1 if /<Grapheme>([^<]+)<\/Grapheme>/;
if ( /<Phoneme>([^<]+)<\/Phoneme>/ )
{
$exceptions{$graph} = $1;
}
}
close $EXC;
# les exceptions sont maintenant chargées dans le hash %exceptions. Continuer en lisant le fichier des régles
my %regles;
my ($graph, $cd, $cg);
open my $RULES, "<", $FichierReglePhonetique or die "Ouverture impossible du fichier $FichierReglePhonetique $! \n";
while (<$RULES>) {
$cd = $1 if /<Composant Nom="C-Droit" Utilite="Oui">([^<]+)<\/Composant>/; # $cd sera undef si utilite = non
$graph = $1 if /<Composant Nom="Graph">([^<]+)<\/Composant>/;
$cg = $1 if /<Composant Nom="C-Gauche" Utilite="Oui">([^<]+)<\/Composant>/;
if ( /<Composant Nom="Remplacement">([^<]+)<\/Composant>/ )
{
my $repl = $1;
# on va créer une clef de hash en concaténant $cd, $cg et $graph (avec séparateur) si ces élements sont définis
if (defined $cd)
{
if (defined $cg)
{
$regles{$cd . '|' . $graph . '|' . $cg} = $repl;
}
else
{
$regles{$cd . '|' . $graph} = $repl;
}
}
else
{
if (defined $cg)
{
$regles{ $graph . '|' . $cg} = $repl;
}
else
{
$regles{$graph} = $repl;
}
}
undef $cd;
undef $cg;
}
}
#foreach my $graph ( keys %regles )
#{
# print {$fh_Aran} $graph, " ", $regles{$graph},"\n";
#}
close $RULES;
# les regles sont maintenant chargée dans le hash %regles
# LEcture du fichier output
my $twig_output = XML::Twig->new(
Twig_handlers => {
'Mot' => \&mot,
},
);
$twig_output->parsefile($FichierOutput);
$twig_output->purge;
sub mot
{
my ($twig, $twig_mot) = @_;
my $mot = $twig_mot->att('Nom');
# 1- Mot identique à grapheme
foreach my $graph ( keys %exceptions)
{
if ( $graph eq $mot )
{
print {$fh_Aran} "$mot $exceptions{$graph}\n";
last;
}
}
print {$fh_Aran} " $mot ";
# 2- Lecture des caractéres
foreach my $twig_caractere ( $twig_mot->children('caractere') )
{
# composant
foreach my $graph ( keys %regles )
{
if ($twig_caractere->text()!~/\x{0652}/)
{
if ($twig_caractere->text()!~/\x{0651}/)
{
if ( $graph eq $twig_caractere->text )
{
$phoneme=$phoneme.$regles{$graph};
}
}
else
{
my $prevSibling = $twig_caractere->prev_sibling();
if ( $graph eq $prevSibling->text )
{
$phoneme=$phoneme.$regles{$graph};
}
}
}
}
}
print {$fh_Aran} "$phoneme\n";
$phoneme="";
} |
Partager