Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Your word frequency calculator?

Reply
Thread Tools

Your word frequency calculator?

 
 
C. Pohjanraito
Guest
Posts: n/a
 
      12-17-2008
Hi! I am studying various texts and code.

Word frequency calculator is one basic tool. I find it an interesting,
elementary problem to solve.

I found this word frequency analyser by William James:

freq = Hash.new(0)
loop {
data = (STDIN.read(4095) or break) + (STDIN.gets || "")
for word in data.downcase!.tr!('^a-z',' ').split
freq[word] += 1
end
}

print freq.to_a.map{|x| sprintf("%7d %s\n",x[1],x[0])}.sort.reverse


Whats yours like? And what about phrase frequency?


Casimir Pohjanraito


 
Reply With Quote
 
 
 
 
Todd Benson
Guest
Posts: n/a
 
      12-17-2008
On Wed, Dec 17, 2008 at 1:42 AM, C. Pohjanraito
<> wrote:
> Hi! I am studying various texts and code.
>
> Word frequency calculator is one basic tool. I find it an interesting,
> elementary problem to solve.
>
> I found this word frequency analyser by William James:
>
> freq = Hash.new(0)
> loop {
> data = (STDIN.read(4095) or break) + (STDIN.gets || "")
> for word in data.downcase!.tr!('^a-z',' ').split
> freq[word] += 1
> end
> }
>
> print freq.to_a.map{|x| sprintf("%7d %s\n",x[1],x[0])}.sort.reverse
>
>
> Whats yours like?


With 1.8.7 and s being data (includes apostrophes for words like "don't")...

p (a=s.downcase.tr('^a-z\'','
').split).uniq.inject(Hash.new(0)){|h,i|h[i]=a.count(i);h}

...of course leaving off #uniq and just doing +=1 instead of #count
would be shorter, but I like the concept behind this one instead

Todd

 
Reply With Quote
 
 
 
 
David A. Black
Guest
Posts: n/a
 
      12-18-2008
Hi --

On Wed, 17 Dec 2008, C. Pohjanraito wrote:

> Hi! I am studying various texts and code.
>
> Word frequency calculator is one basic tool. I find it an interesting,
> elementary problem to solve.
>
> I found this word frequency analyser by William James:
>
> freq = Hash.new(0)
> loop {
> data = (STDIN.read(4095) or break) + (STDIN.gets || "")
> for word in data.downcase!.tr!('^a-z',' ').split


That's a dangerous technique, because str.downcase! is nil if there's
no change to the string, and nil.tr! will blow up.


David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 6.15 How can I print out a word-frequency or line-frequency summary? PerlFAQ Server Perl Misc 0 03-26-2011 04:00 AM
FAQ 6.15 How can I print out a word-frequency or line-frequency summary? PerlFAQ Server Perl Misc 0 02-01-2011 11:00 AM
Counting Frequency of Values in an Array (And Sorting by Frequency?) x1 Ruby 9 10-12-2006 04:04 PM
Looking for a word frequency counter. Gordon Odell HTML 2 02-16-2006 10:16 AM
faster data structure to count word frequency? Kevin Java 16 04-19-2005 05:13 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57