【发布时间】:2015-01-10 23:30:47
【问题描述】:
我有这个使用哈希表的脚本:
#!/usr/bin/env perl
use strict; use warnings;
my $hash = {
'cat' => {
"félin" => '0.500000',
'chat' => '0.600000',
'chatterie' => '0.300000'
'chien' => '0.01000'
},
'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 " ";
}
现在,我有这个输出:
I love my cat[chat;félin;chatterie;chien] and my rabbit[lapin] canteen[cantine;ménagère] !
我需要根据频率(存储在我的哈希中)获得 nbest 键值。例如,我想根据频率获得 3 个最好的翻译,如下所示:
I love my cat[chat;félin;chatterie] and my rabbit[lapin] canteen[cantine;ménagère] !
如何更改我的代码以考虑每个值的频率并打印 nbest 值?
感谢您的帮助。
【问题讨论】:
-
按数字排序键?
-
自然是根据hash的信息。例如对于第一个条目:1) chat 2) félin 3) chatterie 4) chien。然后,在我的示例中,我想获得 3best 值。
标签: perl sorting hash hashtable perl-data-structures