从 Unity 中的 Watson Conversation 获取意图和实体
Get intents and entities from Watson Conversation in Unity
我创建了一个对话工作区,其中包含意图、实体和对话节点。在 Unity 中,我与 Watson 对话、上述对话工作区、语音到文本和文本到语音连接。
我的对话工作区和城市实体中有一个天气意图。我想要实现的是在 Unity 中识别用户使用实体 @city 说出意图 #weather 并挑选出 @city 词并将其显示在应用程序中。
例如:用户说:"What is the weather in Berlin?"
然后我希望能够从该句子中提取柏林(实体国家)一词,并在控制台中显示 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 Asset 商店中的 Watson 2.0.1 一起使用,或者现在应该以完全不同的方式处理它?
这是我在 1 个脚本中组合不同 Watson 函数的代码。使用此脚本,我设置了对话服务、语音到文本等。我想尝试在另一个脚本中提取意图和实体,该脚本包含此 post.[= 顶部的代码11=]
如果您没有收到任何类型的回复,请检查您的工作区 ID 以确保它正确地引用了包含您的意图、实体和对话的工作区。
这是我构建的演示中的示例。我不确定您是如何构建 MessageResponse 的,但是您需要确保自己是 serializing/deserializing,因为您使用 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 = "";
}
}
在底部附近您可以看到我正在寻找包含 "exit".
的意图
我创建了一个对话工作区,其中包含意图、实体和对话节点。在 Unity 中,我与 Watson 对话、上述对话工作区、语音到文本和文本到语音连接。 我的对话工作区和城市实体中有一个天气意图。我想要实现的是在 Unity 中识别用户使用实体 @city 说出意图 #weather 并挑选出 @city 词并将其显示在应用程序中。
例如:用户说:"What is the weather in Berlin?" 然后我希望能够从该句子中提取柏林(实体国家)一词,并在控制台中显示 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 Asset 商店中的 Watson 2.0.1 一起使用,或者现在应该以完全不同的方式处理它?
这是我在 1 个脚本中组合不同 Watson 函数的代码。使用此脚本,我设置了对话服务、语音到文本等。我想尝试在另一个脚本中提取意图和实体,该脚本包含此 post.[= 顶部的代码11=]
如果您没有收到任何类型的回复,请检查您的工作区 ID 以确保它正确地引用了包含您的意图、实体和对话的工作区。
这是我构建的演示中的示例。我不确定您是如何构建 MessageResponse 的,但是您需要确保自己是 serializing/deserializing,因为您使用 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 = "";
}
}
在底部附近您可以看到我正在寻找包含 "exit".
的意图