From: "Jesús Gabriel y Galán" Date: 2010-11-06T03:05:56+09:00 Subject: Re: Conta occorrenze in Hash 2010/11/5 Daniel Jovanovski : > 1) > procedura coppie_frequenti() che stampi l'elenco delle coppie di > parole [p1 p2] tali che nell'array la parola p1 sia immediatamente > seguita da p2 almeno 10 volte. L'output dovrebbe essere quindi qualcosa > del tipo: > >  [diritto di]: 13 volte >  [la legge]: 21 volte >  [può essere]: 12 volte > > Come si fa a contate le occorrenze nell'hash usando come chiave una > coppia di parole (= un array di due elementi contenente le 2 parole). > > def coppie_frequenti() >  h = Hash.new(0) >  File.read("cost54.txt").downcase!.each do |i| #ciclo che conta le >    h[i]+= 1        #occorrenze in un file di testo. come si trasforma > in >  end              #ciclo che conta copie di parole > > 2) > Come posso stampare il risultato del Hash in questo modo: > 10 volte:  diritti casi loro norme > 11 volte:  tutela limiti lavoro > 12 volte:  cittadini > 13 volte:  delle alle > 14 volte:  ogni tutti > 16 volte:  hanno sono legge. > 19 volte:  essere > 20 volte:  dalla > 22 volte:  repubblica alla > 27 volte:  diritto > 28 volte:  legge > 34 volte:  della > 54 volte:  art > > avvendo mi stampato questo: > 10 volte: norme > 10 volte: loro > 10 volte: diritti > 10 volte: casi > 11 volte: lavoro > 11 volte: tutela > 11 volte: limiti > 12 volte: cittadini > 13 volte: alle > 13 volte: delle > 14 volte: ogni > 14 volte: tutti > 16 volte: hanno > 16 volte: sono > 16 volte: legge. > 19 volte: essere > 20 volte: dalla > 22 volte: alla > 22 volte: repubblica > 27 volte: diritto > 28 volte: legge > 34 volte: della > 54 volte: art. > > > il cosice: > def parole_frequenti2() >  h = Hash.new(0) >  File.read("cost54.txt").downcase!.split(" ").each do |i| #ciclo che > conta le occorrenze in un file di testo >    h[i] += 1 >  end > >  h.delete_if { |key,val| key.size <= 3 or val < 10} >  h.sort { |key,val| key[1] <=> val[1]  }.each {|key, val| puts "#{val} > volte: #{key}" } >  array1=h.to_a > >  n=10 >  while n <= 54 >    #cosa dovrei metere dentro il ciclo per ottenere il risultato??? >    n+=1 >  end > end If I understand correctly, you want to count runs of repeated words in the document, and choose the ones that appear more than 10 times, sorted by frequency. If you restrict to a number of words (for example, two consecutive words), you can do something like: irb(main):001:0> text=< "the only thing that this means, is that the only thing that matters is that nothing matters\n" irb(main):004:0> text.split => ["the", "only", "thing", "that", "this", "means,", "is", "that", "the", "only", "thing", "that", "matters", "is", "that", "nothing", "matters"] irb(main):010:0> h = Hash.new 0 => {} irb(main):012:0> text.split(/\W/).delete_if {|x| x.empty?}.each_cons(2) {|w| h[w] += 1} => nil irb(main):015:0> h.sort_by{|k,v| -v} => [[["is", "that"], 2], [["only", "thing"], 2], [["thing", "that"], 2], [["the", "only"], 2], [["that", "matters"], 1], [["that", "this"], 1], [["means", "is"], 1], [["matters", "is"], 1], [["this", "means"], 1], [["that", "nothing"], 1], [["that", "the"], 1], [["nothing", "matters"], 1]] First, I splitted by word boundaries, but your splitting by " " might work for your data. Then I traverse all word pairs and count the occurrence of each pair, which I then sort by frequency. If you want to generalize to arbitrary number of words, that's a more interesting problem :-). Jesus.