Skip to content

Commit 64f1784

Browse files
committed
Changed language to writing as the output labels from the Tone Analyzer have changed. Fixed lookup of keys in a json object in a few locations. Converted null to None in a few locations.
1 parent 4bdfd84 commit 64f1784

File tree

1 file changed

+32
-32
lines changed

1 file changed

+32
-32
lines changed

examples/conversation_tone_analyzer_integration/tone_detection.py

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323
* These thresholds can be adjusted to client/domain requirements.
2424
"""
2525
PRIMARY_EMOTION_SCORE_THRESHOLD = 0.5
26-
LANGUAGE_HIGH_SCORE_THRESHOLD = 0.75
27-
LANGUAGE_NO_SCORE_THRESHOLD = 0.0
26+
WRITING_HIGH_SCORE_THRESHOLD = 0.75
27+
WRITING_NO_SCORE_THRESHOLD = 0.0
2828
SOCIAL_HIGH_SCORE_THRESHOLD = 0.75
2929
SOCIAL_LOW_SCORE_THRESHOLD = 0.25
3030

3131
# Labels for the tone categories returned by the Watson Tone Analyzer
3232
EMOTION_TONE_LABEL = 'emotion_tone'
33-
LANGUAGE_TONE_LABEL = 'language_tone'
33+
WRITING_TONE_LABEL = 'writing_tone'
3434
SOCIAL_TONE_LABEL = 'social_tone'
3535

3636
'''
@@ -44,15 +44,15 @@
4444
def updateUserTone (conversationPayload, toneAnalyzerPayload, maintainHistory):
4545

4646
emotionTone = None
47-
languageTone = None
47+
writingTone = None
4848
socialTone = None
4949

5050
# if there is no context in a
51-
if conversationPayload.context == None:
52-
conversationPayload.context = {};
51+
if 'context' not in conversationPayload:
52+
conversationPayload['context'] = {};
5353

54-
if conversationPayload.context.user == None:
55-
conversationPayload.context = initUser()
54+
if 'user' not in conversationPayload['context']:
55+
conversationPayload['context'] = initUser()
5656

5757
# For convenience sake, define a variable for the user object
5858
user = conversationPayload['context']['user'];
@@ -62,13 +62,13 @@ def updateUserTone (conversationPayload, toneAnalyzerPayload, maintainHistory):
6262
for toneCategory in toneAnalyzerPayload['document_tone']['tone_categories']:
6363
if toneCategory['category_id'] == EMOTION_TONE_LABEL:
6464
emotionTone = toneCategory
65-
if toneCategory['category_id'] == LANGUAGE_TONE_LABEL:
66-
languageTone = toneCategory
65+
if toneCategory['category_id'] == WRITING_TONE_LABEL:
66+
writingTone = toneCategory
6767
if toneCategory['category_id'] == SOCIAL_TONE_LABEL:
6868
socialTone = toneCategory
6969

7070
updateEmotionTone(user, emotionTone, maintainHistory)
71-
updateLanguageTone(user, languageTone, maintainHistory)
71+
updateWritingTone(user, writingTone, maintainHistory)
7272
updateSocialTone(user, socialTone, maintainHistory)
7373

7474
conversationPayload['context']['user'] = user
@@ -77,7 +77,7 @@ def updateUserTone (conversationPayload, toneAnalyzerPayload, maintainHistory):
7777

7878
'''
7979
initToneContext initializes a user object containing tone data (from the Watson Tone Analyzer)
80-
@returns user json object with the emotion, language and social tones. The current
80+
@returns user json object with the emotion, writing and social tones. The current
8181
tone identifies the tone for a specific conversation turn, and the history provides the conversation for
8282
all tones up to the current tone for a conversation instance with a user.
8383
'''
@@ -86,13 +86,13 @@ def initUser():
8686
'user': {
8787
'tone': {
8888
'emotion': {
89-
'current': null
89+
'current': None
9090
},
91-
'language': {
92-
'current': null
91+
'writing': {
92+
'current': None
9393
},
9494
'social': {
95-
'current': null
95+
'current': None
9696
}
9797
}
9898
}
@@ -124,7 +124,7 @@ def updateEmotionTone(user, emotionTone, maintainHistory):
124124
user['tone']['emotion']['current'] = primaryEmotion;
125125

126126
if maintainHistory:
127-
if not user['tone']['emotion']['history']:
127+
if 'history' not in user['tone']['emotion']:
128128
user['tone']['emotion']['history'] = []
129129
user['tone']['emotion']['history'].append({
130130
'tone_name': primaryEmotion,
@@ -136,39 +136,39 @@ def updateEmotionTone(user, emotionTone, maintainHistory):
136136
@param: user a json object representing user information (tone) to be used in conversing with the Conversation Service
137137
@param: languageTone a json object containing the language tones in the payload returned by the Tone Analyzer
138138
'''
139-
def updateLanguageTone (user, languageTone, maintainHistory):
139+
def updateWritingTone (user, writingTone, maintainHistory):
140140

141-
currentLanguage = [];
142-
currentLanguageObject = [];
141+
currentWriting = [];
142+
currentWritingObject = [];
143143

144144
# Process each language tone and determine if it is high or low
145-
for tone in languageTone['tones']:
146-
if tone['score'] >= LANGUAGE_HIGH_SCORE_THRESHOLD:
147-
currentLanguage.append(tone['tone_name'].lower() + '_high')
148-
currentLanguageObject.append({
145+
for tone in writingTone['tones']:
146+
if tone['score'] >= WRITING_HIGH_SCORE_THRESHOLD:
147+
currentWriting.append(tone['tone_name'].lower() + '_high')
148+
currentWritingObject.append({
149149
'tone_name': tone['tone_name'].lower(),
150150
'score': tone['score'],
151151
'interpretation': 'likely high'
152152
})
153-
elif tone['score'] <= LANGUAGE_NO_SCORE_THRESHOLD:
154-
currentLanguageObject.append({
153+
elif tone['score'] <= WRITING_NO_SCORE_THRESHOLD:
154+
currentWritingObject.append({
155155
'tone_name': tone['tone_name'].lower(),
156156
'score': tone['score'],
157157
'interpretation': 'no evidence'
158158
})
159159
else:
160-
currentLanguageObject.append({
160+
currentWritingObject.append({
161161
'tone_name': tone['tone_name'].lower(),
162162
'score': tone['score'],
163163
'interpretation': 'likely medium'
164164
})
165165

166-
# update user language tone
167-
user['tone']['language']['current'] = currentLanguage
166+
# update user writing tone
167+
user['tone']['writing']['current'] = currentWriting
168168
if maintainHistory:
169-
if not user['tone']['language']['history']:
170-
user['tone']['language']['history'] = []
171-
user['tone']['language']['history'].append(currentLanguageObject) #TODO - is this the correct location??? AW
169+
if 'history' not in user['tone']['writing']:
170+
user['tone']['writing']['history'] = []
171+
user['tone']['writing']['history'].append(currentWritingObject) #TODO - is this the correct location??? AW
172172

173173
'''
174174
updateSocialTone updates the user with the social tones interpreted based on the specified thresholds

0 commit comments

Comments
 (0)