-
Notifications
You must be signed in to change notification settings - Fork 4
/
Watson_services.py
56 lines (48 loc) · 2.14 KB
/
Watson_services.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import ibm_watson
def watson_nlu(text_test_eta):
"""
IBM WATSON NLU API
:param text_test_eta: String of text
:return: Non stackoverflow tags based on the custom model trained on watson knowledge studio
"""
from ibm_watson import NaturalLanguageUnderstandingV1
from ibm_watson.natural_language_understanding_v1 import Features,KeywordsOptions ,SyntaxOptions, SyntaxOptionsTokens,SentimentOptions,EntitiesOptions
credentials = {"api_key": "wEHseKE5Iqv2PfZwtOP6FVQYOeoJxIa-32Xu5QbNkBRM",
"api_url": "https://gateway.watsonplatform.net/natural-language-understanding/api",
"model":"9ccb4de2-2bf1-410a-8d51-36f77f9cf907"
}
natural_language_understanding = NaturalLanguageUnderstandingV1(
version='2018-11-16',
iam_apikey=credentials['api_key'],
url=credentials['api_url']
)
response_eta = natural_language_understanding.analyze(
text=text_test_eta,
features=Features(entities=EntitiesOptions(limit=1,model=credentials['model']))).get_result()
# print(response_eta['usage'])
return response_eta
def watson_assistantv1(query_eta):
"""
Watson assistant api
:param query_eta: A string from which stackoverflow tags will be mined by the watson Assistant
:return: Returns a list of tags with their relevance factor in a dictionary
"""
credentials_moh = {"api_key": "HTHsjdKJzDGeQlHu_uJijwKbb48__vZbypti5MVchZL7",
"api_url": "https://gateway.watsonplatform.net/assistant/api",
"workspace_id": "f2d82aa9-b6a3-4657-a6eb-01ff1cfec5a8"}
assistant2 = ibm_watson.AssistantV1(
version='2019-02-28',
url=credentials_moh['api_url'],
iam_apikey=credentials_moh['api_key']
)
response2 = assistant2.message(
workspace_id=credentials_moh['workspace_id'],
input = {'text': query_eta}
).get_result()
tags2 = {}
for i in response2['entities']:
if i['entity'] == 'Stack_Overflow_Tag':
if i['confidence'] > 0.8:
tags2[i['value']] = i['confidence']
if len(tags2) >= 1:
return tags2