Skip to content

Instantly share code, notes, and snippets.

@zombiecalypse
Created September 25, 2014 19:10
Show Gist options
  • Save zombiecalypse/a4d7cfaaf03e4b2d031f to your computer and use it in GitHub Desktop.
Save zombiecalypse/a4d7cfaaf03e4b2d031f to your computer and use it in GitHub Desktop.
A script estimating the gender distribution of a twitter user's people they follow.
#!/usr/bin/env ruby
#
# A script to approximate the gender distribution of your twitter follows.
#
# gem install guess t
#
# ruby twitter-gender.rb USERNAME
#
# Firstly outputs a list of estimations of the gender of all people that user
# follows.
#
# Secondly a sum of confidences in the estimations of each gender (unknown
# having no confidence). This could be interesting if it differs by more than a
# few percent from the numbers below.
#
# Lastly a count of the genders.
require 'net/http'
require 'json'
require 'csv'
require 'guess'
genders = []
user = ARGV.length > 0 ? ARGV[0] : ''
csvout = %x[t followings -c #{user}]
CSV.parse(csvout, headers: true) do |row|
gender = Guess.gender(row["Name"])
gender[:name] = row["Name"]
puts "%20s: %s (%.3f)" % [row["Name"], gender[:gender], gender[:confidence]] if gender[:confidence]
genders << gender
end
grouped = genders.group_by {|g| g[:gender]}
p grouped.collect {|e| [e[0], e[1].inject(0) {|e, f| e+f[:confidence] rescue nil} ]}
p grouped.collect {|e,f| [e, f.length]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment