Skip to content

Instantly share code, notes, and snippets.

View kassuts's full-sized avatar

Eitan Kassuto kassuts

View GitHub Profile
def enrich_keywords(all_articles, all_keywords, threshold=0.8, keyword_limit=10):
params = {}
limit = 0
for key, value in all_articles.items():
# Concatenate Title and Abstract and assign to dictionary item 'text' for API call.
params['text'] = value[0] + ". " + value[1]
content = aylien_api_connect(params=params)
# Accesses the new keywords from the newly generated keyword list from the API
for keys, values in content['concepts'].items():
new_keyword = values['surfaceForms'][0]['string']
def aylien_api_connect(params, app_id, app_key, api_version):
# Define the API endpoint you would like to use - in this case "concepts".
endpoint = "concepts"
headers = {
'Accept': 'application/json',
'Content-type': 'application/x-www-form-urlencoded',
'X-AYLIEN-TextAPI-Application-ID': app_id,
'X-AYLIEN-TextAPI-Application-Key': app_key,
'User-Agent': "Aylien Text API Python " + api_version
}
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:
try:
# 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.
def genderize_api_connect(first_name):
url = 'https://api.genderize.io/?name={}'.format(first_name)
response = requests.get(url)
content = json.loads(response.content)
return content
def aylien_api_connect(params, app_id, app_key, api_version):
# Define the API endpoint you would like to use - in this case "concepts".
endpoint = "concepts"
headers = {
'Accept': 'application/json',
'Content-type': 'application/x-www-form-urlencoded',
'X-AYLIEN-TextAPI-Application-ID': app_id,
'X-AYLIEN-TextAPI-Application-Key': app_key,
'User-Agent': "Aylien Text API Python " + api_version
}
def genderize_api_connect(first_name):
url = 'https://api.genderize.io/?name={}'.format(first_name)
response = requests.get(url)
try:
assert response.status_code == 200
content = json.loads(response.content)
except AssertionError:
self.logger.error("There was an error contacting the API service")
return content