Skip to content
Permalink
Browse files

Merge pull request #214 from watson-developer-cloud/gh-213-emptyDialo…

…gStack

added dialogNode data model
  • Loading branch information...
mediumTaj committed Jun 27, 2017
2 parents e532dc3 + d5d3499 commit fb81974bb6e1905c42534ca0a1d70046c2b4c871
@@ -20,6 +20,10 @@
using IBM.Watson.DeveloperCloud.Utilities;
using IBM.Watson.DeveloperCloud.Logging;
using System;
using System.Collections.Generic;
using System.Collections;
using IBM.Watson.DeveloperCloud.DataTypes;
using System.Reflection;

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

Debug.Log("**********User: Hello!");
MessageWithOnlyInput("Hello!");
}
//Debug.Log("**********User: Hello!");
// MessageWithOnlyInput("Hello!");

private void MessageWithOnlyInput(string input)
{
if (string.IsNullOrEmpty(input))
throw new ArgumentNullException("input");

m_Conversation.Message(OnMessageWithOnlyInput, m_WorkspaceID, input);
}


private void OnMessageWithOnlyInput(MessageResponse resp, string customData)
{
if (resp != null)
{
foreach (Intent mi in resp.intents)
Debug.Log("Message Only intent: " + mi.intent + ", confidence: " + mi.confidence);

if (resp.output != null && resp.output.text.Length > 0)
foreach (string txt in resp.output.text)
Debug.Log("Message Only output: " + txt);

string questionStr = questionArray[UnityEngine.Random.Range(0, questionArray.Length - 1)];
Debug.Log(string.Format("**********User: {0}", questionStr));

MessageRequest messageRequest = new MessageRequest();
messageRequest.InputText = questionStr;
messageRequest.alternate_intents = m_UseAlternateIntents;
messageRequest.ContextData = resp.context;

MessageWithFullMessageRequest(messageRequest);
GetRawOutput("Hello");
}
else

private void GetRawOutput(string input)
{
Debug.Log("Message Only: Failed to invoke Message();");
m_Conversation.Message(OnGetRawOutput, m_WorkspaceID, input);
}
}

private void MessageWithFullMessageRequest(MessageRequest messageRequest)
{
if (messageRequest == null)
throw new ArgumentNullException("messageRequest");
m_Conversation.Message(OnMessageWithFullRequest, m_WorkspaceID, messageRequest);
}

