Your word frequency calculator?

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 J.:

freq =
loop {
data = ( or break) + (STDIN.gets || “”)
for word in data.downcase!.tr!(’^a-z’,’ ').split
freq[word] += 1

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

Whats yours like? And what about phrase frequency?

Casimir P.

On Wed, Dec 17, 2008 at 1:42 AM, C. Pohjanraito
[email protected] wrote:

for word in data.downcase!.tr!(‘^a-z’,’ ').split
freq[word] += 1

print{|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

p (‘^a-z'’,’

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


Hi –

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

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! will blow up.