Ejemplo n.º 1
0
        public static async Task <QueryValueResult> QueryValueFromLuisAsync(
            ILuisService service,
            ILuisAction action,
            string paramName,
            object paramValue,
            CancellationToken token,
            Func <PropertyInfo, IEnumerable <EntityRecommendation>, EntityRecommendation> entityExtractor = null)
        {
            var originalValue = paramValue;

            if (service == null)
            {
                throw new ArgumentNullException(nameof(service));
            }

            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            if (string.IsNullOrWhiteSpace(paramName))
            {
                throw new ArgumentNullException(nameof(paramName));
            }

            if (paramValue == null)
            {
                throw new ArgumentNullException(nameof(paramValue));
            }

            var result = await service.QueryAsync(paramValue.ToString(), token);

            var queryIntent = result.Intents.FirstOrDefault();

            if (!Intents.None.Equals(queryIntent.Intent, StringComparison.InvariantCultureIgnoreCase))
            {
                var newIntentName = default(string);
                var newAction     = new LuisActionResolver(action.GetType().Assembly).ResolveActionFromLuisIntent(result, out newIntentName);
                if (newAction != null)
                {
                    return(new QueryValueResult(false)
                    {
                        NewAction = newAction,
                        NewIntent = newIntentName
                    });
                }
            }

            var properties = new List <PropertyInfo> {
                action.GetType().GetProperty(paramName, BindingFlags.Public | BindingFlags.Instance)
            };

            if (!LuisActionResolver.AssignEntitiesToMembers(action, properties, result.Entities, entityExtractor))
            {
                return(new QueryValueResult(AssignValue(action, properties.First(), originalValue)));
            }

            return(new QueryValueResult(true));
        }
Ejemplo n.º 2
0
        public ILuisAction ResolveActionFromLuisIntent(
            LuisResult luisResult,
            out string intentName,
            out IList <EntityRecommendation> intentEntities,
            Func <PropertyInfo, IEnumerable <EntityRecommendation>, EntityRecommendation> entityExtractor = null)
        {
            intentEntities = default(IList <EntityRecommendation>);

            if (luisResult == null)
            {
                throw new ArgumentNullException(nameof(luisResult));
            }

            // Has Intent?
            intentName = (luisResult.TopScoringIntent ?? luisResult.Intents?.MaxBy(i => i.Score ?? 0d)).Intent;
            if (string.IsNullOrWhiteSpace(intentName))
            {
                return(null);
            }

            // Set out intent entities reference
            intentEntities = luisResult.Entities;

            // Get Actions that map to this intent
            var actionType = default(Type);

            if (!this.luisActions.TryGetValue(intentName, out actionType))
            {
                return(null);
            }

            // Get the action instance and check if it implements ILuisAction
            var luisAction = Activator.CreateInstance(actionType) as ILuisAction;

            if (luisAction == null)
            {
                return(null);
            }

            // Try complete parameters from entities
            var properties = luisAction.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);

            LuisActionResolver.AssignEntitiesToMembers(luisAction, properties, luisResult.Entities, entityExtractor);

            return(luisAction);
        }