Last active
August 29, 2015 14:21
-
-
Save ideasasylum/638ef43ee178aca097eb to your computer and use it in GitHub Desktop.
Find out the gender balance from a list of names
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
require 'rest-client' | |
file = 'names.txt' | |
key = 'YOUR GENDER API KEY' | |
raw = [] | |
results = Hash.new 0 | |
errors = 0 | |
num_names = 0 | |
File.foreach file do |name| | |
params = {key: key} | |
params[:name] = name.split(' ')[0] | |
response = RestClient.get 'https://gender-api.com/get', params: params | |
raw << response.to_s | |
if response.code == 200 | |
json = JSON.parse(response) | |
gender = json['gender'] | |
num_names += 1 | |
results[gender] = results[gender] + 1 | |
break if num_names > 500 | |
else | |
errors += 1 | |
end | |
end | |
results.each do |gender, count| | |
percent = (count.to_f / num_names.to_f) * 100 | |
puts "#{gender}: #{count} (%.2f%)" % percent | |
end | |
puts "#{errors} Errors" | |
File.open("raw.json", "w+") do |f| | |
f.puts raw | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment