예제 #1
0
 /// <summary>
 /// Create recognizer from qnaJson
 /// </summary>
 /// <param name="json"></param>
 public QLuceneRecognizer(string knowledgebase, string json)
 {
     if (json != null)
     {
         this.KnowledgeBase = knowledgebase;
         QLuceneEngineCache.GetEngine(this.KnowledgeBase, json);
     }
 }
예제 #2
0
 /// <summary>
 /// Create QLuceneRecognizer from prebuild FSDirectory
 /// </summary>
 /// <param name="dirInfo"></param>
 public QLuceneRecognizer(string knowledgeBase, DirectoryInfo dirInfo)
 {
     this.KnowledgeBase = knowledgeBase;
     QLuceneEngineCache.GetEngine(this.KnowledgeBase, () => FSDirectory.Open(dirInfo));
 }
예제 #3
0
        public async override Task <RecognizerResult> RecognizeAsync(DialogContext dialogContext, Activity activity, CancellationToken cancellationToken = default, Dictionary <System.String, System.String> telemetryProperties = null, Dictionary <System.String, System.Double> telemetryMetrics = null)
        {
            var qluceneEngine = await QLuceneEngineCache.GetEngine(dialogContext, this.KnowledgeBase).ConfigureAwait(false);

            var threshold        = Threshold.GetValue(dialogContext);
            var recognizerResult = new RecognizerResult
            {
                Text    = activity.Text,
                Intents = new Dictionary <string, IntentScore>(),
            };

            if (string.IsNullOrEmpty(activity.Text))
            {
                recognizerResult.Intents.Add("None", new IntentScore());
                return(recognizerResult);
            }
            var  rankerType = RankerType?.GetValue(dialogContext.State);
            var  strictFiltersCompoundOperationType = StrictFiltersCompoundOperationType?.GetValue(dialogContext.State);
            var  strictFilters = new List <Metadata>();
            var  context       = Context?.GetValue(dialogContext.State);
            bool includeDialogNameInMetadata = IncludeDialogNameInMetadata.GetValue(dialogContext.State);

            if (includeDialogNameInMetadata)
            {
                strictFilters.Add(new Metadata
                {
                    Name  = "dialogName",
                    Value = dialogContext.ActiveDialog.Id
                });
            }

            // if there is $qna.metadata set add to filters
            var externalMetadata = StrictFilters?.GetValue(dialogContext.State);

            if (externalMetadata != null)
            {
                strictFilters.AddRange(externalMetadata);
            }

            var topAnswer = qluceneEngine.GetAnswers(activity.Text,
                                                     strictFilters: strictFilters.ToArray(),
                                                     context: context,
                                                     threshold: threshold,
                                                     rankerType: rankerType,
                                                     strictFiltersCompoundOperationType: strictFiltersCompoundOperationType);

            if (topAnswer != null)
            {
                if (topAnswer.Answer.Trim().ToUpperInvariant().StartsWith(IntentPrefix.ToUpperInvariant(), StringComparison.Ordinal))
                {
                    recognizerResult.Intents.Add(topAnswer.Answer.Trim().Substring(IntentPrefix.Length).Trim(), new IntentScore {
                        Score = topAnswer.Score
                    });
                }
                else
                {
                    recognizerResult.Intents.Add(QnAMatchIntent, new IntentScore {
                        Score = topAnswer.Score
                    });
                }

                var answerArray = new JArray();
                answerArray.Add(topAnswer.Answer);
                ObjectPath.SetPathValue(recognizerResult, "entities.answer", answerArray);

                var instance = new JArray();
                var data     = JObject.FromObject(topAnswer);
                data["startIndex"] = 0;
                data["endIndex"]   = activity.Text.Length;
                instance.Add(data);
                ObjectPath.SetPathValue(recognizerResult, "entities.$instance.answer", instance);

                // recognizerResult.Properties["answers"] = answers;
            }
            else
            {
                recognizerResult.Intents.Add("None", new IntentScore {
                    Score = 1.0f
                });
            }

            var traceInfo = new
            {
                recognizerResult,
                options = new
                {
                    id             = this.Id,
                    knowledgeBase  = this.KnowledgeBase,
                    scoreThreshold = threshold,
                    context,
                    rankerType,
                    strictFilters = strictFilters.ToArray(),
                    includeDialogNameInMetadata,
                    strictFiltersCompoundOperationType
                }
            };

            await dialogContext.Context.TraceActivityAsync(nameof(QLuceneRecognizer), traceInfo, TraceType, TraceLabel, cancellationToken).ConfigureAwait(false);

            return(recognizerResult);
        }