Esempio n. 1
0
        /// <summary>
        /// Tries to extract intent and entities from the input utterance and validates if the the current parameter should be updated or if a context switch is needed.
        /// </summary>
        /// <param name="service">Instance of Microsoft.Bot.Builder.Luis.ILuisService to use for extracting intent and entities.</param>
        /// <param name="action">The current action and context.</param>
        /// <param name="paramName">The parameter to try to assign.</param>
        /// <param name="paramValue">The parameter value to assign.</param>
        /// <param name="token">Cancellation Token.</param>
        /// <param name="entityExtractor">Optional: EntityExtractor to disambiguate multiple values into a single one.</param>
        /// <returns>Indicates if the Action Parameter was validated (and set) or a if a Context Switch should occur.</returns>
        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 (queryIntent != null && !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 && !newAction.GetType().Equals(action.GetType()))
                {
                    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));
        }
Esempio n. 2
0
        /// <summary>
        /// Returns an action based on the LUIS intent.
        /// </summary>
        /// <param name="luisResult">The LUIS Result containing intent and extracted entities.</param>
        /// <param name="intentName">The LUIS intent name.</param>
        /// <param name="intentEntities">The LUIS extracted entities.</param>
        /// <param name="entityExtractor">Optional: EntityExtractor to disambiguate multiple values into a single one.</param>
        /// <returns>The matched action plus its parameters filled with the extracted entities, or Null if no action was matched.</returns>
        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);
        }