-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathaction.py
executable file
·37 lines (31 loc) · 1.38 KB
/
action.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
35
36
37
import json
from watson_developer_cloud import NaturalLanguageUnderstandingV1
from watson_developer_cloud.natural_language_understanding_v1 import Features, EntitiesOptions, KeywordsOptions, SentimentOptions
def main(dict):
try:
model_id=""
natural_language_understanding = NaturalLanguageUnderstandingV1(
version='2018-04-05',
iam_apikey="",
url='https://gateway-wdc.watsonplatform.net/natural-language-understanding/api')
resposta = natural_language_understanding.analyze(
text=dict['texto'],
features=Features(
entities=EntitiesOptions(emotion=True, sentiment=True, model=model_id),
sentiment=SentimentOptions()),
).get_result()
retorno = {}
# Salvar as entidades no retorno
if 'entities' in resposta:
for i in range(len(resposta['entities'])):
retorno[resposta['entities'][i]['type']] = resposta['entities'][i]['text']
# Salvar o sentimento no retorno
if 'sentiment' in resposta:
retorno['sentiment'] = resposta['sentiment']['document']['label']
dict['err'] = False
dict['resposta'] = retorno
return dict
except:
dict['err'] = True
dict['resposta'] = "Erro na chamada ao NLU."
return dict