Skip to content

Commit d1dde32

Browse files
authored
Merge pull request #305 from aprilwebster/master
User can choose whether to maintain tone history for each utterance
2 parents 6efa8bb + 4e25f2f commit d1dde32

2 files changed

Lines changed: 43 additions & 32 deletions

File tree

examples/conversation_tone_analyzer_integration/tone_conversation_integration.v1.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ var tone_analyzer = new watson.ToneAnalyzerV3({
5353
version_date: '2016-05-19'
5454
});
5555

56+
/**
57+
* This example stores tone for each user utterance in conversation context.
58+
* Change this to false, if you do not want to maintain history
59+
*/
60+
var maintainToneHistoryInContext = true;
5661

5762
/**
5863
* Payload for the Watson Conversation Service
@@ -75,11 +80,11 @@ var payload = {
7580
* Note: as indicated below, the console.log statements can be replaced with application-specific code to process
7681
* the err or data object returned by the Conversation Service.
7782
*/
78-
function invokeToneConversation(payload)
83+
function invokeToneConversation(payload,maintainToneHistoryInContext)
7984
{
8085
tone_detection.invokeToneAsync(payload,tone_analyzer)
8186
.then( (tone) => {
82-
tone_detection.updateUserTone(payload, tone);
87+
tone_detection.updateUserTone(payload, tone, maintainToneHistoryInContext);
8388
conversation.message(payload, function(err, data) {
8489
if (err) {
8590
// APPLICATION-SPECIFIC CODE TO PROCESS THE ERROR
@@ -98,4 +103,4 @@ function invokeToneConversation(payload)
98103
})
99104
}
100105

101-
invokeToneConversation(payload);
106+
invokeToneConversation(payload,maintainToneHistoryInContext);

examples/conversation_tone_analyzer_integration/tone_detection.js

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ function invokeToneAsync(conversationPayload, tone_analyzer) {
7676
* @param toneAnalyzerPayload json object returned by the Watson Tone Analyzer Service
7777
* @returns conversationPayload where the user object has been updated with tone information from the toneAnalyzerPayload
7878
*/
79-
function updateUserTone (conversationPayload, toneAnalyzerPayload) {
79+
function updateUserTone (conversationPayload, toneAnalyzerPayload, maintainHistory) {
8080

8181
var emotionTone = null;
8282
var languageTone = null;
@@ -87,7 +87,7 @@ function updateUserTone (conversationPayload, toneAnalyzerPayload) {
8787
}
8888

8989
if(typeof conversationPayload.context.user === 'undefined'){
90-
conversationPayload.context = initUser();
90+
conversationPayload.context = initUser();
9191
}
9292

9393
// For convenience sake, define a variable for the user object
@@ -108,9 +108,9 @@ function updateUserTone (conversationPayload, toneAnalyzerPayload) {
108108
}
109109
});
110110

111-
updateEmotionTone(user, emotionTone);
112-
updateLanguageTone(user, languageTone);
113-
updateSocialTone(user, socialTone);
111+
updateEmotionTone(user, emotionTone, maintainHistory);
112+
updateLanguageTone(user, languageTone, maintainHistory);
113+
updateSocialTone(user, socialTone, maintainHistory);
114114

115115
}
116116

@@ -130,16 +130,13 @@ function initUser() {
130130
'user': {
131131
'tone': {
132132
'emotion': {
133-
'current': null,
134-
'history': []
133+
'current': null
135134
},
136135
'language': {
137-
'current': null,
138-
'history': []
136+
'current': null
139137
},
140138
'social': {
141-
'current': null,
142-
'history': []
139+
'current': null
143140
}
144141
}
145142
}
@@ -152,7 +149,7 @@ function initUser() {
152149
* @param user a json object representing user information (tone) to be used in conversing with the Conversation Service
153150
* @param emotionTone a json object containing the emotion tones in the payload returned by the Tone Analyzer
154151
*/
155-
function updateEmotionTone(user, emotionTone) {
152+
function updateEmotionTone(user, emotionTone, maintainHistory) {
156153

157154
var maxScore = 0.0;
158155
var primaryEmotion = null;
@@ -172,17 +169,20 @@ function updateEmotionTone(user, emotionTone) {
172169
}
173170

174171

175-
if (typeof user.tone.emotion.history === 'undefined') {
176-
user.tone.emotion.history = [];
177-
}
178172

179173
// update user emotion tone
180174
user.tone.emotion.current = primaryEmotion;
181175

182-
user.tone.emotion.history.push({
183-
'tone_name': primaryEmotion,
184-
'score': primaryEmotionScore
185-
});
176+
if(maintainHistory)
177+
{
178+
if (typeof user.tone.emotion.history === 'undefined') {
179+
user.tone.emotion.history = [];
180+
}
181+
user.tone.emotion.history.push({
182+
'tone_name': primaryEmotion,
183+
'score': primaryEmotionScore
184+
});
185+
}
186186
}
187187

188188

@@ -191,7 +191,7 @@ function updateEmotionTone(user, emotionTone) {
191191
* @param user a json object representing user information (tone) to be used in conversing with the Conversation Service
192192
* @param languageTone a json object containing the language tones in the payload returned by the Tone Analyzer
193193
*/
194-
function updateLanguageTone(user, languageTone) {
194+
function updateLanguageTone(user, languageTone, maintainHistory) {
195195

196196
var currentLanguage = [];
197197
var currentLanguageObject = [];
@@ -222,13 +222,16 @@ function updateLanguageTone(user, languageTone) {
222222
}
223223
});
224224

225-
if (typeof user.tone.language.history === 'undefined') {
226-
user.tone.language.history = [];
227-
}
228225

229226
// update user language tone
230227
user.tone.language.current = currentLanguage;
231-
user.tone.language.history.push(currentLanguageObject);
228+
if(maintainHistory)
229+
{
230+
if (typeof user.tone.language.history === 'undefined') {
231+
user.tone.language.history = [];
232+
}
233+
user.tone.language.history.push(currentLanguageObject);
234+
}
232235
}
233236

234237

@@ -237,7 +240,7 @@ function updateLanguageTone(user, languageTone) {
237240
* @param user a json object representing user information (tone) to be used in conversing with the Conversation Service
238241
* @param socialTone a json object containing the social tones in the payload returned by the Tone Analyzer
239242
*/
240-
function updateSocialTone(user, socialTone) {
243+
function updateSocialTone(user, socialTone, maintainHistory) {
241244

242245
var currentSocial = [];
243246
var currentSocialObject = [];
@@ -269,12 +272,15 @@ function updateSocialTone(user, socialTone) {
269272
}
270273
});
271274

272-
if (typeof user.tone.social.history === 'undefined') {
273-
user.tone.social.history = [];
274-
}
275275

276276
// update user social tone
277277
user.tone.social.current = currentSocial;
278-
user.tone.social.history.push(currentSocialObject);
278+
if(maintainHistory)
279+
{
280+
if (typeof user.tone.social.history === 'undefined') {
281+
user.tone.social.history = [];
282+
}
283+
user.tone.social.history.push(currentSocialObject);
284+
}
279285
}
280286

0 commit comments

Comments
 (0)