sorting - get nbest key-value pairs hash table in Perl -
i have script utilize hash table:
#!/usr/bin/env perl utilize strict; utilize warnings; $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' } }; $text = "i love cat , rabbit canteen !\n"; foreach $word (split "\s+", $text) { print $word; exists $hash->{$word} , print "[" . join(";", keys %{ $hash->{$word} }) . "]"; print " "; }
for now, have output:
i love cat[chat;félin;chatterie;chien] , rabbit[lapin] canteen[cantine;ménagère] !
i need have nbest key value according frequencies (stored in hash). example, want have 3 best translations according frequencies this:
i love cat[chat;félin;chatterie] , rabbit[lapin] canteen[cantine;ménagère] !
how can alter code take business relationship frequencies of each values , print nbest values ?
thanks help.
the tidiest way write subroutine returns n frequent translations given word. have written best_n
in programme below that. uses rev_nsort_by
list::utilsby
sort succinctly. isn't core module, , may need installed.
i have used executable substitution modify string in-place.
use utf8; utilize strict; utilize warnings; utilize list::utilsby qw/ rev_nsort_by /; $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', } }; $text = "i love cat , rabbit canteen !\n"; $text =~ s{(\s+)}{ $hash->{$1} ? sprintf '[%s]', join(';', best_n($1, 3)) : $1; }ge; print $text; sub best_n { ($word, $n) = @_; $item = $hash->{$word}; @xlate = rev_nsort_by { $item->{$_} } keys %$item; $n = $n > @xlate ? $#xlate : $n - 1; @xlate[0..$n]; }
output
i love [chat;félin;chatterie] , [lapin] [cantine;ménagère] !
perl sorting hash hashtable perl-data-structures
No comments:
Post a Comment