1818
1919var  Promise  =  require ( 'bluebird' ) ; 
2020
21- /** 
22-  * Thresholds for identifying meaningful tones returned by the Watson Tone Analyzer.  Current values are 
23-  * based on the recommendations made by the Watson Tone Analyzer at 
24-  * https://www.ibm.com/watson/developercloud/doc/tone-analyzer/understanding-tone.shtml 
25-  * These thresholds can be adjusted to client/domain requirements. 
26-  */ 
27- var  PRIMARY_EMOTION_SCORE_THRESHOLD  =  0.5 ; 
28- var  LANGUAGE_HIGH_SCORE_THRESHOLD  =  0.75 ; 
29- var  LANGUAGE_NO_SCORE_THRESHOLD  =  0.0 ; 
30- var  SOCIAL_HIGH_SCORE_THRESHOLD  =  0.75 ; 
31- var  SOCIAL_LOW_SCORE_THRESHOLD  =  0.25 ; 
32- 
33- /** 
34-  * Labels for the tone categories returned by the Watson Tone Analyzer 
35-  */ 
36- var  EMOTION_TONE_LABEL  =  'emotion_tone' ; 
37- var  LANGUAGE_TONE_LABEL  =  'language_tone' ; 
38- var  SOCIAL_TONE_LABEL  =  'social_tone' ; 
39- 
4021/** 
4122 * Public functions for this module 
4223 */ 
@@ -65,18 +46,14 @@ function invokeToneAsync(conversationPayload, tone_analyzer) {
6546} 
6647
6748/** 
68-  * updateUserTone processes the Tone Analyzer payload to pull out  the emotion, language and social  
69-  * tones, and identify the meaningful tones  (i.e., those tones that meet  the specified thresholds).  
70-  * The conversationPayload json object is updated  to include these tones. 
49+  * updateUserTone processes the Tone Analyzer payload to identify  the most significant tone  
50+  * (i.e., the tone with  the largest score). The conversationPayload json object is updated  
51+  * to include these tones. 
7152 * @param  conversationPayload json object returned by the Watson Conversation Service 
7253 * @param  toneAnalyzerPayload json object returned by the Watson Tone Analyzer Service 
7354 * @return  conversationPayload where the user object has been updated with tone information from the toneAnalyzerPayload 
7455 */ 
7556function  updateUserTone ( conversationPayload ,  toneAnalyzerPayload ,  maintainHistory )  { 
76-   var  emotionTone  =  null ; 
77-   var  languageTone  =  null ; 
78-   var  socialTone  =  null ; 
79- 
8057  if  ( typeof  conversationPayload . context  ===  'undefined' )  { 
8158    conversationPayload . context  =  { } ; 
8259  } 
@@ -88,23 +65,9 @@ function updateUserTone(conversationPayload, toneAnalyzerPayload, maintainHistor
8865  // For convenience sake, define a variable for the user object 
8966  var  user  =  conversationPayload . context . user ; 
9067
91-   // Extract the tones - emotion, language and social  
68+   // Extract the document-level tones  
9269  if  ( toneAnalyzerPayload  &&  toneAnalyzerPayload . document_tone )  { 
93-     toneAnalyzerPayload . document_tone . tone_categories . forEach ( function ( toneCategory )  { 
94-       if  ( toneCategory . category_id  ===  EMOTION_TONE_LABEL )  { 
95-         emotionTone  =  toneCategory ; 
96-       } 
97-       if  ( toneCategory . category_id  ===  LANGUAGE_TONE_LABEL )  { 
98-         languageTone  =  toneCategory ; 
99-       } 
100-       if  ( toneCategory . category_id  ===  SOCIAL_TONE_LABEL )  { 
101-         socialTone  =  toneCategory ; 
102-       } 
103-     } ) ; 
104- 
105-     updateEmotionTone ( user ,  emotionTone ,  maintainHistory ) ; 
106-     updateLanguageTone ( user ,  languageTone ,  maintainHistory ) ; 
107-     updateSocialTone ( user ,  socialTone ,  maintainHistory ) ; 
70+     updateTone ( user ,  toneAnalyzerPayload . document_tone . tones ,  maintainHistory ) ; 
10871  } 
10972
11073  conversationPayload . context . user  =  user ; 
@@ -114,149 +77,48 @@ function updateUserTone(conversationPayload, toneAnalyzerPayload, maintainHistor
11477
11578/** 
11679 * initToneContext initializes a user object containing tone data (from the Watson Tone Analyzer) 
117-  * @return  user json object with the emotion, language and social tones.  The current  
118-  * tone identifies  the tone for a specific  conversation turn, and the history provides  the conversation  for 
119-  * all tones up to the current tone for a conversation  instance with a user. 
80+  * @return  user json object. The current tone identifies the tone for a specific conversation turn,  
81+  * and  the history provides the  conversation for all tones up to  the current tone  for a conversation  
82+  * instance with a user. 
12083 */ 
12184function  initUser ( )  { 
12285  return  { 
12386    user : { 
12487      tone : { 
125-         emotion : { 
126-           current : null 
127-         } , 
128-         language : { 
129-           current : null 
130-         } , 
131-         social : { 
132-           current : null 
133-         } 
88+         current : null 
13489      } 
13590    } 
13691  } ; 
13792} 
13893
13994/** 
140-  * updateEmotionTone updates the user emotion tone with the primary emotion - the emotion tone that has 
141-  * a score greater than or equal to the EMOTION_SCORE_THRESHOLD; otherwise primary emotion will be 'neutral' 
95+  * updateTone updates the user tone with the primary tone - the tone with the largest score 
14296 * @param  user a json object representing user information (tone) to be used in conversing with the Conversation Service 
143-  * @param  emotionTone a json object  containing the emotion  tones in the payload returned by the Tone Analyzer 
97+  * @param  tones an array  containing the document-level  tones in the payload returned by the Tone Analyzer 
14498 */ 
145- function  updateEmotionTone ( user ,  emotionTone ,  maintainHistory )  { 
99+ function  updateTone ( user ,  tones ,  maintainHistory )  { 
146100  var  maxScore  =  0.0 ; 
147-   var  primaryEmotion  =  null ; 
148-   var  primaryEmotionScore  =  null ; 
101+   var  primaryTone  =  null ; 
102+   var  primaryToneScore  =  null ; 
149103
150-   emotionTone . tones . forEach ( function ( tone )  { 
104+   tones . forEach ( function ( tone )  { 
151105    if  ( tone . score  >  maxScore )  { 
152106      maxScore  =  tone . score ; 
153-       primaryEmotion  =  tone . tone_name . toLowerCase ( ) ; 
154-       primaryEmotionScore  =  tone . score ; 
107+       primaryTone  =  tone . tone_name . toLowerCase ( ) ; 
108+       primaryToneScore  =  tone . score ; 
155109    } 
156110  } ) ; 
157111
158-   if  ( maxScore  <=  PRIMARY_EMOTION_SCORE_THRESHOLD )  { 
159-     primaryEmotion  =  'neutral' ; 
160-     primaryEmotionScore  =  null ; 
161-   } 
162- 
163-   // update user emotion tone 
164-   user . tone . emotion . current  =  primaryEmotion ; 
112+   // update user tone 
113+   user . tone . current  =  primaryTone ; 
165114
166115  if  ( maintainHistory )  { 
167-     if  ( typeof  user . tone . emotion . history  ===  'undefined' )  { 
168-       user . tone . emotion . history  =  [ ] ; 
116+     if  ( typeof  user . tone . history  ===  'undefined' )  { 
117+       user . tone . history  =  [ ] ; 
169118    } 
170-     user . tone . emotion . history . push ( { 
171-       tone_name : primaryEmotion , 
172-       score : primaryEmotionScore 
119+     user . tone . history . push ( { 
120+       tone_name : primaryTone , 
121+       score : primaryToneScore 
173122    } ) ; 
174123  } 
175124} 
176- 
177- /** 
178-  * updateLanguageTone updates the user with the language tones interpreted based on the specified thresholds 
179-  * @param  user a json object representing user information (tone) to be used in conversing with the Conversation Service 
180-  * @param  languageTone a json object containing the language tones in the payload returned by the Tone Analyzer 
181-  */ 
182- function  updateLanguageTone ( user ,  languageTone ,  maintainHistory )  { 
183-   var  currentLanguage  =  [ ] ; 
184-   var  currentLanguageObject  =  [ ] ; 
185- 
186-   // Process each language tone and determine if it is high or low 
187-   languageTone . tones . forEach ( function ( tone )  { 
188-     if  ( tone . score  >=  LANGUAGE_HIGH_SCORE_THRESHOLD )  { 
189-       currentLanguage . push ( tone . tone_name . toLowerCase ( )  +  '_high' ) ; 
190-       currentLanguageObject . push ( { 
191-         tone_name : tone . tone_name . toLowerCase ( ) , 
192-         score : tone . score , 
193-         interpretation : 'likely high' 
194-       } ) ; 
195-     }  else  if  ( tone . score  <=  LANGUAGE_NO_SCORE_THRESHOLD )  { 
196-       currentLanguageObject . push ( { 
197-         tone_name : tone . tone_name . toLowerCase ( ) , 
198-         score : tone . score , 
199-         interpretation : 'no evidence' 
200-       } ) ; 
201-     }  else  { 
202-       currentLanguageObject . push ( { 
203-         tone_name : tone . tone_name . toLowerCase ( ) , 
204-         score : tone . score , 
205-         interpretation : 'likely medium' 
206-       } ) ; 
207-     } 
208-   } ) ; 
209- 
210-   // update user language tone 
211-   user . tone . language . current  =  currentLanguage ; 
212-   if  ( maintainHistory )  { 
213-     if  ( typeof  user . tone . language . history  ===  'undefined' )  { 
214-       user . tone . language . history  =  [ ] ; 
215-     } 
216-     user . tone . language . history . push ( currentLanguageObject ) ; 
217-   } 
218- } 
219- 
220- /** 
221-  * updateSocialTone updates the user with the social tones interpreted based on the specified thresholds 
222-  * @param  user a json object representing user information (tone) to be used in conversing with the Conversation Service 
223-  * @param  socialTone a json object containing the social tones in the payload returned by the Tone Analyzer 
224-  */ 
225- function  updateSocialTone ( user ,  socialTone ,  maintainHistory )  { 
226-   var  currentSocial  =  [ ] ; 
227-   var  currentSocialObject  =  [ ] ; 
228- 
229-   // Process each social tone and determine if it is high or low 
230-   socialTone . tones . forEach ( function ( tone )  { 
231-     if  ( tone . score  >=  SOCIAL_HIGH_SCORE_THRESHOLD )  { 
232-       currentSocial . push ( tone . tone_name . toLowerCase ( )  +  '_high' ) ; 
233-       currentSocialObject . push ( { 
234-         tone_name : tone . tone_name . toLowerCase ( ) , 
235-         score : tone . score , 
236-         interpretation : 'likely high' 
237-       } ) ; 
238-     }  else  if  ( tone . score  <=  SOCIAL_LOW_SCORE_THRESHOLD )  { 
239-       currentSocial . push ( tone . tone_name . toLowerCase ( )  +  '_low' ) ; 
240-       currentSocialObject . push ( { 
241-         tone_name : tone . tone_name . toLowerCase ( ) , 
242-         score : tone . score , 
243-         interpretation : 'likely low' 
244-       } ) ; 
245-     }  else  { 
246-       currentSocialObject . push ( { 
247-         tone_name : tone . tone_name . toLowerCase ( ) , 
248-         score : tone . score , 
249-         interpretation : 'likely medium' 
250-       } ) ; 
251-     } 
252-   } ) ; 
253- 
254-   // update user social tone 
255-   user . tone . social . current  =  currentSocial ; 
256-   if  ( maintainHistory )  { 
257-     if  ( typeof  user . tone . social . history  ===  'undefined' )  { 
258-       user . tone . social . history  =  [ ] ; 
259-     } 
260-     user . tone . social . history . push ( currentSocialObject ) ; 
261-   } 
262- } 
0 commit comments