Ejemplo n.º 1
0
        private void GetQueryComplete(LUISResult result)
        {
            Debug.Log("LUIS result: \n" + result);

            // Raise LUIS result handler event
            if (OnLUISQueryResult != null)
            {
                OnLUISQueryResult(result, FindCurrentGazedTarget(gazeMaxDistance));
            }
        }
Ejemplo n.º 2
0
        private LUISEntity GetEntityFromLUISResult(LUISResult result, string entityType)
        {
            LUISEntity matchedEntity = null;

            foreach (LUISEntity entity in result.entities)
            {
                if (entity.type.Equals(entityType, ignoreCase) || (entity.resolution != null && entity.resolution.values.Any(s => string.Equals(s, entityType, ignoreCase))))
                {
                    matchedEntity = entity;
                    break;
                }
            }
            return(matchedEntity);
        }
Ejemplo n.º 3
0
        private IEnumerator GetQuery(string query, Action <LUISResult> callback)
        {
            string endpointQuery = endpoint + query;

            Debug.Log("LUIS query endpoint:" + endpointQuery);
            using (UnityWebRequest www = UnityWebRequest.Get(endpointQuery)) {
                yield return(www.SendWebRequest());

                if (www.isNetworkError || www.isHttpError)
                {
                    // Check if LUIS gets too many requests
                    if (www.responseCode.Equals(429))
                    {
                        Debug.LogError("Too many LUIS requests. Try upgrading the service tier. \nQuery: " + query + "\nConcurrent requests will be limited to 1 and a transaction delay has now been set to " + TRANSACTION_DELAY_BASIC_TIER + " secs for future requests.");
                        MaxConcurrentRequests = 1;
                        transactionDelay      = TRANSACTION_DELAY_BASIC_TIER;
                    }
                    else
                    {
                        Debug.LogError("LUIS query error: " + www.error + " status:" + www.responseCode.ToString() + " no. requests:" + requestsCount + "\nQuery: " + query);
                    }
                }
                else
                {
                    // Save token as result
                    string json = www.downloadHandler.text;
                    try {
                        LUISResult result = JsonUtility.FromJson <LUISResult> (json);
                        GetQueryComplete(result);
                    } catch (ArgumentException exception) {
                        Debug.LogError("Failed to parse LUIS result: " + exception.Message + " \nresult:\n" + json);
                    }
                }
                requestsCount--;
            }
        }
Ejemplo n.º 4
0
        private void OnLUISQueryResult(LUISResult result, GameObject gazedObject)
        {
            if (targets == null || actions == null)
            {
                Debug.LogWarning("Add some Unity game object targets and actions");
                return;
            }

            LUISAction action = null;

            foreach (LUISAction possibleAction in actions)
            {
                if (possibleAction.topScoringIntent.Equals(result.topScoringIntent.intent, ignoreCase))
                {
                    action = possibleAction;
                    break;
                }
            }

            if (action == null)
            {
                Debug.LogWarning("No Unity action matches LUIS intent:" + result.topScoringIntent.intent);
                return;
            }

            // Find target's entity name
            LUISEntity targetEntity = GetEntityFromLUISResult(result, entityTargetType);

            if (targetEntity == null)
            {
                Debug.LogWarning("Couldn't find any targets for entity type: " + entityTargetType);
                return;
            }

            // Get action entity property
            LUISEntity entityProperty = GetEntityFromLUISResult(result, action.entityType);

            if (entityProperty == null)
            {
                Debug.LogWarning("No result matches for the action entity type: " + action.entityType);
                return;
            }

            // Only target "this" gameobject
            if (thisTargetSynonyms.Any(s => string.Equals(s, targetEntity.entity, ignoreCase)))
            {
                if (gazedObject == null)
                {
                    Debug.Log("No gazed object detected for target entity: " + targetEntity.entity);
                    return;
                }
                if (GlobalScope)
                {
                    PerformAction(action, gazedObject, entityProperty);
                    return;
                }
                // Check if the gazed object is registered target for LUIS
                bool isThisALUISTarget = false;
                foreach (LUISTarget possibleTarget in targets)
                {
                    if (gazedObject == possibleTarget.target)
                    {
                        isThisALUISTarget = true;
                        Debug.Log("This gazed object is a LUIS target: " + gazedObject.name + " entity:" + entityProperty.entity);
                        PerformAction(action, possibleTarget.target, entityProperty);
                        break;
                    }
                }
                if (!isThisALUISTarget)
                {
                    Debug.LogWarning("This gazed gameObject is not a LUIS target: " + gazedObject.name + " entity:" + entityProperty.entity + "\nTip: Enable GlobalScope to allow LUIS actions to target any gazed GameObject.");
                }
                return;
            }

            // Target the named gameobject (could be modified to invoke action in multiple matched targets)
            foreach (LUISTarget possibleTarget in targets)
            {
                if (String.Equals(possibleTarget.entityName, targetEntity.entity, ignoreCase) || (targetEntity.resolution != null && targetEntity.resolution.values.Any(s => string.Equals(s, possibleTarget.entityName, ignoreCase))))
                {
                    PerformAction(action, possibleTarget.target, entityProperty);
                    break;
                }
                else
                {
                    Debug.Log("Skipped entity name: " + possibleTarget.entityName + " LUIS entity: " + targetEntity.entity);
                }
            }
        }