-
Notifications
You must be signed in to change notification settings - Fork 1
/
gender.py
39 lines (33 loc) · 1.13 KB
/
gender.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# Methods to predict gender based on author name
# Currently uses the genderize.io API, which allows for
# 1000 free requests per ip addr per day
# TODO: use a proxy once the limit is reached
import requests
def get_gender(name, country_id="US"):
try:
name = name.split()[0]
except:
return 'none'
response = requests.get("https://api.genderize.io?name=" + name + "&country_id=" + country_id)
if response.status_code != 200:
print(response.status_code)
if response.status_code == 429:
return 429
else:
return 'none'
else:
return response.json()['gender']
def get_gender_batch(names, country_id="US"):
request_base = "https://api.genderize.io/?"
genders = {}
for name in names:
name = name.split()[0].lower()
genders[name] = ''
request_base += "name[]=" + name + "&"
response = requests.get(request_base[:-1])
if response.status_code != 200:
print(response.status_code)
else:
for piece in response.json():
genders[piece['name']] = piece['gender']
return genders