Skip to content

Instantly share code, notes, and snippets.

@jcody
Created December 6, 2012 10:54
Show Gist options
  • Save jcody/4223643 to your computer and use it in GitHub Desktop.
Save jcody/4223643 to your computer and use it in GitHub Desktop.
Reddit word frequency counter of titles submitted to the /r/christmas subreddit. For ACM@UCSB
require 'httparty'
require 'rubygems'
require 'json'
# Create word frequency hash
freqs = Hash.new(0)
reddit_api_url = "http://www.reddit.com/r/christmas/new.json?sort=new&limit=100"
resp = HTTParty.get(reddit_api_url)
data = JSON.parse(resp.body)
data['data']['children'].each do |submission|
words = submission['data']['title'].split(/[^a-zA-Z]/)
# Add occurance to word frequency hash
words.each do |word|
word.upcase!
freqs[word] += 1
end
end
# Sort and return word frequency hash
freqs = freqs.sort_by {|x,y| y }
freqs.reverse!
freqs.each {|word, freq| puts word+' '+freq.to_s}
# Profit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment