从Unity中的Watson Conversation获取意图和实体

时间:2018-03-08 15:28:32

标签: c# unity3d ibm-watson watson-conversation watson

我用对话节点的实体,实体创建了一个对话工作区。在Unity中,我与Watson会话,上述会话工作区,语音到文本以及文本到语音连接有联系。 在我的对话工作区和城市实体中存在天气意图。我想要实现的是在Unity中识别用户用实体@city说出#weather的意图并挑选@city字并在应用程序中显示它。

例如:用户说:"柏林的天气是什么?" 然后我希望能够从该句子中提取柏林(实体国家)这个词,并使用Debug.Log在控制台中显示它。

我已经在Unity中设置和工作,我只是不知道如何在C#中调出意图和实体。

我在Watson Cardboard vr示例中找到了以下示例,并尝试在我的应用程序中应用它,但它在我的应用程序中不起作用。

public string city;

void OnMessage(MessageResponse resp, string customData)
{
    if (resp != null && (resp.intents.Length > 0 || resp.entities.Length > 0))
    {
        string intent = resp.intents[0].intent;
        Debug.Log("Intent: " + intent);

        if (intent == "weather")
        {
            //foreach (EntityResponse entity in resp.entities)
            foreach (RuntimeEntity entity in resp.entities)
            {
                Debug.Log("entityType: " + entity.entity + " , value: " + entity.value);
                if (entity.entity == "country")
                {
                    //zet spraak gelijk aan city || Voer actie uit
                    city = entity.entity;
                    Debug.Log("City: " + city);
                }
            }
        }
        else
        {
            Debug.Log("Failed to invoke OnMessage();");
        }
    }
}

有没有办法让它与Unity资源商店的Watson 2.0.1一起使用,或者现在应该以完全不同的方式处理它?<​​/ p>

这是我在1个脚本中组合的不同Watson函数的代码。通过这个脚本,我设置了会话服务,语音到文本等。我​​想尝试在包含此帖子顶部代码的不同脚本中提取意图和实体。

1 个答案:

答案 0 :(得分:1)

如果您没有收到任何类型的回复,请检查您的工作区ID,以确保它能够使用您的意图,实体和对话框正确引用工作区。

以下是我构建的演示示例。我不确定您是如何构建MessageResponse的,但是您需要确保序列化/反序列化,因为您正在使用JSON来处理Watson服务。

private void OnMessage(object resp, Dictionary<string, object> customData)
     {
         fsData fsdata = null;
         fsResult r = _serializer.TrySerialize(resp.GetType(), resp, out fsdata);
         if (!r.Succeeded)
             throw new WatsonException(r.FormattedMessages);

         //  Convert fsdata to MessageResponse
         MessageResponse messageResponse = new MessageResponse();
         object obj = messageResponse;
         r = _serializer.TryDeserialize(fsdata, obj.GetType(), ref obj);
         if (!r.Succeeded)
             throw new WatsonException(r.FormattedMessages);

         //  Set context for next round of messaging
         object _tempContext = null;
         (resp as Dictionary<string, object>).TryGetValue("context", out _tempContext);

         if (_tempContext != null)
             _context = _tempContext as Dictionary<string, object>;
         else
             Log.Debug("ExampleConversation.OnMessage()", "Failed to get context");
         //_waitingForResponse = false;

         //if we get a response, do something with it (find the intents, output text, etc.)
         if (resp != null && (messageResponse.intents.Length > 0 || messageResponse.entities.Length > 0))
         {
             string intent = messageResponse.intents[0].intent;
             //foreach (string WatsonResponse in messageResponse.output.text) {
             //    outputText += WatsonResponse + " ";
             //}


             outputText = messageResponse.output.text[0];
             Debug.Log("Intent/Output Text: " + intent + "/" + outputText);
             if (intent.Contains("exit")) {
                 stopListeningFlag = true;
             }
             CallTTS (outputText);
             outputText = "";
         }
     }

在底部附近你可以看到我正在寻找包含&#34;退出&#34;的意图。