Your word frequency calculator?

Discussion in 'Ruby' started by C. Pohjanraito, Dec 17, 2008.

  1. 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
     
    C. Pohjanraito, Dec 17, 2008
    #1
    1. Advertising

  2. C. Pohjanraito

    Todd Benson Guest

    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=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
     
    Todd Benson, Dec 17, 2008
    #2
    1. Advertising

  3. 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)
     
    David A. Black, Dec 18, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin
    Replies:
    16
    Views:
    8,384
    Dale King
    Apr 19, 2005
  2. Gordon Odell

    Looking for a word frequency counter.

    Gordon Odell, Feb 15, 2006, in forum: HTML
    Replies:
    2
    Views:
    510
    Stefan B Rusynko
    Feb 16, 2006
  3. x1
    Replies:
    9
    Views:
    373
    Rick DeNatale
    Oct 12, 2006
  4. PerlFAQ Server
    Replies:
    0
    Views:
    225
    PerlFAQ Server
    Feb 1, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    213
    PerlFAQ Server
    Mar 26, 2011
Loading...

Share This Page