Last active
May 7, 2016 13:53
-
-
Save devStepsize/046f082e29798d5f0bb5a30454b193fb to your computer and use it in GitHub Desktop.
Extract entities from text using the AlchemyAPI
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
import requests | |
entities_endpoint = "http://gateway-a.watsonplatform.net/calls/text/TextGetRankedNamedEntities" | |
def get_entities(text): | |
data = { | |
'apikey': API_KEY, | |
'outputMode': 'json', | |
'text': text | |
} | |
headers = {'content-type': 'application/x-www-form-urlencoded'} | |
r = requests.post(entities_endpoint, data=data, headers=headers) | |
if r.status_code == 200: | |
resp = r.json() | |
# Print entities, type and relevance score. | |
# Entities are already ranked by relevance, high -> low | |
entities = [e['text'] for e in resp.get('entities')] | |
# Print top 3 most relevant | |
return entities |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment