public void TestDateTime_Set() { var model = DateTimeRecognizer.GetInstance().GetDateTimeModel(Culture.Chinese);; var reference = new DateObject(2017, 3, 22); BasicTest(model, reference, "事件 每天都发生", Constants.SYS_DATETIME_SET, "每天", "P1D"); BasicTest(model, reference, "事件每日都发生", Constants.SYS_DATETIME_SET, "每日", "P1D"); BasicTest(model, reference, "事件每周都发生", Constants.SYS_DATETIME_SET, "每周", "P1W"); BasicTest(model, reference, "事件每个星期都发生", Constants.SYS_DATETIME_SET, "每个星期", "P1W"); BasicTest(model, reference, "事件每个月都发生", Constants.SYS_DATETIME_SET, "每个月", "P1M"); BasicTest(model, reference, "事件每年都发生", Constants.SYS_DATETIME_SET, "每年", "P1Y"); BasicTest(model, reference, "事件每周一都发生", Constants.SYS_DATETIME_SET, "每周一", "XXXX-WXX-1"); BasicTest(model, reference, "事件每周一下午八点都发生", Constants.SYS_DATETIME_SET, "每周一下午八点", "XXXX-WXX-1T20"); }
public void TestDateTime_DateTime() { var model = DateTimeRecognizer.GetInstance().GetDateTimeModel(Culture.Chinese); var reference = new DateObject(2016, 11, 7, 14, 7, 0); BasicTest(model, reference, "2010.01.29晚上六点", Constants.SYS_DATETIME_DATETIME, "2010.01.29晚上六点", "2010-01-29T18"); BasicTest(model, reference, "1987年1月11日八点", Constants.SYS_DATETIME_DATETIME, "1987年1月11日八点", "1987-01-11T08"); BasicTest(model, reference, "2015年十月初一早上九点二十", Constants.SYS_DATETIME_DATETIME, "2015年十月初一早上九点二十", "2015-10-01T09:20"); BasicTest(model, reference, "1月19号下午5:00", Constants.SYS_DATETIME_DATETIME, "1月19号下午5:00", "XXXX-01-19T17:00"); BasicTest(model, reference, "明天下午5:00", Constants.SYS_DATETIME_DATETIME, "明天下午5:00", "2016-11-08T17:00"); BasicTest(model, reference, "今晚6点", Constants.SYS_DATETIME_DATETIME, "今晚6点", "2016-11-07T18"); BasicTest(model, reference, "今晨5点", Constants.SYS_DATETIME_DATETIME, "今晨5点", "2016-11-07T05"); BasicTest(model, reference, "今早8点十五分", Constants.SYS_DATETIME_DATETIME, "今早8点十五分", "2016-11-07T08:15"); }
public void TestDateTime_DateTimePeriod() { var model = DateTimeRecognizer.GetInstance().GetDateTimeModel(Culture.Chinese); var reference = new DateObject(2016, 11, 7, 16, 12, 0); BasicTest(model, reference, "从昨天下午两点到明天四点", Constants.SYS_DATETIME_DATETIMEPERIOD, "从昨天下午两点到明天四点", "(2016-11-06T14:00:00,2016-11-08T04:00:00,PT38H)"); BasicTest(model, reference, "从昨天5:00-6:00", Constants.SYS_DATETIME_DATETIMEPERIOD, "从昨天5:00-6:00", "(2016-11-06T05:00,2016-11-06T06:00,PT1H)"); BasicTest(model, reference, "1月15号4点和2月3号9点之间", Constants.SYS_DATETIME_DATETIMEPERIOD, "1月15号4点和2月3号9点之间", "(XXXX-01-15T04,XXXX-02-03T09,PT461H)"); BasicTest(model, reference, "昨晚", Constants.SYS_DATETIME_DATETIMEPERIOD, "昨晚", "2016-11-06TEV"); BasicTest(model, reference, "明天上午", Constants.SYS_DATETIME_DATETIMEPERIOD, "明天上午", "2016-11-08TMO"); BasicTest(model, reference, "上个小时", Constants.SYS_DATETIME_DATETIMEPERIOD, "上个小时", "(2016-11-07T15:12:00,2016-11-07T16:12:00,PT1H)"); BasicTest(model, reference, "之后5分钟", Constants.SYS_DATETIME_DATETIMEPERIOD, "之后5分钟", "(2016-11-07T16:12:00,2016-11-07T16:17:00,PT5M)"); BasicTest(model, reference, "之前3小时", Constants.SYS_DATETIME_DATETIMEPERIOD, "之前3小时", "(2016-11-07T13:12:00,2016-11-07T16:12:00,PT3H)"); }
/// <summary> /// Get all recognizers model instances. /// </summary> /// <returns>A list of all the existing recognizer's models</returns> private static IEnumerable <IModel> GetModels() { return(new IModel[] { // Add Number recognizer - This recognizer will find any number from the input // E.g "I have two apples" will return "2". NumberRecognizer.Instance.GetNumberModel(defaultCulture), // Add Ordinal number recognizer - This recognizer will find any ordinal number // E.g "eleventh" will return "11". NumberRecognizer.Instance.GetOrdinalModel(defaultCulture), // Add Percentage recognizer - This recognizer will find any number presented as percentage // E.g "one hundred percents" will return "100%" NumberRecognizer.Instance.GetPercentageModel(defaultCulture), // Add Age recognizer - This recognizer will find any age number presented // E.g "After ninety five years of age, perspectives change" will return "95 Year" NumberWithUnitRecognizer.Instance.GetAgeModel(defaultCulture), // Add Currency recognizer - This recognizer will find any currency presented // E.g "Interest expense in the 1988 third quarter was $ 75.3 million" will return "75300000 Dollar" NumberWithUnitRecognizer.Instance.GetCurrencyModel(defaultCulture), // Add Dimension recognizer - This 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.Instance.GetDimensionModel(defaultCulture), // Add Temperature recognizer - This recognizer will find any temperature presented // E.g "Set the temperature to 30 degrees celsius" will return "30 C" NumberWithUnitRecognizer.Instance.GetTemperatureModel(defaultCulture), // Add 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.GetInstance().GetDateTimeModel(defaultCulture) }); }
public void TestDateTime_DatePeriod() { var model = DateTimeRecognizer.GetModel(Culture.English); var reference = new DateObject(2016, 11, 7); BasicTest(model, reference, "I'll be out from 4-23 in next month", Constants.SYS_DATETIME_DATEPERIOD, "from 4-23 in next month", "(2016-12-04,2016-12-23,P19D)"); BasicTest(model, reference, "I'll be out between 3 and 12 of Sept hahaha", Constants.SYS_DATETIME_DATEPERIOD, "between 3 and 12 of sept", "(XXXX-09-03,XXXX-09-12,P9D)"); BasicTest(model, reference, "I'll be out this September", Constants.SYS_DATETIME_DATEPERIOD, "this september", "2016-09"); BasicTest(model, reference, "I'll be out January 12, 2016 - 01/22/2016", Constants.SYS_DATETIME_DATEPERIOD, "january 12, 2016 - 01/22/2016", "(2016-01-12,2016-01-22,P10D)"); BasicTest(model, reference, "I'll be out next 3 days", Constants.SYS_DATETIME_DATEPERIOD, "next 3 days", "(2016-11-08,2016-11-11,P3D)"); BasicTest(model, reference, "I'll be out the last week of july", Constants.SYS_DATETIME_DATEPERIOD, "the last week of july", "XXXX-07-W04"); BasicTest(model, reference, "I'll be out 2015-3", Constants.SYS_DATETIME_DATEPERIOD, "2015-3", "2015-03"); BasicTest(model, reference, "I'll leave this SUMMER", Constants.SYS_DATETIME_DATEPERIOD, "this summer", "2016-SU"); }
public void TestDateTime_Time() { var model = DateTimeRecognizer.GetModel(Culture.English); var reference = new DateObject(2016, 11, 7); BasicTest(model, reference, "I'll be back 7:56:30 pm", Constants.SYS_DATETIME_TIME, "7:56:30 pm", "T19:56:30"); BasicTest(model, reference, "It's half past seven o'clock", Constants.SYS_DATETIME_TIME, "half past seven o'clock", "T07:30"); BasicTest(model, reference, "It's 20 min past eight in the evening", Constants.SYS_DATETIME_TIME, "20 min past eight in the evening", "T20:20"); BasicTest(model, reference, "I'll be back in the morning at 7", Constants.SYS_DATETIME_TIME, "in the morning at 7", "T07"); BasicTest(model, reference, "I'll be back in the afternoon at 7", Constants.SYS_DATETIME_TIME, "in the afternoon at 7", "T19"); BasicTest(model, reference, "I'll be back noonish", Constants.SYS_DATETIME_TIME, "noonish", "T12"); BasicTest(model, reference, "I'll be back 11ish", Constants.SYS_DATETIME_TIME, "11ish", "T11"); BasicTest(model, reference, "I'll be back 1140 a.m.", Constants.SYS_DATETIME_TIME, "1140 a.m.", "T11:40"); }
public void TestDateTime_TimePeriod() { var model = DateTimeRecognizer.GetModel(Culture.English); var reference = new DateObject(2016, 11, 7, 16, 12, 0); BasicTest(model, reference, "I'll be out 5 to 6pm", Constants.SYS_DATETIME_TIMEPERIOD, "5 to 6pm", "(T17,T18,PT1H)"); BasicTest(model, reference, "I'll be out 5 to seven in the morning", Constants.SYS_DATETIME_TIMEPERIOD, "5 to seven in the morning", "(T05,T07,PT2H)"); BasicTest(model, reference, "I'll be out between 5 and 6 in the afternoon", Constants.SYS_DATETIME_TIMEPERIOD, "between 5 and 6 in the afternoon", "(T17,T18,PT1H)"); BasicTest(model, reference, "I'll be out 4:00 to 7 oclock", Constants.SYS_DATETIME_TIMEPERIOD, "4:00 to 7 oclock", "(T04:00,T07,PT3H)"); BasicTest(model, reference, "I'll be out from 3 in the morning until 5pm", Constants.SYS_DATETIME_TIMEPERIOD, "from 3 in the morning until 5pm", "(T03,T17,PT14H)"); BasicTest(model, reference, "I'll be out between 4pm and 5pm", Constants.SYS_DATETIME_TIMEPERIOD, "between 4pm and 5pm", "(T16,T17,PT1H)"); BasicTest(model, reference, "let's meet in the morning", Constants.SYS_DATETIME_TIMEPERIOD, "morning", "TMO"); BasicTest(model, reference, "let's meet in the evening", Constants.SYS_DATETIME_TIMEPERIOD, "evening", "TEV"); }
public void TestDateTime_Duration() { var model = DateTimeRecognizer.GetModel(Culture.English); var reference = new DateObject(2016, 11, 7); BasicTest(model, reference, "I'll leave for 3h", Constants.SYS_DATETIME_DURATION, "3h", "PT3H"); BasicTest(model, reference, "I'll leave for 3.5years", Constants.SYS_DATETIME_DURATION, "3.5years", "P3.5Y"); BasicTest(model, reference, "I'll leave for 3 minutes", Constants.SYS_DATETIME_DURATION, "3 minutes", "PT3M"); BasicTest(model, reference, "I'll leave for 123.45 sec", Constants.SYS_DATETIME_DURATION, "123.45 sec", "PT123.45S"); BasicTest(model, reference, "I'll leave for all day", Constants.SYS_DATETIME_DURATION, "all day", "P1D"); BasicTest(model, reference, "I'll leave for twenty and four hours", Constants.SYS_DATETIME_DURATION, "twenty and four hours", "PT24H"); BasicTest(model, reference, "I'll leave for all month", Constants.SYS_DATETIME_DURATION, "all month", "P1M"); BasicTest(model, reference, "I'll leave for an hour", Constants.SYS_DATETIME_DURATION, "an hour", "PT1H"); }
public void TestDateTime_Set() { var model = DateTimeRecognizer.GetModel(Culture.English); var reference = new DateObject(2016, 11, 7); BasicTest(model, reference, "I'll leave weekly", Constants.SYS_DATETIME_SET, "weekly", "P1W"); BasicTest(model, reference, "I'll leave every day", Constants.SYS_DATETIME_SET, "every day", "P1D"); BasicTest(model, reference, "I'll leave annually", Constants.SYS_DATETIME_SET, "annually", "P1Y"); BasicTest(model, reference, "I'll leave each two days", Constants.SYS_DATETIME_SET, "each two days", "P2D"); BasicTest(model, reference, "I'll leave every three week", Constants.SYS_DATETIME_SET, "every three week", "P3W"); BasicTest(model, reference, "I'll leave 3pm each day", Constants.SYS_DATETIME_SET, "3pm each day", "T15"); BasicTest(model, reference, "I'll leave every monday", Constants.SYS_DATETIME_SET, "every monday", "XXXX-WXX-1"); BasicTest(model, reference, "I'll leave each monday at 4pm", Constants.SYS_DATETIME_SET, "each monday at 4pm", "XXXX-WXX-1T16"); }
public void TestDateTime_DateTime() { var model = DateTimeRecognizer.GetModel(Culture.English); var reference = new DateObject(2016, 11, 7); BasicTest(model, reference, "I'll go back now", Constants.SYS_DATETIME_DATETIME, "now", "PRESENT_REF"); BasicTest(model, reference, "I'll go back October 14 for 8:00:31am", Constants.SYS_DATETIME_DATETIME, "october 14 for 8:00:31am", "XXXX-10-14T08:00:31"); BasicTest(model, reference, "I'll go back tomorrow 8:00am", Constants.SYS_DATETIME_DATETIME, "tomorrow 8:00am", "2016-11-08T08:00"); BasicTest(model, reference, "I'll go back 10, tonight", Constants.SYS_DATETIME_DATETIME, "10, tonight", "2016-11-07T22"); BasicTest(model, reference, "I'll go back 8am this morning", Constants.SYS_DATETIME_DATETIME, "8am this morning", "2016-11-07T08"); BasicTest(model, reference, "I'll go back end of tomorrow", Constants.SYS_DATETIME_DATETIME, "end of tomorrow", "2016-11-08T23:59"); BasicTest(model, reference, "I'll go back end of the sunday", Constants.SYS_DATETIME_DATETIME, "end of the sunday", "XXXX-WXX-7T23:59"); BasicTest(model, reference, "I'll go back end of this sunday", Constants.SYS_DATETIME_DATETIME, "end of this sunday", "2016-11-13T23:59"); }
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) )); }
public void TestDateTime_DateTimePeriod() { var model = DateTimeRecognizer.GetInstance().GetDateTimeModel(Culture.English); var reference = new DateObject(2016, 11, 7, 16, 12, 0); BasicTest(model, reference, "I'll be out five to seven today", Constants.SYS_DATETIME_DATETIMEPERIOD, "five to seven today", "(2016-11-07T05,2016-11-07T07,PT2H)"); BasicTest(model, reference, "I'll be out from 5 to 6pm of April 22", Constants.SYS_DATETIME_DATETIMEPERIOD, "from 5 to 6pm of april 22", "(XXXX-04-22T17,XXXX-04-22T18,PT1H)"); BasicTest(model, reference, "I'll be out 3:00 to 4:00 tomorrow", Constants.SYS_DATETIME_DATETIMEPERIOD, "3:00 to 4:00 tomorrow", "(2016-11-08T03:00,2016-11-08T04:00,PT1H)"); BasicTest(model, reference, "I'll go back this evening", Constants.SYS_DATETIME_DATETIMEPERIOD, "this evening", "2016-11-07TEV"); BasicTest(model, reference, "I'll go back tomorrow night", Constants.SYS_DATETIME_DATETIMEPERIOD, "tomorrow night", "2016-11-08TNI"); BasicTest(model, reference, "I'll go back next monday afternoon", Constants.SYS_DATETIME_DATETIMEPERIOD, "next monday afternoon", "2016-11-14TAF"); BasicTest(model, reference, "I'll go back next hour", Constants.SYS_DATETIME_DATETIMEPERIOD, "next hour", "(2016-11-07T16:12:00,2016-11-07T17:12:00,PT1H)"); BasicTest(model, reference, "I'll go back tuesday in the morning", Constants.SYS_DATETIME_DATETIMEPERIOD, "tuesday in the morning", "XXXX-WXX-2TMO"); }
public void TestDateTime_Time() { var model = DateTimeRecognizer.GetInstance().GetDateTimeModel(Culture.Chinese); var reference = new DateObject(2017, 3, 22); BasicTest(model, reference, "下午5:00", Constants.SYS_DATETIME_TIME, "下午5:00", "T17:00"); BasicTest(model, reference, "晚上9:30", Constants.SYS_DATETIME_TIME, "晚上9:30", "T21:30"); BasicTest(model, reference, "晚上19:30", Constants.SYS_DATETIME_TIME, "晚上19:30", "T19:30"); BasicTest(model, reference, "大约十点", Constants.SYS_DATETIME_TIME, "大约十点", "T10"); BasicTest(model, reference, "大约晚上十点", Constants.SYS_DATETIME_TIME, "大约晚上十点", "T22"); BasicTest(model, reference, "凌晨2点半", Constants.SYS_DATETIME_TIME, "凌晨2点半", "T02:30"); BasicTest(model, reference, "零点", Constants.SYS_DATETIME_TIME, "零点", "T00"); BasicTest(model, reference, "零点整", Constants.SYS_DATETIME_TIME, "零点整", "T00"); }
public void TestDateTime_Date() { var model = DateTimeRecognizer.GetModel(Culture.English); var reference = new DateObject(2016, 11, 7); BasicTest(model, reference, "I'll go back Oct/2", Constants.SYS_DATETIME_DATE, "oct/2", "XXXX-10-02", "2017-10-02", "2016-10-02"); BasicTest(model, reference, "I'll go back on 22/04", Constants.SYS_DATETIME_DATE, "22/04", "XXXX-04-22", "2017-04-22", "2016-04-22"); BasicTest(model, reference, "I'll go back May twenty nine", Constants.SYS_DATETIME_DATE, "may twenty nine", "XXXX-05-29", "2017-05-29", "2016-05-29"); BasicTest(model, reference, "I'll go back second of Aug.", Constants.SYS_DATETIME_DATE, "second of aug", "XXXX-08-02", "2017-08-02", "2016-08-02"); BasicTest(model, reference, "I'll go back today", Constants.SYS_DATETIME_DATE, "today", "2016-11-07"); BasicTest(model, reference, "I'll go back tomorrow", Constants.SYS_DATETIME_DATE, "tomorrow", "2016-11-08"); BasicTest(model, reference, "I'll go back yesterday", Constants.SYS_DATETIME_DATE, "yesterday", "2016-11-06"); BasicTest(model, reference, "I'll go back on Friday", Constants.SYS_DATETIME_DATE, "friday", "XXXX-WXX-5", "2016-11-11", "2016-11-04"); }
public static IModel GetModel(this TestContext context) { var language = TestUtils.GetCulture(context.FullyQualifiedTestClassName); var modelName = TestUtils.GetModel(context.TestName); switch (modelName) { case Models.Number: return(NumberRecognizer.Instance.GetNumberModel(language)); case Models.Ordinal: return(NumberRecognizer.Instance.GetOrdinalModel(language)); case Models.Percent: return(NumberRecognizer.Instance.GetPercentageModel(language)); case Models.Age: return(NumberWithUnitRecognizer.Instance.GetAgeModel(language)); case Models.Currency: return(NumberWithUnitRecognizer.Instance.GetCurrencyModel(language)); case Models.Dimension: return(NumberWithUnitRecognizer.Instance.GetDimensionModel(language)); case Models.Temperature: return(NumberWithUnitRecognizer.Instance.GetTemperatureModel(language)); case Models.DateTime: return(DateTimeRecognizer.GetInstance(DateTimeOptions.None).GetDateTimeModel(language)); case Models.CustomNumber: return(GetCustomModelFor(language)); } throw new Exception($"Model '{modelName}' for '{language}' not supported"); }
public void TestDateTime_TimePeriod() { var model = DateTimeRecognizer.GetInstance().GetDateTimeModel(Culture.Chinese); var reference = new DateObject(2017, 3, 22); BasicTest(model, reference, "从下午五点一刻到六点", Constants.SYS_DATETIME_TIMEPERIOD, "从下午五点一刻到六点", "(T17:15,T18,PT0H45M)"); BasicTest(model, reference, "17:55:23-18:33:02", Constants.SYS_DATETIME_TIMEPERIOD, "17:55:23-18:33:02", "(T17:55:23,T18:33:02,PT0H37M39S)"); BasicTest(model, reference, "从17点55分23秒至18点33分02秒", Constants.SYS_DATETIME_TIMEPERIOD, "从17点55分23秒至18点33分02秒", "(T17:55:23,T18:33:02,PT0H37M39S)"); BasicTest(model, reference, "早上五到六点", Constants.SYS_DATETIME_TIMEPERIOD, "早上五到六点", "(T05,T06,PT1H)"); BasicTest(model, reference, "下午五点到晚上七点半", Constants.SYS_DATETIME_TIMEPERIOD, "下午五点到晚上七点半", "(T17,T19:30,PT2H30M)"); BasicTest(model, reference, "下午5:00到凌晨3:00", Constants.SYS_DATETIME_TIMEPERIOD, "下午5:00到凌晨3:00", "(T17:00,T03:00,PT10H)"); BasicTest(model, reference, "下午5:00到6:00", Constants.SYS_DATETIME_TIMEPERIOD, "下午5:00到6:00", "(T17:00,T18:00,PT1H)"); BasicTest(model, reference, "5:00到6:00", Constants.SYS_DATETIME_TIMEPERIOD, "5:00到6:00", "(T05:00,T06:00,PT1H)"); }
public void TestDateTime_Date() { var model = DateTimeRecognizer.GetInstance().GetDateTimeModel(Culture.Chinese); var reference = new DateObject(2017, 3, 22); BasicTest(model, reference, "2010/01/29", Constants.SYS_DATETIME_DATE, "2010/01/29", "2010-01-29"); BasicTest(model, reference, "农历2015年十月初一", Constants.SYS_DATETIME_DATE, "农历2015年十月初一", "2015-10-01"); BasicTest(model, reference, "正月三十", Constants.SYS_DATETIME_DATE, "正月三十", "XXXX-01-30"); BasicTest(model, reference, "10月12号,星期一", Constants.SYS_DATETIME_DATE, "10月12号,星期一", "XXXX-10-12"); BasicTest(model, reference, "最近", Constants.SYS_DATETIME_DATE, "最近", "2017-03-22"); BasicTest(model, reference, "12号", Constants.SYS_DATETIME_DATE, "12号", "XXXX-XX-12"); BasicTest(model, reference, "二零零四年八月十五", Constants.SYS_DATETIME_DATE, "二零零四年八月十五", "2004-08-15"); BasicTest(model, reference, "本月十日", Constants.SYS_DATETIME_DATE, "本月十日", "XXXX-03-10"); }
public void TestDateTime_DatePeriod() { var model = DateTimeRecognizer.GetInstance().GetDateTimeModel(Culture.Chinese); var reference = new DateObject(2017, 3, 22); BasicTest(model, reference, "时间从一月十日到十二日", Constants.SYS_DATETIME_DATEPERIOD, "从一月十日到十二日", "(XXXX-01-10,XXXX-01-12,P2D)"); BasicTest(model, reference, "时间从2016年一月十日到十二日", Constants.SYS_DATETIME_DATEPERIOD, "从2016年一月十日到十二日", "(2016-01-10,2016-01-12,P2D)"); BasicTest(model, reference, "从一月十日到20日", Constants.SYS_DATETIME_DATEPERIOD, "从一月十日到20日", "(XXXX-01-10,XXXX-01-20,P10D)"); BasicTest(model, reference, "明年", Constants.SYS_DATETIME_DATEPERIOD, "明年", "2018"); BasicTest(model, reference, "十月的第一周", Constants.SYS_DATETIME_DATEPERIOD, "十月的第一周", "XXXX-10-W01"); BasicTest(model, reference, "前1周", Constants.SYS_DATETIME_DATEPERIOD, "前1周", "(2017-03-15,2017-03-22,P1W)"); BasicTest(model, reference, "后1年", Constants.SYS_DATETIME_DATEPERIOD, "后1年", "(2017-03-23,2018-03-23,P1Y)"); BasicTest(model, reference, "今年夏天", Constants.SYS_DATETIME_DATEPERIOD, "今年夏天", "2017-SU"); }
private static bool GetDateOrTimeFromUserInput(string userInput, UserDateTimeValueOptions requestedValue, out DateTime userRequestedDateOrTime) { userRequestedDateOrTime = default(DateTime); var managedToRecognize = false; string requestedValueKey = GetRecognitionKeyBasedOnRequestedValue(requestedValue); var culture = Culture.English; var dateTimeRecognitionResult = DateTimeRecognizer.RecognizeDateTime(userInput, culture); if (dateTimeRecognitionResult.Any(entity => entity.TypeName == requestedValueKey)) { var recognizedDateResolution = dateTimeRecognitionResult .First(entity => entity.TypeName == requestedValueKey) .Resolution["values"] as List <Dictionary <string, string> >; DateTime dateTimeDump = default(DateTime); if (recognizedDateResolution.Any(foundDate => DateTime.TryParse(foundDate["value"], out dateTimeDump))) { managedToRecognize = true; userRequestedDateOrTime = dateTimeDump; } } return(managedToRecognize); }
public static void TimeRange() { // Run the recognizer. var results = DateTimeRecognizer.RecognizeDateTime("Some time between 6pm and 6:30pm.", Culture.English); // We should find a single result in this example. foreach (var result in results) { // The resolution includes a single value because there is no ambiguity. var distinctTimexExpressions = new HashSet <string>(); var values = (List <Dictionary <string, string> >)result.Resolution["values"]; foreach (var value in values) { // We are interested in the distinct set of TIMEX expressions. if (value.TryGetValue("timex", out var timex)) { distinctTimexExpressions.Add(timex); } } // The TIMEX expression can also capture the notion of range. Console.WriteLine($"{result.Text} ( {string.Join(',', distinctTimexExpressions)} )"); } }
protected async Task DigestLuisResult(DialogContext dc, ReservationLuis luisResult) { try { var state = await ConversationStateAccessor.GetAsync(dc.Context); // Extract entities and store in state here. if (luisResult != null) { var entities = luisResult.Entities; // Extract the cuisines out (already normalized to canonical form) and put in State thus slot-filling for the dialog. if (entities.cuisine != null) { foreach (var cuisine in entities.cuisine) { var type = cuisine.First <string>(); state.Booking.Category = type; } } if (entities.datetime != null) { var results = DateTimeRecognizer.RecognizeDateTime(dc.Context.Activity.Text, CultureInfo.CurrentUICulture.ToString()); if (results.Count > 0) { // We only care about presence of one DateTime var result = results.First(); // The resolution could include two example values: one for AM and one for PM. var distinctTimexExpressions = new HashSet <string>(); var values = (List <Dictionary <string, string> >)result.Resolution["values"]; foreach (var value in values) { // Each result includes a TIMEX expression that captures the inherent date but not time ambiguity. // We are interested in the distinct set of TIMEX expressions. if (value.TryGetValue("timex", out var timex)) { distinctTimexExpressions.Add(timex); } } // Now we have the timex properties let's see if we have a definite date and time // If so we slot-fill this and move on, if we don't we'll ignore for now meaning the user will be prompted var timexProperty = new TimexProperty(distinctTimexExpressions.First()); if (timexProperty.Types.Contains(Constants.TimexTypes.Date) && timexProperty.Types.Contains(Constants.TimexTypes.Definite)) { // We have definite date (no ambiguity) state.Booking.ReservationDate = new DateTime(timexProperty.Year.Value, timexProperty.Month.Value, timexProperty.DayOfMonth.Value); // Timex doesn't capture time ambiguity (e.g. 4 rather than 4pm) if (timexProperty.Types.Contains(Constants.TimexTypes.Time)) { // If we have multiple TimeX if (distinctTimexExpressions.Count == 1) { // We have definite time (no ambiguity) state.Booking.ReservationTime = DateTime.Parse($"{timexProperty.Hour.Value}:{timexProperty.Minute.Value}:{timexProperty.Second.Value}"); } else { // We don't have a distinct time so add the TimeEx expressions to enable disambiguation later and prepare the natural language versions foreach (var timex in distinctTimexExpressions) { var property = new TimexProperty(timex); state.AmbiguousTimexExpressions.Add(timex, property.ToNaturalLanguage(DateTime.Now)); } } } } else if (timexProperty.Types.Contains(Constants.TimexTypes.Time)) { // We might have a time but no date (e.g. book a table for 4pm) // If we have multiple timex (and time) this means we have a AM and PM component (e.g. ambiguous - book a table at 9) if (distinctTimexExpressions.Count == 1) { state.Booking.ReservationTime = DateTime.Parse($"{timexProperty.Hour.Value}:{timexProperty.Minute.Value}:{timexProperty.Second.Value}"); } } else { // We don't have a distinct time so add the TimeEx expressions to enable disambiguation later and prepare the natural language versions foreach (var timex in distinctTimexExpressions) { var property = new TimexProperty(timex); state.AmbiguousTimexExpressions.Add(timex, property.ToNaturalLanguage(DateTime.Now)); } } } } if (entities.geographyV2 != null) { state.Booking.Location = entities.geographyV2.First().Location; } // Establishing attendee count can be problematic as the number entity can be picked up for poorly qualified // times, e.g. book a restaurant tomorrow at 2 for 4 people so we rely on a composite entity if (entities.attendees != null) { var attendeesComposite = entities.attendees.First(); if (attendeesComposite != null) { int.TryParse(attendeesComposite.number.First().ToString(), out var attendeeCount); if (attendeeCount > 0) { state.Booking.AttendeeCount = attendeeCount; } } } } } catch { // put log here } }
/// <summary> /// Parse query with all recognizers. /// </summary> private static IEnumerable <ModelResult> ParseAll(string query, string culture) { return(MergeResults(new List <ModelResult>[] { // 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 colloquial 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), // Mention recognizer will find all the mention usages // E.g "@Cicero" SequenceRecognizer.RecognizeMention(query, culture), // Hashtag recognizer will find all the hash tag usages // E.g "task #123" SequenceRecognizer.RecognizeHashtag(query, culture), // Email recognizer will find all the emails // E.g "*****@*****.**" SequenceRecognizer.RecognizeEmail(query, culture), // URL recognizer will find all the urls // E.g "bing.com" SequenceRecognizer.RecognizeURL(query, culture), // GUID recognizer will find all the GUID usages // E.g "{123e4567-e89b-12d3-a456-426655440000}" SequenceRecognizer.RecognizeGUID(query, culture), // Quoted text recognizer // E.g "I meant "no"" SequenceRecognizer.RecognizeQuotedText(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), })); }
public void WithInvalidCultureAsTargetAndWithoutFallback_ThrowError() { var recognizer = new DateTimeRecognizer(InvalidCulture); Assert.ThrowsException <ArgumentException>(() => recognizer.GetDateTimeModel(fallbackToDefaultCulture: false)); }
private static void ParseEventInputFlags(string[] args, ref Event evt, string timezone) { uint i = 0; int argsLength = args.Length; while (i < argsLength) { if (args[i].StartsWith("--")) { var key = args[i].Substring(2); i++; var values = new List <string>(); while (i < argsLength && !args[i].StartsWith("--")) { values.Add(args[i]); i++; } switch (key) { case "repeat": if (values.Count == 0) { evt.Repeat = RepeatType.None; } else { switch (values[0]) { case "d": evt.Repeat = RepeatType.Daily; break; case "w": evt.Repeat = RepeatType.Weekly; break; case "m": evt.Repeat = RepeatType.Monthly; break; case "mw": evt.Repeat = RepeatType.MonthlyWeekday; break; case "n": evt.Repeat = RepeatType.None; break; default: if (evt.Repeat != RepeatType.Daily && evt.Repeat != RepeatType.Weekly && evt.Repeat != RepeatType.Monthly && evt.Repeat != RepeatType.MonthlyWeekday) { evt.Repeat = RepeatType.None; } break; } } break; case "desc": evt.Description = string.Join(' ', values.ToArray()); break; case "mention": evt.Mentions = new List <EventMention>(); foreach (var value in values) { if (value == "@everyone") { evt.Mentions.Clear(); evt.Mentions.Add(new EventMention { Type = MentionType.Everyone }); break; } else if (value.ToLower() == "rsvp") { evt.Mentions.Add(new EventMention { Type = MentionType.RSVP }); } else if (value.StartsWith("<@") && value.EndsWith(">")) { var mention = new EventMention(); string mentionIdString; if (value.StartsWith("<@&")) { mention.Type = MentionType.Role; mentionIdString = value.Substring(3); } else { mention.Type = MentionType.User; if (value.StartsWith("<@!")) { mentionIdString = value.Substring(3); } else { mentionIdString = value.Substring(2); } } mentionIdString = mentionIdString.TrimEnd('>'); mention.TargetId = ulong.Parse(mentionIdString); evt.Mentions.Add(mention); } } break; case "remind": var reminderString = string.Join(' ', values); var tz = DateTimeZoneProviders.Tzdb.GetZoneOrNull(timezone); if (tz == null) { throw new InvalidTimeZoneException(); } var results = DateTimeRecognizer.RecognizeDateTime(reminderString, Culture.English); if (results.Count > 0 && results.First().TypeName.StartsWith("datetimeV2")) { var first = results.First(); var resolutionValues = (IList <Dictionary <string, string> >)first.Resolution["values"]; var subType = first.TypeName.Split('.').Last(); if (subType == "duration") { string value = resolutionValues.Select(v => v["value"]).FirstOrDefault(); double seconds = double.Parse(value); var reminderTimestamp = evt.StartTimestamp.AddSeconds(-seconds); if (IsFuture(reminderTimestamp)) { evt.ReminderTimestamp = reminderTimestamp; } else { throw new DateTimeInPastException(); } } } break; default: break; } } else { i++; } } }
/// <summary> /// Extract date and time from a sentence using Microsoft Recognizer /// </summary> /// <param name="message">input user message</param> /// <param name="fromLocale">Source Locale</param> /// <returns></returns> private List <TextAndDateTime> ExtractDate(string message, string fromLocale) { List <TextAndDateTime> fndDates = new List <TextAndDateTime>(); //extract culture name. var cultureName = FindCulture(fromLocale); var results = DateTimeRecognizer.RecognizeDateTime(message, cultureName); //looping on each result and extracting found date objects from input utterance foreach (ModelResult result in results) { var resolutionValues = (IList <Dictionary <string, string> >)result.Resolution["values"]; string type = result.TypeName.Replace("datetimeV2.", ""); DateTime moment;; string momentType; DateTime momentEnd; TextAndDateTime curDateTimeText; if (type.Contains("range")) { if (type.Contains("date") && type.Contains("time")) { momentType = "datetime"; } else if (type.Contains("date")) { momentType = "date"; } else { momentType = "time"; } moment = DateTime.Parse(resolutionValues.First()["start"]); momentEnd = DateTime.Parse(resolutionValues.First()["end"]); curDateTimeText = new TextAndDateTime { dateTime = moment, Text = result.Text, type = momentType, range = true, endDateTime = momentEnd }; } else { if (type.Contains("date") && type.Contains("time")) { momentType = "datetime"; } else if (type.Contains("date")) { momentType = "date"; } else { momentType = "time"; } moment = resolutionValues.Select(v => DateTime.Parse(v["value"])).FirstOrDefault(); curDateTimeText = new TextAndDateTime { dateTime = moment, Text = result.Text, type = momentType, range = false, }; } fndDates.Add(curDateTimeText); } return(fndDates); }
private static void ParseEventTimestamps(string bodyString, string timezone, ref Event evt) { var tz = DateTimeZoneProviders.Tzdb.GetZoneOrNull(timezone); if (tz == null) { throw new InvalidTimeZoneException(); } var results = DateTimeRecognizer.RecognizeDateTime(bodyString, Culture.English); if (results.Count > 0 && results.Any(r => r.TypeName.StartsWith("datetimeV2"))) { var first = results .Where(r => r.Resolution != null) .SkipWhile(r => { var v = (IList <Dictionary <string, string> >)r.Resolution["values"]; var returnValue = v.Any(x => { try { return(x["value"] == "not resolved"); } catch (KeyNotFoundException) { return(false); } }); return(returnValue); }) .FirstOrDefault(); if (first == null) { throw new EventParseException(); } var resolutionValues = (IList <Dictionary <string, string> >)first.Resolution["values"]; var subType = first.TypeName.Split('.').Last(); if (subType == "date") { string timex = resolutionValues.Select(v => v["timex"]).FirstOrDefault(); string value = resolutionValues.Select(v => v["value"]).FirstOrDefault(); if (timex.StartsWith("XXXX-")) { value = value.Substring(4); value = string.Format("{0}{1}", DateTime.Now.Year, value); } LocalDateTime startDateTime = _dateOnlyPattern.Parse(value).Value; ZonedDateTime zonedStartDateTime = tz.AtLeniently(startDateTime); if (IsFuture(zonedStartDateTime.ToDateTimeOffset())) { evt.StartTimestamp = zonedStartDateTime.ToDateTimeOffset(); evt.EndTimestamp = zonedStartDateTime.ToDateTimeOffset().AddHours(1); } else { throw new DateTimeInPastException(); } } else if (subType.Contains("date") && !subType.Contains("range")) { string timex = resolutionValues.Select(v => v["timex"]).FirstOrDefault(); string value = resolutionValues.Select(v => v["value"]).FirstOrDefault(); if (timex.StartsWith("XXXX-")) { value = value.Substring(4); value = string.Format("{0}{1}", DateTime.Now.Year, value); } LocalDateTime startDateTime = _dateTimePattern.Parse(value).Value; ZonedDateTime zonedStartDateTime = tz.AtLeniently(startDateTime); if (IsFuture(zonedStartDateTime.ToDateTimeOffset())) { evt.StartTimestamp = zonedStartDateTime.ToDateTimeOffset(); evt.EndTimestamp = zonedStartDateTime.ToDateTimeOffset().AddHours(1); } else { throw new DateTimeInPastException(); } } else if (subType.Contains("date") && subType.Contains("range")) { string timex = resolutionValues.First()["timex"].TrimStart('(').TrimEnd(')'); string[] timexSplit = timex.Split(','); string fromString = resolutionValues.First()["start"]; string toString = resolutionValues.First()["end"]; if (timexSplit[0].StartsWith("XXXX-")) { fromString = fromString.Substring(4); fromString = string.Format("{0}{1}", DateTime.Now.Year, fromString); } Console.WriteLine(timexSplit.Length); if (timexSplit.Length > 1 && timexSplit[1].StartsWith("XXXX-")) { toString = toString.Substring(4); toString = string.Format("{0}{1}", DateTime.Now.Year, toString); } LocalDateTime from, to; if (fromString.Length == 10) { from = _dateOnlyPattern.Parse(fromString).Value; } else { from = _dateTimePattern.Parse(fromString).Value; } if (toString.Length == 10) { to = _dateOnlyPattern.Parse(toString).Value; } else { to = _dateTimePattern.Parse(toString).Value; } ZonedDateTime zonedFrom = tz.AtLeniently(from); ZonedDateTime zonedTo = tz.AtLeniently(to); if (IsFuture(zonedFrom.ToDateTimeOffset()) && IsFuture(zonedTo.ToDateTimeOffset())) { evt.StartTimestamp = zonedFrom.ToDateTimeOffset(); evt.EndTimestamp = zonedTo.ToDateTimeOffset(); if (IsEventEndBeforeStart(evt)) { throw new EventEndBeforeStartException(); } } else { throw new DateTimeInPastException(); } } else if (subType.Contains("time") && !subType.Contains("range")) { var clock = SystemClock.Instance; LocalDate today = clock.InZone(tz).GetCurrentDate(); string timeString = resolutionValues.Select(v => v["value"]).FirstOrDefault(); string dateTimeString = string.Format("{0} {1}", today.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture), timeString); LocalDateTime startDateTime = _dateTimePattern.Parse(dateTimeString).Value; ZonedDateTime zonedStartDateTime = tz.AtLeniently(startDateTime); if (IsFuture(zonedStartDateTime.ToDateTimeOffset())) { evt.StartTimestamp = zonedStartDateTime.ToDateTimeOffset(); evt.EndTimestamp = zonedStartDateTime.ToDateTimeOffset().AddHours(1); } else { throw new DateTimeInPastException(); } } else if (subType.Contains("time") && subType.Contains("range")) { var clock = SystemClock.Instance; LocalDate today = clock.InZone(tz).GetCurrentDate(); string fromString = resolutionValues.First()["start"]; string toString = resolutionValues.First()["end"]; string fromDateTimeString = string.Format("{0} {1}", today.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture), fromString); string toDateTimeString = string.Format("{0} {1}", today.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture), toString); LocalDateTime startDateTime = _dateTimePattern.Parse(fromDateTimeString).Value; ZonedDateTime zonedStartDateTime = tz.AtLeniently(startDateTime); LocalDateTime endDateTime = _dateTimePattern.Parse(toDateTimeString).Value; ZonedDateTime zonedEndDateTime = tz.AtLeniently(endDateTime); if (IsFuture(zonedStartDateTime.ToDateTimeOffset()) && IsFuture(zonedEndDateTime.ToDateTimeOffset())) { evt.StartTimestamp = zonedStartDateTime.ToDateTimeOffset(); evt.EndTimestamp = zonedEndDateTime.ToDateTimeOffset(); if (IsEventEndBeforeStart(evt)) { throw new EventEndBeforeStartException(); } } else { throw new DateTimeInPastException(); } } else { throw new EventParseException(); } evt.Name = bodyString.RemoveCaseInsensitive(first.Text); } else { throw new EventParseException(); } }
/// <summary> /// Check the Forms Recognizer response - once complete map it to a POCO /// </summary> /// <param name="receipt"></param> /// <returns></returns> private async Task <Receipt> ReceiptAnalysisResult(Receipt receipt) { _logger.LogInformation($"Getting receipt analysis result: {receipt.Id}"); using HttpResponseMessage response = await _httpClient.GetAsync(receipt.OCRRequestUrl); var ocrResponse = await response.Content.ReadAsStringAsync(); if (!response.IsSuccessStatusCode) { var serviceError = JsonSerializer.Deserialize <OCRError>(ocrResponse); _logger.LogError($"OCR Response status code: {response.IsSuccessStatusCode} : Error Code: {serviceError.error.code}, Error Message: {serviceError.error.message}"); receipt.Status = serviceError.error.message; return(receipt); } try { var ocrResult = JsonSerializer.Deserialize <OCRResult>(ocrResponse); receipt.Status = ocrResult.status; _logger.LogInformation($"OCR response status: {receipt.Status}"); if (receipt.Status.Equals(Constants.OCRServiceOperationStatus.Succeeded.ToString(), StringComparison.InvariantCultureIgnoreCase) || receipt.Status.Equals(Constants.OCRServiceOperationStatus.Failed.ToString(), StringComparison.InvariantCultureIgnoreCase)) { receipt.OCRComplete = true; } if (ocrResult.analyzeResult?.documentResults?.Length > 0) { // Assuming only 1 receipt per scan var ocrReceipt = ocrResult.analyzeResult?.documentResults.FirstOrDefault(); receipt.RequestComplete = DateTime.Now; receipt.Merchant = ocrReceipt.fields.MerchantName?.text; receipt.Total = Convert.ToDouble(ocrReceipt.fields.Total?.valueNumber); receipt.SubTotal = Convert.ToDouble(ocrReceipt.fields.Subtotal?.valueNumber); receipt.Tax = Convert.ToDouble(ocrReceipt.fields.Tax?.valueNumber); receipt.Tip = Convert.ToDouble(ocrReceipt.fields.Tip?.valueNumber); //receipt.Address = ocrReceipt.fields.MerchantAddress?.text; if (ocrReceipt.fields.TransactionTime != null && DateTime.TryParse(ocrReceipt.fields.TransactionTime.text, out DateTime time)) { receipt.Time = time; } if (ocrReceipt.fields.TransactionDate != null) { // Dates could come in a variety of different formats and styles var recognizerDate = DateTimeRecognizer.RecognizeDateTime(ocrReceipt.fields.TransactionDate.text, Culture.English); if (recognizerDate.Any()) { var recognizerValues = (List <Dictionary <string, string> >)recognizerDate.First().Resolution["values"]; if (recognizerValues.Any()) { if (recognizerValues[0].TryGetValue("value", out string v)) { DateTime.TryParse(v, out DateTime outputDate); receipt.Date = outputDate; } } } } // Extract line items if (ocrReceipt.fields.Items?.valueArray?.Length > 0) { receipt.Items = new LineItem[ocrReceipt.fields.Items.valueArray.Length]; for (int i = 0; i < ocrReceipt.fields.Items.valueArray.Length; i++) { LineItem lineItem = new LineItem { Item = ocrReceipt.fields.Items?.valueArray[i].valueObject?.Name?.text, }; if (ocrReceipt.fields.Items?.valueArray[i].valueObject?.Quantity?.text != null) { var quantityRecognizer = NumberRecognizer.RecognizeNumber(ocrReceipt.fields.Items.valueArray[i].valueObject.Quantity.text, Culture.English); if (quantityRecognizer.Any()) { var recognizerValue = quantityRecognizer.FirstOrDefault().Resolution["value"]; if (recognizerValue != null) { double.TryParse(recognizerValue.ToString(), out double outputQuantity); lineItem.Quantity = outputQuantity; } } } // Total price could include currency symbol if (ocrReceipt.fields.Items?.valueArray[i].valueObject?.TotalPrice?.text != null) { var numberRecognizer = NumberRecognizer.RecognizeNumber(ocrReceipt.fields.Items.valueArray[i].valueObject.TotalPrice.text, Culture.English); if (numberRecognizer.Any()) { var recognizerValue = numberRecognizer.FirstOrDefault().Resolution["value"]; if (recognizerValue != null) { double.TryParse(recognizerValue.ToString(), out double outputTotal); lineItem.TotalPrice = outputTotal; } } } receipt.Items[i] = lineItem; } } _logger.LogInformation($"Successful OCR receipt extraction Id: {receipt.Id} Total: {receipt.Total}"); } } catch (Exception exp) { _logger.LogError($"Error deserialising the OCR response: {exp.Message}"); receipt.Status = exp.Message; } return(receipt); }
public ActionResult Recognize(int id, string checkedModelString = "[]") { var text = db.Texts.Find(id); if (text == null) { return(HttpNotFound()); } var checkedModels = JsonConvert.DeserializeObject <int[]>(checkedModelString); string culture; switch (text.Language) { case Lang.Chinese: culture = Culture.Chinese; break; case Lang.English: culture = Culture.English; break; case Lang.French: culture = Culture.French; break; case Lang.Spanish: culture = Culture.Spanish; break; case Lang.Portuguese: culture = Culture.Portuguese; break; default: throw new ArgumentOutOfRangeException(); } var results = new List <ModelResult>(); if (checkedModels.Contains(1)) { var model = NumberRecognizer.Instance.GetNumberModel(culture); results.AddRange(model.Parse(text.Content)); } if (checkedModels.Contains(2)) { var model = NumberRecognizer.Instance.GetOrdinalModel(culture); results.AddRange(model.Parse(text.Content)); } if (checkedModels.Contains(3)) { var model = NumberRecognizer.Instance.GetPercentageModel(culture); results.AddRange(model.Parse(text.Content)); } if (checkedModels.Contains(4)) { var model = NumberWithUnitRecognizer.Instance.GetAgeModel(culture); results.AddRange(model.Parse(text.Content)); } if (checkedModels.Contains(5)) { var model = NumberWithUnitRecognizer.Instance.GetCurrencyModel(culture); results.AddRange(model.Parse(text.Content)); } if (checkedModels.Contains(6)) { var model = NumberWithUnitRecognizer.Instance .GetDimensionModel(culture); results.AddRange(model.Parse(text.Content)); } if (checkedModels.Contains(7)) { var model = NumberWithUnitRecognizer.Instance.GetTemperatureModel( culture); results.AddRange(model.Parse(text.Content)); } if (checkedModels.Contains(8)) { var model = DateTimeRecognizer.GetInstance() .GetDateTimeModel(culture); results.AddRange(model.Parse(text.Content)); } return(Json(results)); }
/// <summary> /// DateTime recognizing implementation. /// </summary> /// <param name="text">Text to recognize.</param> /// <param name="culture"><see cref="Culture"/> to use.</param> /// <returns>The recognized <see cref="ModelResult"/> list.</returns> protected override List <ModelResult> Recognize(string text, string culture) { return(DateTimeRecognizer.RecognizeDateTime(text, culture)); }
public static Extraction ValidateAndExtract(string input, string culture) { // Get DateTime model for the specified culture var model = DateTimeRecognizer.GetSingleCultureInstance(culture).GetDateTimeModel(); var results = model.Parse(input); // Check there are valid results if (results.Count > 0 && results.First().TypeName.StartsWith("datetimeV2")) { // The DateTime model can return several resolution types (https://github.com/Microsoft/Recognizers-Text/blob/master/.NET/Microsoft.Recognizers.Text.DateTime/Constants.cs#L7-L14) // We only care for those with a date, date and time, or date time period: // date, daterange, datetime, datetimerange var first = results.First(); var resolutionValues = (IList <Dictionary <string, string> >)first.Resolution["values"]; var subType = first.TypeName.Split('.').Last(); if (subType.Contains("date") && !subType.Contains("range")) { // a date (or date & time) or multiple var moment = resolutionValues.Select(v => DateTime.Parse(v["value"])).FirstOrDefault(); if (IsFuture(moment)) { // a future moment, valid! return(new Extraction { IsValid = true, Values = new[] { moment } }); } // a past moment return(new Extraction { IsValid = false, Values = new[] { moment }, ErrorMessage = PastValueErrorMessage.Replace("$moment$", MomentOrRangeToString(moment)) }); } else if (subType.Contains("date") && subType.Contains("range")) { // range var from = DateTime.Parse(resolutionValues.First()["start"]); var to = DateTime.Parse(resolutionValues.First()["end"]); if (IsFuture(from) && IsFuture(to)) { // future return(new Extraction { IsValid = true, Values = new[] { from, to } }); } var values = new[] { from, to }; return(new Extraction { IsValid = false, Values = values, ErrorMessage = PastValueErrorMessage.Replace("$moment$", MomentOrRangeToString(values)) }); } } return(new Extraction { IsValid = false, Values = Enumerable.Empty <DateTime>(), ErrorMessage = "I'm sorry, that doesn't seem to be a valid delivery date and time" }); }