Skip to content

Commit fb81974

Browse files
authored
Merge pull request #214 from watson-developer-cloud/gh-213-emptyDialogStack
added dialogNode data model
2 parents e532dc3 + d5d3499 commit fb81974

File tree

5 files changed

+228
-118
lines changed

5 files changed

+228
-118
lines changed

Examples/ServiceExamples/Scripts/ExampleConversation.cs

Lines changed: 138 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
using IBM.Watson.DeveloperCloud.Utilities;
2121
using IBM.Watson.DeveloperCloud.Logging;
2222
using System;
23+
using System.Collections.Generic;
24+
using System.Collections;
25+
using IBM.Watson.DeveloperCloud.DataTypes;
26+
using System.Reflection;
2327

2428
public class ExampleConversation : MonoBehaviour
2529
{
@@ -33,75 +37,146 @@ void Start()
3337
LogSystem.InstallDefaultReactors();
3438
m_WorkspaceID = Config.Instance.GetVariableValue("ConversationV1_ID");
3539

36-
Debug.Log("**********User: Hello!");
37-
MessageWithOnlyInput("Hello!");
38-
}
40+
//Debug.Log("**********User: Hello!");
41+
// MessageWithOnlyInput("Hello!");
3942

40-
private void MessageWithOnlyInput(string input)
41-
{
42-
if (string.IsNullOrEmpty(input))
43-
throw new ArgumentNullException("input");
44-
45-
m_Conversation.Message(OnMessageWithOnlyInput, m_WorkspaceID, input);
46-
}
47-
48-
49-
private void OnMessageWithOnlyInput(MessageResponse resp, string customData)
50-
{
51-
if (resp != null)
52-
{
53-
foreach (Intent mi in resp.intents)
54-
Debug.Log("Message Only intent: " + mi.intent + ", confidence: " + mi.confidence);
55-
56-
if (resp.output != null && resp.output.text.Length > 0)
57-
foreach (string txt in resp.output.text)
58-
Debug.Log("Message Only output: " + txt);
59-
60-
string questionStr = questionArray[UnityEngine.Random.Range(0, questionArray.Length - 1)];
61-
Debug.Log(string.Format("**********User: {0}", questionStr));
62-
63-
MessageRequest messageRequest = new MessageRequest();
64-
messageRequest.InputText = questionStr;
65-
messageRequest.alternate_intents = m_UseAlternateIntents;
66-
messageRequest.ContextData = resp.context;
67-
68-
MessageWithFullMessageRequest(messageRequest);
43+
GetRawOutput("Hello");
6944
}
70-
else
45+
46+
private void GetRawOutput(string input)
7147
{
72-
Debug.Log("Message Only: Failed to invoke Message();");
48+
m_Conversation.Message(OnGetRawOutput, m_WorkspaceID, input);
7349
}
74-
}
75-
76-
private void MessageWithFullMessageRequest(MessageRequest messageRequest)
77-
{
78-
if (messageRequest == null)
79-
throw new ArgumentNullException("messageRequest");
80-
m_Conversation.Message(OnMessageWithFullRequest, m_WorkspaceID, messageRequest);
81-
}
8250

83-
private void OnMessageWithFullRequest(MessageResponse resp, string customData)
84-
{
85-
if (resp != null)
51+
private void OnGetRawOutput(object resp, string customData)
8652
{
87-
foreach (Intent mi in resp.intents)
88-
Debug.Log("Full Request intent: " + mi.intent + ", confidence: " + mi.confidence);
89-
90-
if (resp.output != null && resp.output.text.Length > 0)
91-
foreach (string txt in resp.output.text)
92-
Debug.Log("Full Request output: " + txt);
53+
if (!string.IsNullOrEmpty(customData))
54+
Debug.Log(customData);
55+
else
56+
Debug.Log("No raw data was received.");
57+
58+
if (resp != null)
59+
{
60+
Dictionary<string, object> respDict = resp as Dictionary<string, object>;
61+
object intents;
62+
respDict.TryGetValue("intents", out intents);
63+
64+
foreach(var intentObj in (intents as List<object>))
65+
{
66+
Dictionary<string, object> intentDict = intentObj as Dictionary<string, object>;
67+
68+
object intentString;
69+
intentDict.TryGetValue("intent", out intentString);
70+
71+
object confidenceString;
72+
intentDict.TryGetValue("confidence", out confidenceString);
73+
74+
Log.Debug("ExampleConversation", "intent: {0} | confidence {1}", intentString.ToString(), confidenceString.ToString());
75+
}
76+
}
77+
}
9378

94-
string questionStr = questionArray[UnityEngine.Random.Range(0, questionArray.Length - 1)];
95-
Debug.Log(string.Format("**********User: {0}", questionStr));
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+
//}
96181

97-
MessageRequest messageRequest = new MessageRequest();
98-
messageRequest.InputText = questionStr;
99-
messageRequest.alternate_intents = m_UseAlternateIntents;
100-
messageRequest.ContextData = resp.context;
101-
}
102-
else
103-
{
104-
Debug.Log("Full Request: Failed to invoke Message();");
105-
}
106-
}
107182
}

Examples/ServiceExamples/ServiceExamples.unity

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ GameObject:
452452
m_Icon: {fileID: 0}
453453
m_NavMeshLayer: 0
454454
m_StaticEditorFlags: 0
455-
m_IsActive: 0
455+
m_IsActive: 1
456456
--- !u!114 &859102723
457457
MonoBehaviour:
458458
m_ObjectHideFlags: 0

Scripts/Services/Conversation/Conversation.cs

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@
1717

