Search code examples
c#unity-game-engineibm-watsonwatson-conversationwatson

Get intents and entities from Watson Conversation in Unity


I have made a conversation workspace with intents, entities en dialog nodes. In Unity i have a connection with Watson conversation, the aforementioned conversation workspace, speech to text and text to speech connected. there is a weather intent in my conversation workspace and a city entity. What i want to achieve with that is to recognize in Unity that the user says the intent #weather with the entity @city and pick out the @city word and show it in the application.

For example: the user says: "What is the weather in Berlin?" Then i want to be able to extract the word Berlin (the entity country) out of that sentence and display it in the console with a Debug.Log.

I have everything set-up and working in Unity, I just dont know how to call out intents and entities in C#.

I have found the following example from the Watson Cardboard vr example and tried to apply it in my application, but it doesn't work in my application.

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();");
        }
    }
}

Is there a way to get this to work with Watson 2.0.1 from the Unity Asset store or should this now be approached in a totally different way?

This is the code I have for the different Watson functions combined in 1 script. With this script I have set-up the conversation service, Speech-to-Text etc. And I wanna try to extract the intents and entities within a different script that contains the code at the top of this post.


Solution

  • If you are not getting a response back of any kind, check your workspace id to make sure it's correctly referencing the workspace with your intents, entities, and dialog.

    Here's an example from a demo I built. I'm not sure how you are building your MessageResponse, but you need to make sure you are serializing/deserializing since you are working with JSON to and from Watson services.

    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 = "";
             }
         }
    

    Near the bottom you can see I'm looking for an intent that contains "exit".