ruby / programming / text analysis

Reading time:
1 min. | 114 words

I wrote this simple Ruby program while enrolled in Prof. Steve Ramsay’s Electronic Text course.  I’m releasing it into the wild for anyone that might find it useful. You’ll need to have Ruby installed on your machine to run the program. Simply pass the program a file name and it will output to the screen a numerically-sorted list of word frequencies.


#!/usr/bin/ruby -w

# FREQr.rb
#
# Basic word frequency generator.
#
# Written by Jason Heppler
#
# Last Modified: Wed Nov 3 23:53:52 CDT 2010

# Pass the program a file to open
# e.g., $ ruby freqr.rb poe.txt
filename = File.new(ARGV[0]).read().downcase().scan(/[\w']+/)
frequency = Hash.new(0)
words.each { |word| frequency[word] +=1 }
frequency.sort_by { |x,y| y }.reverse().each{ |w,f| puts "#{f}, #{w}" }

If you wish to make changes to the tool or download a copy, you can find it in my Github repository.


About

Greetings! My name is Jason Heppler. I am a Digital Engagement Librarian and Assistant Professor of History at the University of Nebraska at Omaha and a scholar of the twentieth-century United States. I often write here about the history of the North American West, technology, the environment, politics, culture, and coffee. You can follow me on Twitter, or learn more about me.

Where

Search