Skip to content

Instantly share code, notes, and snippets.

@drusepth
Forked from AlexNisnevich/score_subreddit.rb
Last active August 29, 2015 14:25
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 drusepth/dbeeb6af956c11831252 to your computer and use it in GitHub Desktop.
Save drusepth/dbeeb6af956c11831252 to your computer and use it in GitHub Desktop.
#
# 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