2121using IBM . Watson . DeveloperCloud . Logging ;
2222using System ;
2323using System . Collections . Generic ;
24+ using System . Collections ;
25+ using IBM . Watson . DeveloperCloud . DataTypes ;
26+ using System . Reflection ;
2427
2528public class ExampleConversation : MonoBehaviour
2629{
@@ -34,125 +37,146 @@ void Start()
3437 LogSystem . InstallDefaultReactors ( ) ;
3538 m_WorkspaceID = Config . Instance . GetVariableValue ( "ConversationV1_ID" ) ;
3639
37- Debug . Log ( "**********User: Hello!" ) ;
38- MessageWithOnlyInput ( "Hello!" ) ;
39-
40- //GetRawOutput("Hello");
41- }
40+ //Debug.Log("**********User: Hello!");
41+ // MessageWithOnlyInput("Hello!");
4242
43+ GetRawOutput ( "Hello" ) ;
44+ }
45+
4346 private void GetRawOutput ( string input )
4447 {
4548 m_Conversation . Message ( OnGetRawOutput , m_WorkspaceID , input ) ;
4649 }
4750
48- private void OnGetRawOutput ( MessageResponse resp , string customData )
51+ private void OnGetRawOutput ( object resp , string customData )
4952 {
5053 if ( ! string . IsNullOrEmpty ( customData ) )
5154 Debug . Log ( customData ) ;
5255 else
5356 Debug . Log ( "No raw data was received." ) ;
54- }
55-
56- private void MessageWithOnlyInput ( string input )
57- {
58- if ( string . IsNullOrEmpty ( input ) )
59- throw new ArgumentNullException ( "input" ) ;
60-
61- m_Conversation . Message ( OnMessageWithOnlyInput , m_WorkspaceID , input ) ;
62- }
63-
64-
65- private void OnMessageWithOnlyInput ( MessageResponse resp , string customData )
66- {
67- if ( resp != null )
68- {
69- foreach ( Intent mi in resp . intents )
70- Debug . Log ( "Message Only intent: " + mi . intent + ", confidence: " + mi . confidence ) ;
71-
72- if ( resp . output != null && resp . output . text . Length > 0 )
73- foreach ( string txt in resp . output . text )
74- Debug . Log ( "Message Only output: " + txt ) ;
75-
76- if ( resp . context != null )
77- {
78- if ( ! string . IsNullOrEmpty ( resp . context . conversation_id ) )
79- Log . Debug ( "ExampleConversation" , "Conversation ID: {0}" , resp . context . conversation_id ) ;
80- else
81- Log . Debug ( "ExampleConversation" , "Conversation ID is null." ) ;
82-
83- if ( resp . context . system != null )
84- {
85- Log . Debug ( "ExampleConversation" , "dialog_request_counter: {0}" , resp . context . system . dialog_request_counter ) ;
86- Log . Debug ( "ExampleConversation" , "dialog_turn_counter: {0}" , resp . context . system . dialog_turn_counter ) ;
87-
88- if ( resp . context . system . dialog_stack != null )
89- {
90- foreach ( Dictionary < string , string > dialogNode in resp . context . system . dialog_stack )
91- foreach ( KeyValuePair < string , string > node in dialogNode )
92- Log . Debug ( "ExampleConversation" , "dialogNode: {0}" , node . Value ) ;
93- }
94- else
95- {
96- Log . Debug ( "ExampleConversation" , "dialog stack is null" ) ;
97- }
9857
99- }
100- else
58+ if ( resp != null )
10159 {
102- Log . Debug ( "ExampleConversation" , "system is null." ) ;
103- }
60+ Dictionary < string , object > respDict = resp as Dictionary < string , object > ;
61+ object intents ;
62+ respDict . TryGetValue ( "intents" , out intents ) ;
10463
105- }
106- else
107- {
108- Log . Debug ( "ExampleConversation" , "Context is null" ) ;
109- }
64+ foreach ( var intentObj in ( intents as List < object > ) )
65+ {
66+ Dictionary < string , object > intentDict = intentObj as Dictionary < string , object > ;
11067
111- string questionStr = questionArray [ UnityEngine . Random . Range ( 0 , questionArray . Length - 1 ) ] ;
112- Debug . Log ( string . Format ( "**********User: {0} ", questionStr ) ) ;
68+ object intentString ;
69+ intentDict . TryGetValue ( "intent ", out intentString ) ;
11370
114- MessageRequest messageRequest = new MessageRequest ( ) ;
115- messageRequest . InputText = questionStr ;
116- messageRequest . alternate_intents = m_UseAlternateIntents ;
117- messageRequest . ContextData = resp . context ;
71+ object confidenceString ;
72+ intentDict . TryGetValue ( "confidence" , out confidenceString ) ;
11873
119- MessageWithFullMessageRequest ( messageRequest ) ;
120- }
121- else
122- {
123- Debug . Log ( "Message Only: Failed to invoke Message();" ) ;
74+ Log . Debug ( "ExampleConversation" , "intent: {0} | confidence {1}" , intentString . ToString ( ) , confidenceString . ToString ( ) ) ;
75+ }
76+ }
12477 }
125- }
12678
127- private void MessageWithFullMessageRequest ( MessageRequest messageRequest )
128- {
129- if ( messageRequest == null )
130- throw new ArgumentNullException ( "messageRequest" ) ;
131- m_Conversation . Message ( OnMessageWithFullRequest , m_WorkspaceID , messageRequest ) ;
132- }
79+ //private void MessageWithOnlyInput(string input)
80+ //{
81+ // if (string.IsNullOrEmpty(input))
82+ // throw new ArgumentNullException("input");
83+
84+ // m_Conversation.Message(OnMessageWithOnlyInput, m_WorkspaceID, input);
85+ //}
86+
87+
88+ //private void OnMessageWithOnlyInput(object resp, string customData)
89+ //{
90+ // if (resp != null)
91+ // {
92+ // foreach (Intent mi in resp.intents)
93+ // Debug.Log("Message Only intent: " + mi.intent + ", confidence: " + mi.confidence);
94+
95+ // if (resp.output != null && resp.output.text.Length > 0)
96+ // foreach (string txt in resp.output.text)
97+ // Debug.Log("Message Only output: " + txt);
98+
99+ // if (resp.context != null)
100+ // {
101+ // if (!string.IsNullOrEmpty(resp.context.conversation_id))
102+ // Log.Debug("ExampleConversation", "Conversation ID: {0}", resp.context.conversation_id);
103+ // else
104+ // Log.Debug("ExampleConversation", "Conversation ID is null.");
105+
106+ // if (resp.context.system != null)
107+ // {
108+ // Log.Debug("ExampleConversation", "dialog_request_counter: {0}", resp.context.system.dialog_request_counter);
109+ // Log.Debug("ExampleConversation", "dialog_turn_counter: {0}", resp.context.system.dialog_turn_counter);
110+
111+ // if (resp.context.system.dialog_stack != null)
112+ // {
113+ // foreach (Dictionary<string, string> dialogNode in resp.context.system.dialog_stack)
114+ // foreach(KeyValuePair<string, string> node in dialogNode)
115+ // Log.Debug("ExampleConversation", "dialogNode: {0}", node.Value);
116+ // }
117+ // else
118+ // {
119+ // Log.Debug("ExampleConversation", "dialog stack is null");
120+ // }
121+
122+ // }
123+ // else
124+ // {
125+ // Log.Debug("ExampleConversation", "system is null.");
126+ // }
127+
128+ // }
129+ // else
130+ // {
131+ // Log.Debug("ExampleConversation", "Context is null");
132+ // }
133+
134+ // string questionStr = questionArray[UnityEngine.Random.Range(0, questionArray.Length - 1)];
135+ // Debug.Log(string.Format("**********User: {0}", questionStr));
136+
137+ // MessageRequest messageRequest = new MessageRequest();
138+ // messageRequest.InputText = questionStr;
139+ // messageRequest.alternate_intents = m_UseAlternateIntents;
140+ // messageRequest.ContextData = resp.context;
141+
142+ // MessageWithFullMessageRequest(messageRequest);
143+ // }
144+ // else
145+ // {
146+ // Debug.Log("Message Only: Failed to invoke Message();");
147+ // }
148+ //}
149+
150+ //private void MessageWithFullMessageRequest(MessageRequest messageRequest)
151+ //{
152+ // if (messageRequest == null)
153+ // throw new ArgumentNullException("messageRequest");
154+ // m_Conversation.Message(OnMessageWithFullRequest, m_WorkspaceID, messageRequest);
155+ //}
156+
157+ //private void OnMessageWithFullRequest(MessageResponse resp, string customData)
158+ //{
159+ // if (resp != null)
160+ // {
161+ // foreach (Intent mi in resp.intents)
162+ // Debug.Log("Full Request intent: " + mi.intent + ", confidence: " + mi.confidence);
163+
164+ // if (resp.output != null && resp.output.text.Length > 0)
165+ // foreach (string txt in resp.output.text)
166+ // Debug.Log("Full Request output: " + txt);
167+
168+ // string questionStr = questionArray[UnityEngine.Random.Range(0, questionArray.Length - 1)];
169+ // Debug.Log(string.Format("**********User: {0}", questionStr));
170+
171+ // MessageRequest messageRequest = new MessageRequest();
172+ // messageRequest.InputText = questionStr;
173+ // messageRequest.alternate_intents = m_UseAlternateIntents;
174+ // messageRequest.ContextData = resp.context;
175+ // }
176+ // else
177+ // {
178+ // Debug.Log("Full Request: Failed to invoke Message();");
179+ // }
180+ //}
133181
134- private void OnMessageWithFullRequest ( MessageResponse resp , string customData )
135- {
136- if ( resp != null )
137- {
138- foreach ( Intent mi in resp . intents )
139- Debug . Log ( "Full Request intent: " + mi . intent + ", confidence: " + mi . confidence ) ;
140-
141- if ( resp . output != null && resp . output . text . Length > 0 )
142- foreach ( string txt in resp . output . text )
143- Debug . Log ( "Full Request output: " + txt ) ;
144-
145- string questionStr = questionArray [ UnityEngine . Random . Range ( 0 , questionArray . Length - 1 ) ] ;
146- Debug . Log ( string . Format ( "**********User: {0}" , questionStr ) ) ;
147-
148- MessageRequest messageRequest = new MessageRequest ( ) ;
149- messageRequest . InputText = questionStr ;
150- messageRequest . alternate_intents = m_UseAlternateIntents ;
151- messageRequest . ContextData = resp . context ;
152- }
153- else
154- {
155- Debug . Log ( "Full Request: Failed to invoke Message();" ) ;
156- }
157- }
158182}
0 commit comments