protected override Task <PromptRecognizerResult <NumberWithUnitResult> > 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 <NumberWithUnitResult>();

            if (turnContext.Activity.Type == ActivityTypes.Message)
            {
                var message = turnContext.Activity.AsMessageActivity();
                var culture = turnContext.Activity.Locale ?? DefaultLocale ?? English;

                List <Microsoft.Recognizers.Text.ModelResult> results = null;

                switch (PromptType)
                {
                case NumberWithUnitPromptType.Currency:
                    results = NumberWithUnitRecognizer.RecognizeCurrency(message.Text, culture);
                    break;

                case NumberWithUnitPromptType.Dimension:
                    results = NumberWithUnitRecognizer.RecognizeDimension(message.Text, culture);
                    break;

                case NumberWithUnitPromptType.Age:
                    results = NumberWithUnitRecognizer.RecognizeAge(message.Text, culture);
                    break;

                case NumberWithUnitPromptType.Temperature:
                    results = NumberWithUnitRecognizer.RecognizeTemperature(message.Text, culture);
                    break;
                }

                if (results?.Count > 0)
                {
                    var resolvedUnit  = results[0].Resolution["unit"].ToString();
                    var resolvedValue = results[0].Resolution["value"].ToString();

                    if (double.TryParse(resolvedValue, out var value))
                    {
                        result.Succeeded = true;

                        var numberWithUnitResult = new NumberWithUnitResult
                        {
                            Unit  = resolvedUnit,
                            Value = value
                        };

                        result.Value = numberWithUnitResult;
                    }
                }
            }

            return(Task.FromResult(result));
        }
Esempio n. 2
0
        public void RecognizeDimension()
        {
            var input = "75ml";

            var actual = NumberWithUnitRecognizer.RecognizeDimension(input, Culture.English);

            Assert.AreEqual("dimension", actual[0].TypeName);
            Assert.AreEqual("75ml", actual[0].Text);
            Assert.AreEqual("75", actual[0].Resolution["value"]);
            Assert.AreEqual("Milliliter", actual[0].Resolution["unit"]);
        }
Esempio n. 3
0
        /// <summary>
        /// Parse query with all recognizers
        /// </summary>
        private static IEnumerable <ModelResult> ParseAll(string query, string culture)
        {
            return(MergeResults(
                       // Number recognizer will find any number from the input
                       // E.g "I have two apples" will return "2".
                       NumberRecognizer.RecognizeNumber(query, culture),

                       // Ordinal number recognizer will find any ordinal number
                       // E.g "eleventh" will return "11".
                       NumberRecognizer.RecognizeOrdinal(query, culture),

                       // Percentage recognizer will find any number presented as percentage
                       // E.g "one hundred percents" will return "100%"
                       NumberRecognizer.RecognizePercentage(query, culture),

                       // Number Range recognizer will find any cardinal or ordinal number range
                       // E.g. "between 2 and 5" will return "(2,5)"
                       NumberRecognizer.RecognizeNumberRange(query, culture),

                       // Age recognizer will find any age number presented
                       // E.g "After ninety five years of age, perspectives change" will return "95 Year"
                       NumberWithUnitRecognizer.RecognizeAge(query, culture),

                       // Currency recognizer will find any currency presented
                       // E.g "Interest expense in the 1988 third quarter was $ 75.3 million" will return "75300000 Dollar"
                       NumberWithUnitRecognizer.RecognizeCurrency(query, culture),

                       // Dimension recognizer will find any dimension presented
                       // E.g "The six-mile trip to my airport hotel that had taken 20 minutes earlier in the day took more than three hours." will return "6 Mile"
                       NumberWithUnitRecognizer.RecognizeDimension(query, culture),

                       // Temperature recognizer will find any temperature presented
                       // E.g "Set the temperature to 30 degrees celsius" will return "30 C"
                       NumberWithUnitRecognizer.RecognizeTemperature(query, culture),

                       // Datetime recognizer This model will find any Date even if its write in coloquial language
                       // E.g "I'll go back 8pm today" will return "2017-10-04 20:00:00"
                       DateTimeRecognizer.RecognizeDateTime(query, culture),

                       // PhoneNumber recognizer will find any phone number presented
                       // E.g "My phone number is ( 19 ) 38294427."
                       SequenceRecognizer.RecognizePhoneNumber(query, culture),

                       // Add IP recognizer - This recognizer will find any Ipv4/Ipv6 presented
                       // E.g "My Ip is 8.8.8.8"
                       SequenceRecognizer.RecognizeIpAddress(query, culture),

                       // Add Boolean recognizer - This model will find yes/no like responses, including emoji -
                       // E.g "yup, I need that" will return "True"
                       ChoiceRecognizer.RecognizeBoolean(query, culture)
                       ));
        }
Esempio n. 4
0
        private static List <ModelResult> RecognizeNumberWithUnit(string utterance, string culture)
        {
            var number = NumberRecognizer.RecognizeNumber(utterance, culture);

            if (number.Any())
            {
                // Result when it matches with a number recognizer
                return(number);
            }
            else
            {
                // Analyze every option for numberWithUnit
                var results = new List <List <ModelResult> >();
                results.Add(NumberWithUnitRecognizer.RecognizeCurrency(utterance, culture));
                results.Add(NumberWithUnitRecognizer.RecognizeAge(utterance, culture));
                results.Add(NumberWithUnitRecognizer.RecognizeTemperature(utterance, culture));
                results.Add(NumberWithUnitRecognizer.RecognizeDimension(utterance, culture));

                // Filter the options that returned nothing and return the one that matched
                return(results.FirstOrDefault(r => r.Any()) ?? new List <ModelResult>());
            }
        }
Esempio n. 5
0
        private static IEnumerable <ModelResult> ParseAll(string query, string culture)
        {
            return(MergeResults(
                       // Number recognizer will find any number from the input
                       NumberRecognizer.RecognizeNumber(query, culture),

                       // Ordinal number recognizer will find any ordinal number
                       NumberRecognizer.RecognizeOrdinal(query, culture),

                       // Percentage recognizer will find any number presented as percentage
                       NumberRecognizer.RecognizePercentage(query, culture),

                       // Number Range recognizer will find any cardinal or ordinal number range
                       NumberRecognizer.RecognizeNumberRange(query, culture),

                       // Age recognizer will find any age number presented
                       NumberWithUnitRecognizer.RecognizeAge(query, culture),

                       // Currency recognizer will find any currency presented
                       NumberWithUnitRecognizer.RecognizeCurrency(query, culture),

                       // Dimension recognizer will find any dimension presented
                       NumberWithUnitRecognizer.RecognizeDimension(query, culture),

                       // Temperature recognizer will find any temperature presented
                       NumberWithUnitRecognizer.RecognizeTemperature(query, culture),

                       // Datetime recognizer This model will find any Date even if its write in coloquial language
                       DateTimeRecognizer.RecognizeDateTime(query, culture),

                       // PhoneNumber recognizer will find any phone number presented
                       SequenceRecognizer.RecognizePhoneNumber(query, culture),

                       // Add IP recognizer - This recognizer will find any Ipv4/Ipv6 presented
                       SequenceRecognizer.RecognizeIpAddress(query, culture)
                       ));
        }
Esempio n. 6
0
        public Task <object> AnswerAsync(Question question)
        {
            var questionText = question.ToString();
            var results      = NumberWithUnitRecognizer.RecognizeDimension(questionText, Culture.English)
                               .Union(NumberWithUnitRecognizer.RecognizeTemperature(questionText, Culture.English))
                               .ToList();

            // unit conversion - using microsoft recognizers for extraction
            if (results.Count == 2)
            {
                var from = results[0].AttributesStrings();
                var to   = results[1].AttributesStrings();

                if (TryConvert(double.Parse(from["value"]), from["unit"], to["unit"], out var result))
                {
                    return(Task.FromResult <object>(Math.Round(result, 10)));
                }
            }

            // unit conversion - using regex for extraction
            var match = Regex.Match(questionText, @"(?<number>[-\.\d]+) (?<fromUnit>[\w ]*) (?:to|in|as) (?<toUnit>[\w ]*)"); // match "<Number> meters to miles"

            if (match.Success)
            {
                var number   = double.Parse(match.Groups["number"].Value);
                var fromUnit = match.Groups["fromUnit"].Value;
                var toUnit   = match.Groups["toUnit"].Value;

                if (TryConvert(number, fromUnit, toUnit, out var result))
                {
                    return(Task.FromResult <object>(Math.Round(result, 10)));
                }
            }

            throw new Exception("Not matched");
        }
 public void RecognizeDimension(string text, string culture, string scale, string typeName)
 {
     NumberWithUnitRecognizer.RecognizeDimension(text, culture);
 }
