Skip to content

anonymous /haskelldex.rb
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
#!/usr/bin/env ruby
require 'nokogiri'
require 'open-uri'
Doc = Nokogiri::HTML(open "http://www.reddit.com/r/programming")
Entries = Doc.search("//div[@class='entry unvoted']")
.map{ |e| e.at_xpath(".//a[@class='comments may-blank']")}
.compact.map{|e| e.attributes['href'].value}
.map{|e| Nokogiri::HTML(open e)}
def get_comments page
xpath = "//div[@class='usertext-body may-blank-within']"
# First element is always the subreddit description
page.search(xpath).drop(1).map(&:text)
end
def count_haskells str
str.split(/[^a-zA-Z]/).delete_if{|w| not w.casecmp("haskell").zero?}.length
end
def generate_report
title_to_comments = Hash[Entries.map{|e| [e.title, get_comments(e)]}]
title_to_haskells = Hash[title_to_comments.map do |k, v|
hs = v.map{|comment| count_haskells comment}.inject(0, :+)
[k, hs]
end]
total_comments = title_to_comments.values.map(&:length).inject(0, :+)
total_haskells = title_to_haskells.values.inject(0, :+)
puts "Comments: #{total_comments}"
puts "Haskell mentions: #{total_haskells}"
title_to_haskells.each do |title, hs|
puts "#{title.chomp " : programming"}"
puts "\t #{hs} haskell mentions"
end
end
generate_report
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.