Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: hash of unique words

by choroba (Abbot)
on Apr 22, 2013 at 16:37 UTC ( #1029922=note: print w/ replies, xml ) Need Help??


in reply to hash of unique words

Rather than hash of arrays, use a hash of hashes. At the end, replace each inner hash with its number of keys:

#!/usr/bin/perl use warnings; use strict; use Data::Dumper; open my $ENGLISH, '<', 'corpus.e' or die $!; open my $FOREIGN, '<', 'corpus.f' or die $!; chomp(my @sents_en = <$ENGLISH>); chomp(my @sents_f = <$FOREIGN>); my %uniform; for my $sentence_index (0 .. $#sents_en) { my @words_en = split ' ', $sents_en[$sentence_index]; my @words_f = split ' ', $sents_f[$sentence_index]; for my $word_index (0 .. $#words_en) { $uniform{ $words_en[$word_index] }{$_}++ for @words_f; } } for my $word (keys %uniform) { $uniform{$word} = keys %{ $uniform{$word} }; } print Dumper \%uniform;
لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ


Comment on Re: hash of unique words
Download Code
Re^2: hash of unique words
by Ninke (Novice) on Apr 22, 2013 at 17:50 UTC
    Choroba, thanx very much, that does exactly what I want. A nice trick with {$_} for @words, that reduced the number of lines twice:) Though I don't understand the magic to the end, especially when a hash of hashes ($uniform{ $words_en$word_index }{$_}) turnes into a one-dimentional hash: $uniform{$word}. I just need to use it in practice and then I'll get it:)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1029922]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (14)
As of 2014-08-29 12:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (280 votes), past polls