/// <summary>
        /// Used to validate the incoming text, expected on context.Request, is
        /// valid according to the rules defined in the validation steps.
        /// </summary>
        public override async Task <NumberResult <float> > Recognize(IBotContext context)
        {
            BotAssert.ContextNotNull(context);
            BotAssert.ActivityNotNull(context.Request);
            if (context.Request.Type != ActivityTypes.Message)
            {
                throw new InvalidOperationException("No Message to Recognize");
            }

            IMessageActivity message = context.Request.AsMessageActivity();
            var results = _model.Parse(message.Text);

            if (results.Any())
            {
                var result = results.First();
                if (float.TryParse(result.Resolution["value"].ToString().TrimEnd('%'), out float value))
                {
                    NumberResult <float> numberResult = new NumberResult <float>()
                    {
                        Value = value,
                        Text  = result.Text
                    };
                    if (await Validate(context, numberResult))
                    {
                        return(numberResult);
                    }
                }
            }
            return(null);
        }
示例#2
0
        /// <summary>
        /// Used to validate the incoming text, expected on context.Request, is
        /// valid according to the rules defined in the validation steps.
        /// </summary>
        public override async Task <NumberResult <T> > Recognize(IBotContext context)
        {
            BotAssert.ContextNotNull(context);
            BotAssert.ActivityNotNull(context.Request);
            if (context.Request.Type != ActivityTypes.Message)
            {
                throw new InvalidOperationException("No Message to Recognize");
            }

            NumberResult <T> numberResult = new NumberResult <T>();

            IMessageActivity message = context.Request.AsMessageActivity();
            var results = _model.Parse(message.Text);

            if (results.Any())
            {
                var result = results.First();
                if (typeof(T) == typeof(float))
                {
                    if (float.TryParse(result.Resolution["value"].ToString(), out float value))
                    {
                        numberResult.Status = RecognitionStatus.Recognized;
                        numberResult.Value  = (T)(object)value;
                        numberResult.Text   = result.Text;
                        await Validate(context, numberResult);
                    }
                }
                else if (typeof(T) == typeof(int))
                {
                    if (int.TryParse(result.Resolution["value"].ToString(), out int value))
                    {
                        numberResult.Status = RecognitionStatus.Recognized;
                        numberResult.Value  = (T)(object)value;
                        numberResult.Text   = result.Text;
                        await Validate(context, numberResult);
                    }
                }
            }
            return(numberResult);
        }