Пример #1
0
        public override async Task <RecognizerResult> RecognizeAsync(DialogContext dialogContext, Activity activity, CancellationToken cancellationToken = default, Dictionary <string, string> telemetryProperties = null, Dictionary <string, double> telemetryMetrics = null)
        {
            if (this._engine == null)
            {
                if (this.Model == null)
                {
                    var resourceExplorer = dialogContext.Context.TurnState.Get <ResourceExplorer>() ?? throw new ArgumentException("No resource Explorer was found in dialog context");
                    resourceExplorer.AddResourceType("yaml");
                    var modelId       = ResourceId.GetValue(dialogContext.State);
                    var modelResource = resourceExplorer.GetResource(modelId) ?? throw new ArgumentException($"{modelId} not found");
                    var yamlOrJson    = await modelResource.ReadTextAsync();

                    this._engine = new LucyEngine(yamlOrJson);
                }
                else
                {
                    this._engine = new LucyEngine(this.Model);
                }
            }

            List <LucyEntity> externalEntities = new List <LucyEntity>();

            if (ExternalEntityRecognizer != null)
            {
                var results = await ExternalEntityRecognizer.RecognizeAsync(dialogContext, activity, cancellationToken, telemetryProperties, telemetryMetrics).ConfigureAwait(false);

                externalEntities = GetEntitiesFromObject(activity, results.Entities).ToList();
            }

            var recognizerResult = new RecognizerResult();
            var lucyEntities     = _engine.MatchEntities(activity.Text ?? string.Empty, externalEntities);

            recognizerResult.Entities = GetRecognizerEntities(lucyEntities);

            var intents = this.Intents.GetValue(dialogContext.State) ?? new List <string>();

            if (intents.Any())
            {
                foreach (var lucyEntity in lucyEntities.Where(entity => intents.Contains(entity.Type)))
                {
                    recognizerResult.Intents.Add(lucyEntity.Type, new IntentScore()
                    {
                        Score = lucyEntity.Score
                    });
                }
            }
            else if (recognizerResult.Entities.Count > externalEntities.Count + 1)
            {
                recognizerResult.Intents.Add(MatchedIntent, new IntentScore()
                {
                    Score = lucyEntities.Max(e => e.Score)
                });
            }

            if (!recognizerResult.Intents.Any())
            {
                recognizerResult.Intents.Add(NoneIntent, new IntentScore {
                    Score = 1.0f
                });
            }

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

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

            return(recognizerResult);
        }