Пример #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));
        }
Пример #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);
        }
Пример #3
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);
        }
Пример #4
0
        public static LuisActionBindingAttribute GetActionDefinition(ILuisAction action)
        {
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            return(action.GetType().GetCustomAttributes <LuisActionBindingAttribute>(true).FirstOrDefault());
        }
Пример #5
0
        public static bool IsContextualAction(ILuisAction action)
        {
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            return(action
                   .GetType()
                   .GetInterfaces()
                   .Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ILuisContextualAction <>)));
        }
Пример #6
0
        public static LuisActionBindingParamAttribute GetParameterDefinition(ILuisAction action, string paramName)
        {
            action.ThrowIfNull(nameof(action));

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

            var prop = action.GetType().GetProperty(paramName, BindingFlags.Public | BindingFlags.Instance);

            // search binding attrib
            return(prop.GetCustomAttributes <LuisActionBindingParamAttribute>(true).FirstOrDefault());
        }
Пример #7
0
        public static bool UpdateIfValidContextualAction(ILuisAction action, ILuisAction context, out bool isContextual)
        {
            action.ThrowIfNull(nameof(action));
            context.ThrowIfNull(nameof(context));

            isContextual = false;
            if (!IsContextualAction(context))
            {
                return(true);
            }

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

            return(IsValidContextualAction(action, actionContext, out isContextual));
        }
Пример #8
0
        public static bool AssignValue(ILuisAction action, string paramName, object paramValue)
        {
            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));
            }

            return(AssignValue(action, action.GetType().GetProperty(paramName, BindingFlags.Public | BindingFlags.Instance), paramValue));
        }
Пример #9
0
        public static bool IsValidContextualAction(ILuisAction action, ILuisAction context, out bool isContextual)
        {
            action.ThrowIfNull(nameof(action));
            context.ThrowIfNull(nameof(context));

            isContextual = IsContextualAction(action);
            if (isContextual)
            {
                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);
        }
Пример #10
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));
        }
Пример #11
0
 private static Type[] GetActionInterface(ILuisAction action)
 {
     return(action.GetType().GetInterfaces());
 }