private void OnMessageWithFullRequest(MessageResponse resp, string customData)
{
if (resp != null)
private void OnGetRawOutput(object resp, string customData)
{
foreach (Intent mi in resp.intents)
Debug.Log("Full Request intent: " + mi.intent + ", confidence: " + mi.confidence);

if (resp.output != null && resp.output.text.Length > 0)
foreach (string txt in resp.output.text)
Debug.Log("Full Request output: " + txt);
if (!string.IsNullOrEmpty(customData))
Debug.Log(customData);
else
Debug.Log("No raw data was received.");

if (resp != null)
{
Dictionary<string, object> respDict = resp as Dictionary<string, object>;
object intents;
respDict.TryGetValue("intents", out intents);

foreach(var intentObj in (intents as List<object>))
{
Dictionary<string, object> intentDict = intentObj as Dictionary<string, object>;

object intentString;
intentDict.TryGetValue("intent", out intentString);

object confidenceString;
intentDict.TryGetValue("confidence", out confidenceString);

Log.Debug("ExampleConversation", "intent: {0} | confidence {1}", intentString.ToString(), confidenceString.ToString());
}
}
}

string questionStr = questionArray[UnityEngine.Random.Range(0, questionArray.Length - 1)];
Debug.Log(string.Format("**********User: {0}", questionStr));
//private void MessageWithOnlyInput(string input)
//{
// if (string.IsNullOrEmpty(input))
// throw new ArgumentNullException("input");

// m_Conversation.Message(OnMessageWithOnlyInput, m_WorkspaceID, input);
//}


//private void OnMessageWithOnlyInput(object resp, string customData)
//{
// if (resp != null)
// {
// foreach (Intent mi in resp.intents)
// Debug.Log("Message Only intent: " + mi.intent + ", confidence: " + mi.confidence);

// if (resp.output != null && resp.output.text.Length > 0)
// foreach (string txt in resp.output.text)
// Debug.Log("Message Only output: " + txt);

// if (resp.context != null)
// {
// if (!string.IsNullOrEmpty(resp.context.conversation_id))
// Log.Debug("ExampleConversation", "Conversation ID: {0}", resp.context.conversation_id);
// else
// Log.Debug("ExampleConversation", "Conversation ID is null.");

// if (resp.context.system != null)
// {
// Log.Debug("ExampleConversation", "dialog_request_counter: {0}", resp.context.system.dialog_request_counter);
// Log.Debug("ExampleConversation", "dialog_turn_counter: {0}", resp.context.system.dialog_turn_counter);

// if (resp.context.system.dialog_stack != null)
// {
// foreach (Dictionary<string, string> dialogNode in resp.context.system.dialog_stack)
// foreach(KeyValuePair<string, string> node in dialogNode)
// Log.Debug("ExampleConversation", "dialogNode: {0}", node.Value);
// }
// else
// {
// Log.Debug("ExampleConversation", "dialog stack is null");
// }

// }
// else
// {
// Log.Debug("ExampleConversation", "system is null.");
// }

// }
// else
// {
// Log.Debug("ExampleConversation", "Context is null");
// }

// string questionStr = questionArray[UnityEngine.Random.Range(0, questionArray.Length - 1)];
// Debug.Log(string.Format("**********User: {0}", questionStr));

// MessageRequest messageRequest = new MessageRequest();
// messageRequest.InputText = questionStr;
// messageRequest.alternate_intents = m_UseAlternateIntents;
// messageRequest.ContextData = resp.context;

// MessageWithFullMessageRequest(messageRequest);
// }
// else
// {
// Debug.Log("Message Only: Failed to invoke Message();");
// }
//}

//private void MessageWithFullMessageRequest(MessageRequest messageRequest)
//{
// if (messageRequest == null)
// throw new ArgumentNullException("messageRequest");
// m_Conversation.Message(OnMessageWithFullRequest, m_WorkspaceID, messageRequest);
//}

//private void OnMessageWithFullRequest(MessageResponse resp, string customData)
//{
// if (resp != null)
// {
// foreach (Intent mi in resp.intents)
// Debug.Log("Full Request intent: " + mi.intent + ", confidence: " + mi.confidence);

// if (resp.output != null && resp.output.text.Length > 0)
// foreach (string txt in resp.output.text)
// Debug.Log("Full Request output: " + txt);

// string questionStr = questionArray[UnityEngine.Random.Range(0, questionArray.Length - 1)];
// Debug.Log(string.Format("**********User: {0}", questionStr));

// MessageRequest messageRequest = new MessageRequest();
// messageRequest.InputText = questionStr;
// messageRequest.alternate_intents = m_UseAlternateIntents;
// messageRequest.ContextData = resp.context;
// }
// else
// {
// Debug.Log("Full Request: Failed to invoke Message();");
// }
//}

MessageRequest messageRequest = new MessageRequest();
messageRequest.InputText = questionStr;
messageRequest.alternate_intents = m_UseAlternateIntents;
messageRequest.ContextData = resp.context;
}
else
{
Debug.Log("Full Request: Failed to invoke Message();");
}
}
}
@@ -452,7 +452,7 @@ GameObject:
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 0
m_IsActive: 1
--- !u!114 &859102723
MonoBehaviour:
m_ObjectHideFlags: 0
@@ -17,12 +17,11 @@

using System;
using System.Text;
using System.Collections.Generic;
using FullSerializer;
using MiniJSON;
using IBM.Watson.DeveloperCloud.Utilities;
using IBM.Watson.DeveloperCloud.Connection;
using IBM.Watson.DeveloperCloud.Logging;
using MiniJSON;