Esempio n. 8
0
 protected override List <ModelResult> Recognize(string text, string culture)
 {
     return(NumberWithUnitRecognizer.RecognizeDimension(text, culture));
 }
Esempio n. 9
0
        private void OutputButton_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();

            string query = InputField.Text;
            string SelectedRecognizer = RecognizerField.Text;
            string culture            = LanguageToCode(LanguageField.Text);

            if (query != "")
            {
                //----------------------------------------------------------Recognizers
                try
                {
                    if (SelectedRecognizer == "Number Recognizer")
                    {
                        // Number recognizer will find any number from the input
                        // E.g "I have two apples" will return "2".
                        var Recognizer = NumberRecognizer.RecognizeNumber(query, culture);

                        for (int i = 0; i < Recognizer.Count; i++)
                        {
                            string[]     arr = new string[4];
                            ListViewItem itm;
                            //add items to ListView
                            arr[0] = Recognizer[i].Text.ToString();
                            arr[1] = Recognizer[i].Resolution["value"].ToString();
                            arr[2] = Recognizer[i].Start.ToString();
                            arr[3] = Recognizer[i].End.ToString();
                            itm    = new ListViewItem(arr);
                            listView1.Items.Add(itm);
                        }
                    }
                    else if (SelectedRecognizer == "Ordinal Recognizer")
                    {
                        // Ordinal number recognizer will find any ordinal number
                        // E.g "eleventh" will return "11".
                        var Recognizer = NumberRecognizer.RecognizeOrdinal(query, culture);

                        for (int i = 0; i < Recognizer.Count; i++)
                        {
                            string[]     arr = new string[4];
                            ListViewItem itm;
                            //add items to ListView
                            arr[0] = Recognizer[i].Text.ToString();
                            arr[1] = Recognizer[i].Resolution["value"].ToString();
                            arr[2] = Recognizer[i].Start.ToString();
                            arr[3] = Recognizer[i].End.ToString();
                            itm    = new ListViewItem(arr);
                            listView1.Items.Add(itm);
                        }
                    }
                    else if (SelectedRecognizer == "Percentage Recognizer")
                    {
                        // Percentage recognizer will find any number presented as percentage
                        // E.g "one hundred percents" will return "100%"
                        var Recognizer = NumberRecognizer.RecognizePercentage(query, culture);

                        for (int i = 0; i < Recognizer.Count; i++)
                        {
                            string[]     arr = new string[4];
                            ListViewItem itm;
                            //add items to ListView
                            arr[0] = Recognizer[i].Text.ToString();
                            arr[1] = Recognizer[i].Resolution["value"].ToString();
                            arr[2] = Recognizer[i].Start.ToString();
                            arr[3] = Recognizer[i].End.ToString();
                            itm    = new ListViewItem(arr);
                            listView1.Items.Add(itm);
                        }
                    }
                    else if (SelectedRecognizer == "Number Range Recognizer")
                    {
                        // Number Range recognizer will find any cardinal or ordinal number range
                        // E.g. "between 2 and 5" will return "(2,5)"
                        var Recognizer = NumberRecognizer.RecognizeNumberRange(query, culture);

                        for (int i = 0; i < Recognizer.Count; i++)
                        {
                            string[]     arr = new string[4];
                            ListViewItem itm;
                            //add items to ListView
                            arr[0] = Recognizer[i].Text.ToString();
                            arr[1] = Recognizer[i].Resolution["value"].ToString();
                            arr[2] = Recognizer[i].Start.ToString();
                            arr[3] = Recognizer[i].End.ToString();
                            itm    = new ListViewItem(arr);
                            listView1.Items.Add(itm);
                        }
                    }
                    else if (SelectedRecognizer == "Age Recognizer")
                    {
                        // Age recognizer will find any age number presented
                        // E.g "After ninety five years of age, perspectives change" will return "95 Year"
                        var Recognizer = NumberWithUnitRecognizer.RecognizeAge(query, culture);

                        for (int i = 0; i < Recognizer.Count; i++)
                        {
                            string[]     arr = new string[4];
                            ListViewItem itm;
                            //add items to ListView
                            arr[0] = Recognizer[i].Text.ToString();
                            arr[1] = Recognizer[i].Resolution["value"].ToString();
                            arr[2] = Recognizer[i].Start.ToString();
                            arr[3] = Recognizer[i].End.ToString();
                            itm    = new ListViewItem(arr);
                            listView1.Items.Add(itm);
                        }
                    }
                    else if (SelectedRecognizer == "Currency Recognizer")
                    {
                        // Currency recognizer will find any currency presented
                        // E.g "Interest expense in the 1988 third quarter was $ 75.3 million" will return "75300000 Dollar"
                        var Recognizer = NumberWithUnitRecognizer.RecognizeCurrency(query, culture);

                        for (int i = 0; i < Recognizer.Count; i++)
                        {
                            string[]     arr = new string[4];
                            ListViewItem itm;
                            //add items to ListView
                            arr[0] = Recognizer[i].Text.ToString();
                            arr[1] = Recognizer[i].Resolution["value"].ToString();
                            arr[2] = Recognizer[i].Start.ToString();
                            arr[3] = Recognizer[i].End.ToString();
                            itm    = new ListViewItem(arr);
                            listView1.Items.Add(itm);
                        }
                    }
                    else if (SelectedRecognizer == "Dimention Recognizer")
                    {
                        // Dimension recognizer will find any dimension presented
                        // E.g "The six-mile trip to my airport hotel that had taken 20 minutes earlier in the day took more than three hours." will return "6 Mile"
                        var Recognizer = NumberWithUnitRecognizer.RecognizeDimension(query, culture);

                        for (int i = 0; i < Recognizer.Count; i++)
                        {
                            string[]     arr = new string[4];
                            ListViewItem itm;
                            //add items to ListView
                            arr[0] = Recognizer[i].Text.ToString();
                            arr[1] = Recognizer[i].Resolution["value"].ToString();
                            arr[2] = Recognizer[i].Start.ToString();
                            arr[3] = Recognizer[i].End.ToString();
                            itm    = new ListViewItem(arr);
                            listView1.Items.Add(itm);
                        }
                    }
                    else if (SelectedRecognizer == "Temperature Recognizer")
                    {
                        // Temperature recognizer will find any temperature presented
                        // E.g "Set the temperature to 30 degrees celsius" will return "30 C"
                        var Recognizer = NumberWithUnitRecognizer.RecognizeTemperature(query, culture);
                        for (int i = 0; i < Recognizer.Count; i++)
                        {
                            string[]     arr = new string[4];
                            ListViewItem itm;
                            //add items to ListView
                            arr[0] = Recognizer[i].Text.ToString();
                            arr[1] = Recognizer[i].Resolution["value"].ToString();
                            arr[2] = Recognizer[i].Start.ToString();
                            arr[3] = Recognizer[i].End.ToString();
                            itm    = new ListViewItem(arr);
                            listView1.Items.Add(itm);
                        }
                    }
                    else if (SelectedRecognizer == "Date And Time Recognizer")
                    {
                        // Datetime recognizer This model will find any Date even if its write in coloquial language
                        // E.g "I'll go back 8pm today" will return "2017-10-04 20:00:00"
                        var Recognizer = DateTimeRecognizer.RecognizeDateTime(query, culture);

                        for (int i = 0; i < Recognizer.Count; i++)
                        {
                            string[]     arr = new string[4];
                            ListViewItem itm;
                            //add items to ListView
                            arr[0] = Recognizer[i].Text.ToString();
                            arr[1] = Recognizer[i].Resolution["value"].ToString();
                            arr[2] = Recognizer[i].Start.ToString();
                            arr[3] = Recognizer[i].End.ToString();
                            itm    = new ListViewItem(arr);
                            listView1.Items.Add(itm);
                        }
                    }
                    else if (SelectedRecognizer == "Phone Number Recognizer")
                    {
                        // PhoneNumber recognizer will find any phone number presented
                        // E.g "My phone number is ( 19 ) 38294427."
                        var Recognizer = SequenceRecognizer.RecognizePhoneNumber(query, culture);

                        for (int i = 0; i < Recognizer.Count; i++)
                        {
                            string[]     arr = new string[4];
                            ListViewItem itm;
                            //add items to ListView
                            arr[0] = Recognizer[i].Text.ToString();
                            arr[1] = Recognizer[i].Resolution["value"].ToString();
                            arr[2] = Recognizer[i].Start.ToString();
                            arr[3] = Recognizer[i].End.ToString();
                            itm    = new ListViewItem(arr);
                            listView1.Items.Add(itm);
                        }
                    }
                    else if (SelectedRecognizer == "Ip Address Recognizer")
                    {
                        //IP recognizer will find any Ipv4/Ipv6 presented
                        // E.g "My Ip is 8.8.8.8"
                        var Recognizer = SequenceRecognizer.RecognizeIpAddress(query, culture);

                        for (int i = 0; i < Recognizer.Count; i++)
                        {
                            string[]     arr = new string[4];
                            ListViewItem itm;
                            //add items to ListView
                            arr[0] = Recognizer[i].Text.ToString();
                            arr[1] = Recognizer[i].Resolution["value"].ToString();
                            arr[2] = Recognizer[i].Start.ToString();
                            arr[3] = Recognizer[i].End.ToString();
                            itm    = new ListViewItem(arr);
                            listView1.Items.Add(itm);
                        }
                    }
                    else if (SelectedRecognizer == "Choice Recognizer")
                    {
                        //Boolean recognizer will find yes/no like responses, including emoji -
                        // E.g "yup, I need that" will return "True"
                        var Recognizer = ChoiceRecognizer.RecognizeBoolean(query, culture);

                        for (int i = 0; i < Recognizer.Count; i++)
                        {
                            string[]     arr = new string[4];
                            ListViewItem itm;
                            //add items to ListView
                            arr[0] = Recognizer[i].Text.ToString();
                            arr[1] = Recognizer[i].Resolution["value"].ToString();
                            arr[2] = Recognizer[i].Start.ToString();
                            arr[3] = Recognizer[i].End.ToString();
                            itm    = new ListViewItem(arr);
                            listView1.Items.Add(itm);
                        }
                    }
                }
                catch (Exception error)
                {
                    MessageBox.Show("Unable to Recognize\n" + "Exception: " + error.Message.ToString());
                }
            }
            //----------------------------------------------------------Recognizers End
        }
