@@ -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