namespace IBM.Watson.DeveloperCloud.Services.Conversation.v1
{
@@ -49,16 +48,17 @@ public class Conversation : IWatsonService
/// The callback delegate for the Message() function.
/// </summary>
/// <param name="resp">The response object to a call to Message().</param>
public delegate void OnMessage(MessageResponse resp, string customData);

/// <summary>
/// Message the specified workspaceId, input and callback.
/// </summary>
/// <param name="workspaceID">Workspace identifier.</param>
/// <param name="input">Input.</param>
/// <param name="callback">Callback.</param>
/// <param name="customData">Custom data.</param>
public bool Message(OnMessage callback, string workspaceID, string input, string customData = default(string))
public delegate void OnMessage(object resp, string customData);
//public delegate void OnMessage(MessageResponse resp, string customData);

/// <summary>
/// Message the specified workspaceId, input and callback.
/// </summary>
/// <param name="workspaceID">Workspace identifier.</param>
/// <param name="input">Input.</param>
/// <param name="callback">Callback.</param>
/// <param name="customData">Custom data.</param>
public bool Message(OnMessage callback, string workspaceID, string input, string customData = default(string))
{
if (string.IsNullOrEmpty(workspaceID))
throw new ArgumentNullException("workspaceId");
@@ -134,32 +134,42 @@ private class MessageReq : RESTConnector.Request
public string Data { get; set; }
}

private void MessageResp(RESTConnector.Request req, RESTConnector.Response resp)
{
MessageResponse response = new MessageResponse();
if (resp.Success)
{
try
{
fsData data = null;
fsResult r = fsJsonParser.Parse(Encoding.UTF8.GetString(resp.Data), out data);
if (!r.Succeeded)
throw new WatsonException(r.FormattedMessages);

object obj = response;
r = sm_Serializer.TryDeserialize(data, obj.GetType(), ref obj);
if (!r.Succeeded)
throw new WatsonException(r.FormattedMessages);
}
catch (Exception e)
private void MessageResp(RESTConnector.Request req, RESTConnector.Response resp)
{
Log.Error("Conversation", "MessageResp Exception: {0}", e.ToString());
resp.Success = false;
}
}

if (((MessageReq)req).Callback != null)
((MessageReq)req).Callback(resp.Success ? response : null, ((MessageReq)req).Data);
object dataObject = null;
string data = "";

//MessageResponse response = new MessageResponse();
//fsData data = null;

if (resp.Success)
{
try
{
// For deserializing into a generic object
data = Encoding.UTF8.GetString(resp.Data);
dataObject = Json.Deserialize(data);

// For deserializing into fixed data model
// fsResult r = fsJsonParser.Parse(Encoding.UTF8.GetString(resp.Data), out data);
// if (!r.Succeeded)
// throw new WatsonException(r.FormattedMessages);

// object obj = response;
// r = sm_Serializer.TryDeserialize(data, obj.GetType(), ref obj);
// if (!r.Succeeded)
// throw new WatsonException(r.FormattedMessages);
}
catch (Exception e)
{
Log.Error("Conversation", "MessageResp Exception: {0}", e.ToString());
data = e.Message;
resp.Success = false;
}
}

if (((MessageReq)req).Callback != null)
((MessageReq)req).Callback(resp.Success ? dataObject : null, !string.IsNullOrEmpty(((MessageReq)req).Data) ? ((MessageReq)req).Data : data.ToString());
}
#endregion

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

private void OnMessage(MessageResponse resp, string customData)
private void OnMessage(object resp, string customData)
{
if (m_ConversationCount > 0)
{
@@ -14,7 +14,9 @@
* limitations under the License.
*
*/

using FullSerializer;
using System.Collections.Generic;

namespace IBM.Watson.DeveloperCloud.Services.Conversation.v1
{
@@ -282,7 +284,7 @@ public class SystemResponse
/// <summary>
/// An array of dialog node IDs that are in focus in the conversation.
/// </summary>
public string[] dialog_stack { get; set; }
public Dictionary<string, string>[] dialog_stack { get; set; }
/// <summary>
/// The number of cycles of user input and response in this conversation.
/// </summary>
@@ -303,7 +305,7 @@ public class Version
/// <summary>
/// The version.
/// </summary>
public const string VERSION = "2016-09-20";
public const string VERSION = "2016-09-20";
}
#endregion
}

0 comments on commit fb81974

Please sign in to comment.
You can’t perform that action at this time.