Skip to content

Instantly share code, notes, and snippets.

@devStepsize
Last active May 7, 2016 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devStepsize/046f082e29798d5f0bb5a30454b193fb to your computer and use it in GitHub Desktop.
Save devStepsize/046f082e29798d5f0bb5a30454b193fb to your computer and use it in GitHub Desktop.
Extract entities from text using the AlchemyAPI
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