1818
using System;
1919
using System.Text;
20-
using System.Collections.Generic;
2120
using FullSerializer;
22-
using MiniJSON;
2321
using IBM.Watson.DeveloperCloud.Utilities;
2422
using IBM.Watson.DeveloperCloud.Connection;
2523
using IBM.Watson.DeveloperCloud.Logging;
24+
using MiniJSON;
2625

2726
namespace IBM.Watson.DeveloperCloud.Services.Conversation.v1
2827
{
@@ -49,16 +48,17 @@ public class Conversation : IWatsonService
4948
/// The callback delegate for the Message() function.
5049
/// </summary>
5150
/// <param name="resp">The response object to a call to Message().</param>
52-
public delegate void OnMessage(MessageResponse resp, string customData);
53-
54-
/// <summary>
55-
/// Message the specified workspaceId, input and callback.
56-
/// </summary>
57-
/// <param name="workspaceID">Workspace identifier.</param>
58-
/// <param name="input">Input.</param>
59-
/// <param name="callback">Callback.</param>
60-
/// <param name="customData">Custom data.</param>
61-
public bool Message(OnMessage callback, string workspaceID, string input, string customData = default(string))
51+
public delegate void OnMessage(object resp, string customData);
52+
//public delegate void OnMessage(MessageResponse resp, string customData);
53+
54+
/// <summary>
55+
/// Message the specified workspaceId, input and callback.
56+
/// </summary>
57+
/// <param name="workspaceID">Workspace identifier.</param>
58+
/// <param name="input">Input.</param>
59+
/// <param name="callback">Callback.</param>
60+
/// <param name="customData">Custom data.</param>
61+
public bool Message(OnMessage callback, string workspaceID, string input, string customData = default(string))
6262
{
6363
if (string.IsNullOrEmpty(workspaceID))
6464
throw new ArgumentNullException("workspaceId");
@@ -134,32 +134,42 @@ private class MessageReq : RESTConnector.Request
134134
public string Data { get; set; }
135135
}
136136

137-
private void MessageResp(RESTConnector.Request req, RESTConnector.Response resp)
138-
{
139-
MessageResponse response = new MessageResponse();
140-
if (resp.Success)
141-
{
142-
try
143-
{
144-
fsData data = null;
145-
fsResult r = fsJsonParser.Parse(Encoding.UTF8.GetString(resp.Data), out data);
146-
if (!r.Succeeded)
147-
throw new WatsonException(r.FormattedMessages);
148-
149-
object obj = response;
150-
r = sm_Serializer.TryDeserialize(data, obj.GetType(), ref obj);
151-
if (!r.Succeeded)
152-
throw new WatsonException(r.FormattedMessages);
153-
}
154-
catch (Exception e)
137+
private void MessageResp(RESTConnector.Request req, RESTConnector.Response resp)
155138
{
156-
Log.Error("Conversation", "MessageResp Exception: {0}", e.ToString());
157-
resp.Success = false;
158-
}
159-
}
160-
161-
if (((MessageReq)req).Callback != null)
162-
((MessageReq)req).Callback(resp.Success ? response : null, ((MessageReq)req).Data);
139+
object dataObject = null;
140+
string data = "";
141+
142+
//MessageResponse response = new MessageResponse();
143+
//fsData data = null;
144+
145+
if (resp.Success)
146+
{
147+
try
148+
{
149+
// For deserializing into a generic object
150+
data = Encoding.UTF8.GetString(resp.Data);
151+
dataObject = Json.Deserialize(data);
152+
153+
// For deserializing into fixed data model
154+
// fsResult r = fsJsonParser.Parse(Encoding.UTF8.GetString(resp.Data), out data);
155+
// if (!r.Succeeded)
156+
// throw new WatsonException(r.FormattedMessages);
157+
158+
// object obj = response;
159+
// r = sm_Serializer.TryDeserialize(data, obj.GetType(), ref obj);
160+
// if (!r.Succeeded)
161+
// throw new WatsonException(r.FormattedMessages);
162+
}
163+
catch (Exception e)
164+
{
165+
Log.Error("Conversation", "MessageResp Exception: {0}", e.ToString());
166+
data = e.Message;
167+
resp.Success = false;
168+
}
169+
}
170+
171+
if (((MessageReq)req).Callback != null)
172+
((MessageReq)req).Callback(resp.Success ? dataObject : null, !string.IsNullOrEmpty(((MessageReq)req).Data) ? ((MessageReq)req).Data : data.ToString());
163173
}
164174
#endregion
165175

@@ -221,7 +231,7 @@ public CheckServiceStatus(Conversation service, ServiceStatus callback)
221231
}
222232
}
223233

224-
private void OnMessage(MessageResponse resp, string customData)
234+
private void OnMessage(object resp, string customData)
225235
{
226236
if (m_ConversationCount > 0)
227237
{

Scripts/Services/Conversation/DataModels.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
* limitations under the License.
1515
*
1616
*/
17+
1718
using FullSerializer;
19+
using System.Collections.Generic;
1820

1921
namespace IBM.Watson.DeveloperCloud.Services.Conversation.v1
2022
{
@@ -282,7 +284,7 @@ public class SystemResponse
282284
/// <summary>
283285
/// An array of dialog node IDs that are in focus in the conversation.
284286
/// </summary>
285-
public string[] dialog_stack { get; set; }
287+
public Dictionary<string, string>[] dialog_stack { get; set; }
286288
/// <summary>
287289
/// The number of cycles of user input and response in this conversation.
288290
/// </summary>
@@ -303,7 +305,7 @@ public class Version
303305
/// <summary>
304306
/// The version.
305307
/// </summary>
306-
public const string VERSION = "2016-09-20";
308+
public const string VERSION = "2016-09-20";
307309
}
308310
#endregion
309311
}

0 commit comments

Comments
 (0)