[英]How to get $context variables from the Watson Conversation Service in Unity?
[英]Get intents and entities from Watson Conversation in Unity
我已經創建了一個包含意圖,對話節點實體的對話工作區。 在Unity中,我與Watson對話,上述對話工作區,語音到文本以及文本到語音的連接都有聯系。 我的對話工作區和一個城市實體中有一個天氣意圖。 我要實現的目標是在Unity中認識到用戶使用實體@city說了意圖#weather,並選擇了@city單詞並將其顯示在應用程序中。
例如:用戶說:“柏林的天氣如何?” 然后,我希望能夠從該句子中提取單詞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 Store將其與Watson 2.0.1一起使用,還是現在應該以完全不同的方式來實現?
這是我在1個腳本中組合的不同Watson函數的代碼。 使用此腳本,我設置了對話服務,語音轉文本等。並且我想嘗試在包含本文頂部代碼的不同腳本中提取意圖和實體。
如果沒有收到任何響應,請檢查您的工作區ID,以確保它正確地引用了您的意圖,實體和對話框。
這是我構建的演示中的示例。 我不確定您如何構建MessageResponse,但由於要使用往返於Watson服務的JSON,因此需要確保要進行序列化/反序列化。
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 = "";
}
}
在底部附近,您可以看到我正在尋找包含“退出”的意圖。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.