Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
def author_gender(all_authors, threshold=0.6):
for key, value in all_authors.items():
# Ensure the author being evaluated has not been enriched using the API — this avoids unnecessary API calls
if 'gender' not in value:
# Retrieve the probability of the gender prediction of the author
# and compare it to the threshold set in line 11.
result = genderize_api_connect(value['first_name'])
if result['probability'] > threshold:
# Add the gender to the author dictionary object and ensure that it is capitalized.
value['gender'] = result['gender'].capitalize()
# Handle exceptions, specifically if no gender is returned from the API.
# This case is handled by assigning the gender as “Unknown”.
except KeyError:
value['gender'] = "Unknown"
return all_authors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.