Esempio n. 10
0
        protected override Task <InputState> OnRecognizeInputAsync(DialogContext dc, CancellationToken cancellationToken)
        {
            var validateText = dc.State.GetValue <object>(VALUE_PROPERTY);

            if (!(validateText is string))
            {
                return(Task.FromResult(InputState.Invalid));
            }

            var culture = GetCulture(dc);
            List <ModelResult> results;

            var message = validateText.ToString();

            switch (NumberUnit.GetValue(dc.State))
            {
            case NumberWithUnitInputType.Temperature:
                results = NumberWithUnitRecognizer.RecognizeTemperature(message, culture);
                break;

            case NumberWithUnitInputType.Dimension:
                results = NumberWithUnitRecognizer.RecognizeDimension(message, culture);
                break;

            case NumberWithUnitInputType.Currency:
                results = NumberWithUnitRecognizer.RecognizeCurrency(message, culture);
                break;

            case NumberWithUnitInputType.Age:
                results = NumberWithUnitRecognizer.RecognizeAge(message, culture);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            if (results == null || results.Count <= 0)
            {
                return(Task.FromResult(InputState.Unrecognized));
            }

            var resolvedUnit  = results[0].Resolution["unit"].ToString();
            var resolvedValue = results[0].Resolution["value"].ToString();

            if (!double.TryParse(resolvedValue, out var value))
            {
                return(Task.FromResult(InputState.Valid));
            }

            var result = new
            {
                Unit  = resolvedUnit,
                Value = value,
            };

            if (ResultProperty != null)
            {
                dc.State.SetValue(ResultProperty.GetValue(dc.State), result);
            }

            return(Task.FromResult(InputState.Valid));
        }