YnotZer0 / unity3d_watsonSdk

C# file used in the developerWorks article
8 stars 5 forks source link

(Question) How to access intent in this case? #3

Closed tiancaipipi110 closed 5 years ago

tiancaipipi110 commented 5 years ago

Hello, I'd like to write a switch based on intent. How can I achieve it in the Watson script in here?

YnotZer0 commented 5 years ago

not sure what it is you are requesting here? are you asking if multiple intents are returned from the Assistant service, you would like to step through those values and perform some action based upon that?

If so, line 440: (response as Dictionary<string, object>).TryGetValue("intents", out tempIntentsObj); is where the intents are extracted from the JSON returned from the Assistant service (you can debug / Log.Debug() here to see the value of the JSON). I've hardcoded it in the following lines to only get element [0] text.

The reason I'm a bit confused is that this is after the event - ie. you passed the utterance to Assistant service, it is that service that then performs the Intent identification and then uses the dialog flow to return a response to the code. As I say, the JSON response will include the list of Intents identified and using the above location in the code you can interrogate the values and do what you want with them.

does that answer your question?

tiancaipipi110 commented 5 years ago

are you asking if multiple intents are returned from the Assistant service, you would like to step through those values and perform some action based upon that?

Yes. I'm trying to do something like:

 if (resp != null && (messageResponse.intents.Length > 0 || messageResponse.entities.Length > 0))
        {
            string intent = messageResponse.intents[0].intent;
            Debug.Log("Intent: " + intent);
            string currentMat = null;
            string currentScale = null;
            if (intent == "joke")
            {
                 Do something here;
            }
            else if (intent == "story")
            {
                 Do something here;       
            }

But I can't really access the individual intent in this case.

YnotZer0 commented 5 years ago

okay, on line 404 I output this Log.Debug() Log.Debug("Assistant.OnMessage()", "Response: {0}", customData["json"].ToString());

[01/25/2019 17:09:08][Assistant.OnMessage()][DEBUG] Response: {"intents":[{"intent":"General_Greetings","confidence":0.9858184337615967}],"entities":[],"input":{"text":"first hello"},"output":{"text":["Hello. Good evening"],"nodes_visited":["node_13_1502484041694","node_15_1488295465298"],"log_messages":[]},"context":{"conversation_id":"17d483b5-d426-4490-bd59-1633c05fc3b2","system":{"initialized":true,"dialog_stack":[{"dialog_node":"root"}],"dialog_turn_counter":1,"dialog_request_counter":1,"_node_output_map":{"node_15_1488295465298":[0]},"branch_exited":true,"branch_exited_reason":"completed"}}}

You can see there that the "intents":[] array object is returned, initially you see that for me here the "General_Greetings" intent is the only entry.

so, you'd have to do something similar to be able to step through the "intents" object above - I can see your logic in your snippet above - it seems logical..and my JavaScript background would say, "sure that should be okay". but we're dealing with C# and Microsoft here....

have you tried to see what messageResponse.Intents contains? (note the capital "I" - I mention this as line 444 is referencing messageResponse.Output and that successfully gets the output object text content)

tiancaipipi110 commented 5 years ago

@YnotZer0 Thanks. Btw, Watson seems to sleep after couple minutes of no activity. Is there a way to wake Waton up manually?

YnotZer0 commented 5 years ago

not sure what you mean by "Watson sleep after a couple of minutes"? There is no "Watson", it's just a couple of REST API services that are called from the code, STT, WA, TTS.

Do you mean the code for the microphone listening within Unity? That is like Amazon or Google Home, it's in listening mode all the time (apart from when the TTS service has finished and the speaker is speaking the words).

tiancaipipi110 commented 5 years ago

I mean STT stops working if there’s no user input after couple minutes. It is not always in listening mode.

On Fri, Feb 1, 2019 at 10:06 AM tony pigram notifications@github.com wrote:

not sure what you mean by "Watson sleep after a couple of minutes"? There is no "Watson", it's just a couple of REST API services that are called from the code, STT, WA, TTS.

Do you mean the code for the microphone listening within Unity? That is like Amazon or Google Home, it's in listening mode all the time (apart from when the TTS service has finished and the speaker is speaking the words).

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/YnotZer0/unity3d_watsonSdk/issues/3#issuecomment-459752850, or mute the thread https://github.com/notifications/unsubscribe-auth/AhpC8mT84PpI1B6rtJ3xzTCs9TCT5kGaks5vJFfsgaJpZM4aOCD4 .