Ejemplo n.º 1
0
        public static ILuisAction BuildContextForContextualAction(ILuisAction action, out string intentName)
        {
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            if (!LuisActionResolver.IsContextualAction(action))
            {
                intentName = null;
                return(null);
            }

            var contextType = action
                              .GetType()
                              .GetInterfaces()
                              .First(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ILuisContextualAction <>))
                              .GetGenericArguments()[0];

            var result = Activator.CreateInstance(contextType) as ILuisAction;

            intentName = LuisActionResolver.GetActionDefinition(result).IntentName;

            var isContextual = false;

            LuisActionResolver.IsValidContextualAction(action, result, out isContextual);

            return(result);
        }
Ejemplo n.º 2
0
        public static bool IsValidContextualAction(ILuisAction action, ILuisAction context, out bool isContextual)
        {
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

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

            isContextual = LuisActionResolver.IsContextualAction(action);

            if (!isContextual)
            {
                return(false);
            }

            var validContextualType = typeof(ILuisContextualAction <>).MakeGenericType(context.GetType());

            if (validContextualType.IsAssignableFrom(action.GetType()))
            {
                var prop = validContextualType.GetProperty("Context", BindingFlags.Public | BindingFlags.Instance);
                prop.SetValue(action, context);

                return(true);
            }

            return(false);
        }
Ejemplo n.º 3
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.º 4
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);
        }
Ejemplo n.º 5
0
        public static bool CanStartWithNoContextAction(ILuisAction action, out LuisActionBindingAttribute actionDefinition)
        {
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            if (!LuisActionResolver.IsContextualAction(action))
            {
                actionDefinition = null;
                return(true);
            }

            actionDefinition = LuisActionResolver.GetActionDefinition(action);

            return(actionDefinition.CanExecuteWithNoContext);
        }
Ejemplo n.º 6
0
        public virtual bool IsValid(out ICollection <ValidationResult> validationResults)
        {
            var context = new ValidationContext(this, null, null);

            validationResults = new List <ValidationResult>();
            var result = Validator.TryValidateObject(this, context, validationResults, true);

            // do order properties
            validationResults = validationResults
                                .OrderBy(r =>
            {
                var paramAttrib = LuisActionResolver.GetParameterDefinition(this, r.MemberNames.First());

                return(paramAttrib != null ? paramAttrib.Order : int.MaxValue);
            })
                                .ThenBy(r => r.MemberNames.First())
                                .ToList();

            return(result);
        }
Ejemplo n.º 7
0
        public static bool UpdateIfValidContextualAction(ILuisAction action, ILuisAction context, out bool isContextual)
        {
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

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

            isContextual = false;

            if (!LuisActionResolver.IsContextualAction(context))
            {
                return(true);
            }

            var prop          = context.GetType().GetProperty("Context", BindingFlags.Public | BindingFlags.Instance);
            var actionContext = prop.GetValue(context) as ILuisAction;

            return(LuisActionResolver.IsValidContextualAction(action, actionContext, out isContextual));
        }