Skip to content

Instantly share code, notes, and snippets.

@wengzilla
Created January 6, 2014 05:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wengzilla/8278652 to your computer and use it in GitHub Desktop.
Save wengzilla/8278652 to your computer and use it in GitHub Desktop.
Wheel of Fortune analysis of Bonus Round answers.
require 'nokogiri'
require 'open-uri'
class Analyzer
attr_accessor :frequency_table
def initialize
@frequency_table = Hash.new(0)
end
def insert(word)
word.chomp.split(//).each { |letter| self.frequency_table[letter] += 1 }
end
def results(limit = 10)
frequency_table.sort_by{ |letter, count| -1 * count }.first(limit).map do |letter, value|
"#{letter} #{value}"
end
end
end
class WOFPage
attr_accessor :parsed_html
def initialize(uri)
@parsed_html = Nokogiri::HTML(open(uri))
rescue OpenURI::HTTPError => e
end
def answers
return [] if parsed_html.nil?
answers_column.reject{ |answers| answers.empty? }
end
private
def answers_column
rows.collect do |row|
answer = scrub_answer(row.at_xpath('td[3]/text()'))
unless answer.length > 5
answer = scrub_answer(row.at_xpath('td[2]/text()'))
end
answer
end
end
def scrub_answer(answer)
answer.to_s.upcase.gsub(/ /, "")
end
def rows
parsed_html.search('tr')
end
end
class Runner
def analyze
File.open('answers.txt', 'w') { |file| file.write(answers) }
answers.flatten.each { |word| analyzer.insert(word) }
puts analyzer.results(26)
end
private
def analyzer
@analyzer ||= Analyzer.new
end
def answers
answers_uris.collect { |uri| WOFPage.new(uri).answers }.flatten
end
def answers_uris
(1983..2014).map do |year|
Date::MONTHNAMES.compact.map do |month|
answers_uri(year, month)
end
end.flatten
end
def answers_uri(year, month)
"http://www.angelfire.com/mi4/malldirectories/wheel/#{year}/#{month.downcase}.html"
end
end
Runner.new.analyze
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment