Last active
January 14, 2019 19:43
-
-
Save kassuts/70104ebc54b3b95589c39a3602acbddd to your computer and use it in GitHub Desktop.
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
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 | |
} | |
url = 'https://api.aylien.com/api/v1' + "/" + endpoint | |
response = requests.post(url, headers=headers, params=params) | |
try: | |
assert response.status_code == 200 | |
content = json.loads(response.content) | |
except AssertionError: | |
logger.error("There was an error contacting the API service") | |
return content | |
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'] | |
# Ensure the new keyword has a score above the set threshold in Line 20, | |
# that it does not already exist in the keywords list and finally, | |
# that the number of keywords has not exceeded the limit also set in Line 20. | |
if values['surfaceForms'][0]['score'] > threshold and new_keyword not in all_keywords[key] \ | |
and limit < keyword_limit: | |
# If all conditions satisfied, the keyword is capitalized and added to the dictionary. | |
all_keywords[key].append(new_keyword.capitalize()) | |
# Increment keyword limit counter by one for every new keyword added. | |
limit += 1 | |
return all_keywords |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment