Exemple #1
0
        /// <summary>
        /// Construct V3 recognizer options from the current dialog context.
        /// </summary>
        /// <param name="dialogContext">Context.</param>
        /// <returns>LUIS Recognizer options.</returns>
        public LuisRecognizerOptionsV3 RecognizerOptions(DialogContext dialogContext)
        {
            var options = PredictionOptions;

            if (DynamicLists != null)
            {
                options = new AI.LuisV3.LuisPredictionOptions(options);
                var list = new List <AI.LuisV3.DynamicList>();
                foreach (var listEntity in DynamicLists.GetValue(dialogContext.State))
                {
                    list.Add(new AI.LuisV3.DynamicList(listEntity.Entity, listEntity.List));
                }

                options.DynamicLists = list;
            }

            var application = new LuisApplication(ApplicationId.GetValue(dialogContext.State), EndpointKey.GetValue(dialogContext.State), Endpoint.GetValue(dialogContext.State));

            return(new LuisRecognizerOptionsV3(application)
            {
                ExternalEntityRecognizer = ExternalEntityRecognizer,
                PredictionOptions = options,
                TelemetryClient = TelemetryClient
            });
        }
Exemple #2
0
        /// <summary>
        /// Construct V3 recognizer options from the current dialog context.
        /// </summary>
        /// <param name="dialogContext">Context.</param>
        /// <returns>LUIS Recognizer options.</returns>
        public LuisRecognizerOptionsV3 RecognizerOptions(DialogContext dialogContext)
        {
            AI.LuisV3.LuisPredictionOptions options = new LuisV3.LuisPredictionOptions();
            if (this.PredictionOptions != null)
            {
                options = new LuisV3.LuisPredictionOptions(this.PredictionOptions);
            }
            else if (this.Options != null)
            {
                options.DateTimeReference   = this.Options.DateTimeReference?.GetValue(dialogContext);
                options.ExternalEntities    = this.Options.ExternalEntities?.GetValue(dialogContext);
                options.IncludeAllIntents   = this.Options.IncludeAllIntents?.GetValue(dialogContext) ?? false;
                options.IncludeInstanceData = this.Options.IncludeInstanceData?.GetValue(dialogContext) ?? true;
                options.IncludeAPIResults   = this.Options.IncludeAPIResults?.GetValue(dialogContext) ?? false;
                options.Log = this.Options.Log?.GetValue(dialogContext) ?? true;
                options.PreferExternalEntities = this.Options.PreferExternalEntities?.GetValue(dialogContext) ?? true;
                options.Slot = this.Options.Slot?.GetValue(dialogContext);
            }

            if (this.Version != null)
            {
                options.Version = this.Version?.GetValue(dialogContext.State);
            }

            if (DynamicLists != null)
            {
                var list = new List <AI.LuisV3.DynamicList>();
                foreach (var listEntity in DynamicLists.GetValue(dialogContext.State))
                {
                    list.Add(new AI.LuisV3.DynamicList(listEntity.Entity, listEntity.List));
                }

                options.DynamicLists = list;
            }

            var application = new LuisApplication(ApplicationId.GetValue(dialogContext.State), EndpointKey.GetValue(dialogContext.State), Endpoint.GetValue(dialogContext.State));

            return(new LuisRecognizerOptionsV3(application)
            {
                ExternalEntityRecognizer = ExternalEntityRecognizer,
                PredictionOptions = options,
                TelemetryClient = TelemetryClient,
                IncludeAPIResults = options.IncludeAPIResults,
            });
        }
Exemple #3
0
        /// <summary>
        /// Runs current DialogContext.TurnContext.Activity through a recognizer and returns a <see cref="RecognizerResult"/>.
        /// </summary>
        /// <param name="dialogContext">The <see cref="DialogContext"/> for the current turn of conversation.</param>
        /// <param name="activity"><see cref="Activity"/> to recognize.</param>
        /// <param name="cancellationToken">Optional, <see cref="CancellationToken"/> of the task.</param>
        /// <param name="telemetryProperties">Optional, additional properties to be logged to telemetry with the LuisResult event.</param>
        /// <param name="telemetryMetrics">Optional, additional metrics to be logged to telemetry with the LuisResult event.</param>
        /// <returns>Analysis of utterance.</returns>
        public override async Task <RecognizerResult> RecognizeAsync(DialogContext dialogContext, Activity activity, CancellationToken cancellationToken, Dictionary <string, string> telemetryProperties = null, Dictionary <string, double> telemetryMetrics = null)
        {
            // Identify matched intents
            var text   = activity.Text ?? string.Empty;
            var locale = activity.Locale ?? "en-us";

            var recognizerResult = new RecognizerResult()
            {
                Text     = text,
                Entities = new JObject()
            };

            if (string.IsNullOrWhiteSpace(text))
            {
                // nothing to recognize, return empty recognizerResult
                return(recognizerResult);
            }

            var dynamicLists = DynamicLists.GetValue(dialogContext.State);
            var fuzzyMatch   = FuzzyMatch.GetValue(dialogContext.State);

            dynamic entities = recognizerResult.Entities;

            foreach (var dynamicList in dynamicLists)
            {
                var matches = Search(text, dynamicList.List, fuzzyMatch);

                if (matches.Any())
                {
                    var clusters = matches.GroupBy(match => $"{match.StartOffset}-{match.EndOffset}");

                    entities["$instance"]        = new JObject();
                    entities[dynamicList.Entity] = new JArray();
                    entities["$instance"][dynamicList.Entity] = new JArray();

                    foreach (var cluster in clusters)
                    {
                        // add all ids with exact same start/end
                        entities[dynamicList.Entity].Add(JArray.FromObject(cluster.Select(cl => cl.Id).ToArray()));

                        dynamic instance = new JObject();
                        var     match    = cluster.First();
                        instance.startIndex = match.StartOffset;
                        instance.endIndex   = match.EndOffset;
                        instance.score      = match.Score;
                        instance.text       = text.Substring(match.StartOffset, match.EndOffset - match.StartOffset);
                        instance.type       = dynamicList.Entity;
                        entities["$instance"][dynamicList.Entity].Add(instance);
                    }
                }
            }

            // if no match return None intent
            recognizerResult.Intents.Add("None", new IntentScore()
            {
                Score = 1.0
            });

            await dialogContext.Context.TraceActivityAsync(nameof(DynamicListRecognizer), JObject.FromObject(recognizerResult), "RecognizerResult", "DynamicListRecognizerResult", cancellationToken).ConfigureAwait(false);

            this.TrackRecognizerResult(dialogContext, "DynamicListRecognizerResult", this.FillRecognizerResultTelemetryProperties(recognizerResult, telemetryProperties), telemetryMetrics);

            return(recognizerResult);
        }