-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwatson.py
More file actions
37 lines (26 loc) · 1.06 KB
/
watson.py
File metadata and controls
37 lines (26 loc) · 1.06 KB
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
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
from ibm_watson import ToneAnalyzerV3
from dotenv import load_dotenv
import os
load_dotenv()
api_key = os.environ.get("WATSON_KEY")
watson_url = os.environ.get("WATSON_URL")
def reload_watson_api():
global ta
watson_authenticator = IAMAuthenticator(api_key) # authentication
ta = ToneAnalyzerV3(version='2017-09-21', authenticator=watson_authenticator) # sets up analyzer instance
ta.set_service_url(watson_url)
language_tones = ['analytical', 'confident', 'tentative']
def watson_tone_analysis(message: str) -> dict:
results = ta.tone(message).get_result()
most_confident_score = {'score': 0}
for tone in results['document_tone']['tones']:
if tone['tone_id'] not in language_tones:
# Ignore language indicators
if tone['score'] > most_confident_score['score']:
most_confident_score = tone
if most_confident_score == {'score': 0}:
return None
else:
return most_confident_score
reload_watson_api()