Created
December 6, 2012 10:54
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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