forked from hacknyu2019/flask-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatson.py
34 lines (25 loc) · 1.1 KB
/
watson.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import os
from pprint import pprint
from watson_developer_cloud import NaturalLanguageUnderstandingV1
from watson_developer_cloud.natural_language_understanding_v1 import Features, \
EntitiesOptions, KeywordsOptions, ConceptsOptions, CategoriesOptions
API_KEY = os.environ.get('IBM_API_KEY')
concepts_cache = {}
def get_concepts(text_input):
natural_language_understanding = NaturalLanguageUnderstandingV1(
version="2017-02-27",
iam_apikey=API_KEY)
try:
response = natural_language_understanding.analyze(
text=text_input,
features=Features(concepts=ConceptsOptions(limit=5),
entities=EntitiesOptions(),
# keywords=KeywordsOptions(),
# categories=CategoriesOptions()
)).get_result()
return response['concepts'], response['entities']
except Exception as e:
print("Could not get concepts for this page", text_input)
return None
if __name__ == '__main__':
pprint(get_concepts("Sample Test data sentence "))