-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwatson_api.py
23 lines (21 loc) · 886 Bytes
/
watson_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import json
import requests
username= "b5a2fef5-4737-4e0d-b8eb-fc74816af24d"
password= "23YY3My8YbYN"
def get_entities_and_concepts(text):
params = {
('version', '2017-02-27'),
('text', text),
('features', 'sentiment,entities,concepts'),
('keywords.sentiment', 'true'),
}
auth = (username, password)
r = requests.get("https://gateway.watsonplatform.net/natural-language-understanding/api/v1/analyze", params=params, auth=auth)
print("PARAMS:", params)
print(r, r.text)
t = json.loads(r.text)
concepts = sorted(t['concepts'], key=lambda x: x['relevance'], reverse=True)
entities = sorted(t['entities'], key=lambda x: x['relevance'], reverse=True)
#entities = [e['text'] for e in entities]
#concepts = [c['text'] for c in concepts]
return entities[:min(6, len(entities))], concepts[:min(6, len(concepts))]