Your word frequency calculator?

C

C. Pohjanraito

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
 
T

Todd Benson

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=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
 
D

David A. Black

Hi --

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top