Skip to content

Instantly share code, notes, and snippets.

@radex
Created May 2, 2014 19:53
Show Gist options
  • Save radex/e759ec36b71c953f0c3e to your computer and use it in GitHub Desktop.
Save radex/e759ec36b71c953f0c3e to your computer and use it in GitHub Desktop.
require 'csv'
tweets = CSV.read('tweets.csv')
tweets.shift
stats = tweets
.reject { |t| t[5] =~ /^RT/ }
.group_by { |t|
date = Date.parse t[3]
"#{date.year}-#{date.month}"
}
.map { |month, tweets|
swearing = tweets
.map { |t| t[5] }
.reject { |t| t =~ /^RT/ }
.select { |t| t=~/(fuck|shit|crap| ass )/i }
[month, tweets.count, swearing.count, (swearing.count.to_f / tweets.count.to_f).round(3).to_s.sub('.', ',')]
}
.reverse
# p stats
stats.unshift %w(Date All Swearing %)
IO.write 'stats.csv', stats.map { |row| row.join(';') }.join("\n") # to_csv doesn't work with Numbers.app
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment