Created
November 9, 2012 11:32
-
-
Save AlexNisnevich/4045249 to your computer and use it in GitHub Desktop.
score_subreddit: Computes the Flesch-Kincaid grade level of the last 25 comments of the given subreddits.
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
# | |
# Usage: ruby score_subreddit.rb <list of subreddits> | |
# | |
# Computes the Flesch-Kincaid grade level of the last 25 comments of the given subreddits. | |
# | |
# For example: | |
# > ruby score_subreddit.rb science worldnews funny | |
# science: 8.33 | |
# worldnews: 9.71 | |
# funny: 5.32 | |
# | |
require 'curb' | |
require 'json' | |
require 'cgi' | |
require 'nokogiri' | |
# taken from https://github.com/matstc/text-analysis-utils/blob/master/bin/readability-of | |
def flesch_kincaid_grade_level(text) | |
def vowels w | |
w.split(/b|c|d|f|g|h|j|k|l|m|n|p|q|r|s|t|v|w|z/i).reject{|s|s.empty?} | |
end | |
words = text.split(" ").size.to_f | |
sentences = text.split(/\.|\?|!/).reject{|s| s.strip.empty?}.size.to_f | |
syllables = text.split(" ").inject([]){|sum, w| sum + vowels(w)} | |
syllables = syllables.size.to_f * 0.9 # for silent vowels | |
grade_level = (0.39 * (words / sentences) + 11.8 * (syllables / words) - 15.59) | |
return (grade_level * 100).floor() / 100.0 | |
end | |
def score_subreddit(subreddit) | |
response = Curl::Easy.perform("http://www.reddit.com/r/#{ subreddit }/comments.json") | |
response_json = JSON.parse(response.body_str) | |
text = "" | |
response_json['data']['children'].each{ |comment| | |
text += Nokogiri::HTML(CGI.unescapeHTML(comment['data']['body_html'])).text | |
} | |
return flesch_kincaid_grade_level(text) | |
end | |
ARGV.each do |subreddit| | |
puts "#{subreddit}: #{score_subreddit(subreddit)}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment