コード例 #1
0
        /// <summary>
        /// Called when input has been received, recognices choice.
        /// </summary>
        /// <param name="dc">The <see cref="DialogContext"/> for the current turn of conversation.</param>
        /// <param name="cancellationToken">Optional, the <see cref="CancellationToken"/> that can be used by other objects or threads to receive notice of cancellation.</param>
        /// <returns>InputState which reflects whether input was recognized as valid or not.</returns>
        protected override Task <InputState> OnRecognizeInputAsync(DialogContext dc, CancellationToken cancellationToken = default(CancellationToken))
        {
            var input   = dc.State.GetValue <object>(VALUE_PROPERTY);
            var options = dc.State.GetValue <ChoiceInputOptions>(ThisPath.Options);

            var choices = options.Choices;

            var result = new PromptRecognizerResult <FoundChoice>();

            if (dc.Context.Activity.Type == ActivityTypes.Message)
            {
                var opt = this.RecognizerOptions?.GetValue(dc.State) ?? new FindChoicesOptions();
                opt.Locale = GetCulture(dc);
                var results = ChoiceRecognizers.RecognizeChoices(input.ToString(), choices, opt);
                if (results == null || results.Count == 0)
                {
                    return(Task.FromResult(InputState.Unrecognized));
                }

                var foundChoice = results[0].Resolution;
                switch (this.OutputFormat.GetValue(dc.State))
                {
                case ChoiceOutputFormat.Value:
                default:
                    dc.State.SetValue(VALUE_PROPERTY, foundChoice.Value);
                    break;

                case ChoiceOutputFormat.Index:
                    dc.State.SetValue(VALUE_PROPERTY, foundChoice.Index);
                    break;
                }
            }

            return(Task.FromResult(InputState.Valid));
        }
コード例 #2
0
        /// <summary>
        /// Attempts to recognize the user's input.
        /// </summary>
        /// <param name="turnContext">Context for the current turn of conversation with the user.</param>
        /// <param name="state">Contains state for the current instance of the prompt on the dialog stack.</param>
        /// <param name="options">A prompt options object constructed from the options initially provided
        /// in the call to <see cref="DialogContext.PromptAsync(string, PromptOptions, CancellationToken)"/>.</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects
        /// or threads to receive notice of cancellation.</param>
        /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
        /// <remarks>If the task is successful, the result describes the result of the recognition attempt.</remarks>
        protected override Task <PromptRecognizerResult <FoundChoice> > OnRecognizeAsync(ITurnContext turnContext, IDictionary <string, object> state, PromptOptions options, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (turnContext == null)
            {
                throw new ArgumentNullException(nameof(turnContext));
            }

            var choices = options.Choices ?? new List <Choice>();

            var result = new PromptRecognizerResult <FoundChoice>();

            if (turnContext.Activity.Type == ActivityTypes.Message)
            {
                var activity  = turnContext.Activity;
                var utterance = activity.Text;
                var opt       = RecognizerOptions ?? new FindChoicesOptions();
                opt.Locale = DetermineCulture(activity, opt);
                var results = ChoiceRecognizers.RecognizeChoices(utterance, choices, opt);
                if (results != null && results.Count > 0)
                {
                    result.Succeeded = true;
                    result.Value     = results[0].Resolution;
                }
                else
                {
                    result.Succeeded = true;
                }
            }

            return(Task.FromResult(result));
        }
コード例 #3
0
        public void ShouldFindMultipleChoicesInAnUtteranceByNumerical_index()
        {
            var found = ChoiceRecognizers.RecognizeChoices("option one and 3.", colorChoices);

            Assert.AreEqual(2, found.Count);
            AssertChoice(found[0], "red", 0, 1.0f);
            AssertChoice(found[1], "blue", 2, 1.0f);
        }
コード例 #4
0
        public void ShouldFindAChoiceInAnUtteranceByNumericalIndex_Text()
        {
            var found = ChoiceRecognizers.RecognizeChoices("one", colorChoices);

            Assert.AreEqual(1, found.Count);
            AssertResult(found[0], 0, 2, "one");
            AssertChoice(found[0], "red", 0, 1.0f);
        }
コード例 #5
0
        public void ShouldFindMultipleChoicesInAnUtteranceByOrdinalPosition()
        {
            var found = ChoiceRecognizers.RecognizeChoices("the first and third one please.", colorChoices);

            Assert.AreEqual(2, found.Count);
            AssertChoice(found[0], "red", 0, 1.0f);
            AssertChoice(found[1], "blue", 2, 1.0f);
        }
コード例 #6
0
        public void ShouldFindAChoiceInAnUtteranceByOrdinalPosition()
        {
            var found = ChoiceRecognizers.RecognizeChoices("the first one please.", colorChoices);

            Assert.AreEqual(1, found.Count);
            AssertResult(found[0], 4, 8, "first");
            AssertChoice(found[0], "red", 0, 1.0f);
        }
コード例 #7
0
        public void ShouldFindAChoiceInAnUtteranceByName()
        {
            var found = ChoiceRecognizers.RecognizeChoices("the red one please.", colorChoices);

            Assert.AreEqual(1, found.Count);
            AssertResult(found[0], 4, 6, "red");
            AssertChoice(found[0], "red", 0, 1.0f, "red");
        }
コード例 #8
0
        public void ShouldFindAChoiceInAnUtteranceByNumericalIndex_digit()
        {
            var found = ChoiceRecognizers.RecognizeChoices("1", colorChoices);

            Assert.Single(found);
            AssertResult(found[0], 0, 0, "1");
            AssertChoice(found[0], "red", 0, 1.0f);
        }
コード例 #9
0
        public void ShouldNOTFindAChoiceInAnUtteranceByNumericalIndex_Text_RecognizeNumbersFalse()
        {
            var found = ChoiceRecognizers.RecognizeChoices("one", colorChoices, new FindChoicesOptions()
            {
                RecognizeNumbers = false
            });

            Assert.AreEqual(0, found.Count);
        }
コード例 #10
0
        public void ShouldNOTFindAChoiceInAnUtteranceByOrdinalPosition_RecognizeOrdinalsFalseAndRecognizeNumbersFalse()
        {
            var found = ChoiceRecognizers.RecognizeChoices("the first one please.", colorChoices, new FindChoicesOptions()
            {
                RecognizeOrdinals = false, RecognizeNumbers = false
            });

            Assert.AreEqual(0, found.Count);
        }
コード例 #11
0
        protected override Task <PromptRecognizerResult <bool> > OnRecognizeAsync(ITurnContext turnContext, IDictionary <string, object> state, PromptOptions options, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (turnContext == null)
            {
                throw new ArgumentNullException(nameof(turnContext));
            }

            var result = new PromptRecognizerResult <bool>();

            if (turnContext.Activity.Type == ActivityTypes.Message)
            {
                // Recognize utterance
                var message = turnContext.Activity.AsMessageActivity();
                var culture = turnContext.Activity.Locale ?? DefaultLocale ?? English;
                var results = ChoiceRecognizer.RecognizeBoolean(message.Text, culture);
                if (results.Count > 0)
                {
                    var first = results[0];
                    if (bool.TryParse(first.Resolution["value"].ToString(), out var value))
                    {
                        result.Succeeded = true;
                        result.Value     = value;
                    }
                }
                else
                {
                    // First check whether the prompt was sent to the user with numbers - if it was we should recognize numbers
                    var choiceOptions = ChoiceOptions ?? DefaultChoiceOptions[culture];

                    // This logic reflects the fact that IncludeNumbers is nullable and True is the default set in Inline style
                    if (!choiceOptions.IncludeNumbers.HasValue || choiceOptions.IncludeNumbers.Value)
                    {
                        // The text may be a number in which case we will interpret that as a choice.
                        var confirmChoices = ConfirmChoices ?? DefaultConfirmChoices[culture];
                        var choices        = new List <Choice> {
                            confirmChoices.Item1, confirmChoices.Item2
                        };
                        var secondAttemptResults = ChoiceRecognizers.RecognizeChoices(message.Text, choices);
                        if (secondAttemptResults.Count > 0)
                        {
                            result.Succeeded = true;
                            result.Value     = secondAttemptResults[0].Resolution.Index == 0;
                        }
                    }
                }
            }

            return(Task.FromResult(result));
        }
コード例 #12
0
        public async Task <ChoiceResult> RecognizeAsync(ITurnContext context, List <Choice> choices)
        {
            BotAssert.ContextNotNull(context);
            BotAssert.ActivityNotNull(context.Activity);
            if (context.Activity.Type != ActivityTypes.Message)
            {
                throw new InvalidOperationException("No Message to Recognize");
            }

            if (choices == null)
            {
                throw new ArgumentNullException(nameof(choices));
            }

            var request   = context.Activity;
            var utterance = request.Text;
            var options   = RecognizerOptions ?? new FindChoicesOptions();

            options.Locale = request.Locale ?? options.Locale ?? Culture ?? Recognizers.Text.Culture.English;
            var results = ChoiceRecognizers.RecognizeChoices(utterance, choices, options);

            if (results != null && results.Count > 0)
            {
                var value  = results[0].Resolution;
                var result = new ChoiceResult {
                    Status = PromptStatus.Recognized, Value = value
                };
                if (Validator != null)
                {
                    await Validator(context, result).ConfigureAwait(false);
                }

                return(result);
            }
            else
            {
                return(new ChoiceResult {
                    Status = PromptStatus.NotRecognized
                });
            }
        }
コード例 #13
0
        public void ShouldAcceptNullUtteranceInRecognizeChoices()
        {
            var found = ChoiceRecognizers.RecognizeChoices(null, colorChoices);

            Assert.AreEqual(0, found.Count);
        }
コード例 #14
0
        public void ShouldAcceptNullUtteranceInRecognizeChoices()
        {
            var found = ChoiceRecognizers.RecognizeChoices(null, colorChoices);

            Assert.Empty(found);
        }