I have this data structure (a multidimensional hash table):
$VAR1 = {
'cat' => {
"félin" => '0.500000',
'chat' => '0.600000'
},
'rabbit' => {
'lapin' => '0.600000'
},
'canteen' => {
"ménagère" => '0.400000',
'cantine' => '0.600000'
}
};
My goal is to read a tokenized text, and for each word I need to find the translation(s). For my tokens, I read my text and I create an array like that:
##store each word to translate in a table
while(my $text_to_translate = <TEXTTOTRANSLATE>)
{
my @temp = split(/ /, $text_to_translate);
push(@tokens, @temp);
}
My problem is to find the best way (and the fastest) to lookup into the bidimensional hash table and print the possible translation(s) like that:
I love my cat[chat;félin] and my rabbit[lapin].
For now, I had create this loop but It didn't work well, and I think it is not the best way:
foreach my $source (sort keys %hash) {
foreach my $target (keys %{ $hash{$source} }) {
my $val = $source;
##loop into each tokens
foreach (@tokens) {
my $actualWord = $_;
if($val eq $actualWord){
print $actualWord."==>".$target."\n";
}
else{
print $actualWord."\n";
next;
}
}
}
}
What I would do :
#!/usr/bin/env perl
use strict; use warnings;
my $hash = {
'cat' => {
"félin" => '0.500000',
'chat' => '0.600000'
},
'rabbit' => {
'lapin' => '0.600000'
},
'canteen' => {
"ménagère" => '0.400000',
'cantine' => '0.600000'
}
};
my $text = "I love my cat and my rabbit canteen !\n";
foreach my $word (split /\s/, $text) {
print $word;
exists $hash->{$word}
and print "[" . join(";", keys %{ $hash->{$word} }) . "]";
print " ";
}
print "\n";
Output:
I love my cat[chat;félin] and my rabbit[lapin] canteen[cantine;ménagère] !