public override ValidatorResult <DateTime> Validate(IBotContext context) { // Recognize Date DateTimeRecognizer dateRecognizer = DateTimeRecognizer.GetInstance(DateTimeOptions.None); DateTimeModel dateModel = dateRecognizer.GetDateTimeModel("en-US"); var result = dateModel.Parse(context.Request.AsMessageActivity().Text); if (result.Count > 0 && !string.IsNullOrEmpty(result[0].Text)) { try { return(new ValidatorResult <DateTime> { Value = DateTime.ParseExact(result[0].Text, "yyyy-MM-dd", CultureInfo.CurrentCulture) }); } catch (Exception e) { return(new ValidatorResult <DateTime> { Reason = Constants.DATE_ERROR }); } } else { return(new ValidatorResult <DateTime> { Reason = Constants.DATE_ERROR }); } }
public static IList <DateTime> GetDateTimes(this BotContext context) { IList <DateTime> times = new List <DateTime>(); // Get DateTime model for English var model = DateTimeRecognizer.GetInstance().GetDateTimeModel(context.Request.AsMessageActivity().Locale ?? "en-us"); var results = model.Parse(context.Request.AsMessageActivity().Text); // Check there are valid results if (results.Any() && 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 return(results.Where(result => { var subType = result.TypeName.Split('.').Last(); return subType.Contains("time") && !subType.Contains("range"); }) .Select(result => { var resolutionValues = (IList <Dictionary <string, string> >)result.Resolution["values"]; return resolutionValues.Select(v => DateTime.Parse(v["value"])); }).SelectMany(l => l).ToList()); } return(times); }
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.NumberRange: return(NumberRecognizer.Instance.GetNumberRangeModel(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.DateTimeSplitDateAndTime: return(DateTimeRecognizer.GetInstance(DateTimeOptions.SplitDateAndTime).GetDateTimeModel(language)); case Models.CustomNumber: return(GetCustomModelFor(language)); case Models.DateTimeCalendarMode: return(DateTimeRecognizer.GetInstance(DateTimeOptions.CalendarMode).GetDateTimeModel(language)); // DateTimeAlt function is only activated when ExtendedTypes is enabled case Models.DateTimeExtendedTypes: return(DateTimeRecognizer.GetInstance(DateTimeOptions.ExtendedTypes).GetDateTimeModel(language)); case Models.PhoneNumber: return(SequenceRecognizer.Instance.GetPhoneNumberModel(language)); case Models.IpAddress: return(SequenceRecognizer.Instance.GetIpAddressModel(language)); } throw new Exception($"Model '{modelName}' for '{language}' not supported"); }
public void TestDateTime_Duration() { var model = DateTimeRecognizer.GetInstance().GetDateTimeModel(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"); BasicTest(model, reference, "I'll leave for few hours", Constants.SYS_DATETIME_DURATION, "few hours", "PT3H"); BasicTest(model, reference, "I'll leave for a few minutes", Constants.SYS_DATETIME_DURATION, "a few minutes", "PT3M"); BasicTest(model, reference, "I'll leave for some days", Constants.SYS_DATETIME_DURATION, "some days", "P3D"); BasicTest(model, reference, "I'll leave for several weeks", Constants.SYS_DATETIME_DURATION, "several weeks", "P3W"); }
/// <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 Number Range recognizer - This recognizer will find any cardinal or ordinal number range // E.g. "between 2 and 5" will return "(2,5)" NumberRecognizer.Instance.GetNumberRangeModel(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), // Add PhoneNumber recognizer - This recognizer will find any phone number presented // E.g "My phone number is ( 19 ) 38294427." SequenceRecognizer.Instance.GetPhoneNumberModel(defaultCulture), // Add IP recognizer - This recognizer will find any Ipv4/Ipv6 presented // E.g "My Ip is 8.8.8.8" SequenceRecognizer.Instance.GetIpAddressModel(defaultCulture) }); }
public void TestDateTime_DatePeriod() { var model = DateTimeRecognizer.GetInstance().GetDateTimeModel(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"); BasicTest(model, reference, "I'll be out since tomorrow", Constants.SYS_DATETIME_DATEPERIOD, "since tomorrow", "2016-11-08"); BasicTest(model, reference, "I'll be out since August", Constants.SYS_DATETIME_DATEPERIOD, "since august", "XXXX-08"); BasicTest(model, reference, "I'll be out since this August", Constants.SYS_DATETIME_DATEPERIOD, "since this august", "2016-08"); }
public void TestDateTime_Time() { var model = DateTimeRecognizer.GetInstance().GetDateTimeModel(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"); BasicTest(model, reference, "12 noon", Constants.SYS_DATETIME_TIME, "12 noon", "T12"); }
public void TestDateTime_Duration() { var model = DateTimeRecognizer.GetInstance().GetDateTimeModel(Culture.Chinese); var reference = new DateObject(2017, 3, 22); BasicTest(model, reference, "两年", Constants.SYS_DATETIME_DURATION, "两年", "P2Y"); BasicTest(model, reference, "6 天", Constants.SYS_DATETIME_DURATION, "6 天", "P6D"); BasicTest(model, reference, "7 周", Constants.SYS_DATETIME_DURATION, "7 周", "P7W"); BasicTest(model, reference, "5 小时", Constants.SYS_DATETIME_DURATION, "5 小时", "PT5H"); }
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.DateTimeSplitDateAndTime: return(DateTimeRecognizer.GetInstance(DateTimeOptions.SplitDateAndTime).GetDateTimeModel(language)); case Models.CustomNumber: return(GetCustomModelFor(language)); } throw new Exception($"Model '{modelName}' for '{language}' not supported"); }
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_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_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_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_Set() { var model = DateTimeRecognizer.GetInstance().GetDateTimeModel(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.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_DateTime() { var model = DateTimeRecognizer.GetInstance().GetDateTimeModel(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"); }
public void TestDateTime_Date() { var model = DateTimeRecognizer.GetInstance().GetDateTimeModel(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 void TestDateTime_TimePeriod() { var model = DateTimeRecognizer.GetInstance().GetDateTimeModel(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, "in the morning", "TMO"); BasicTest(model, reference, "let's meet in the evening", Constants.SYS_DATETIME_TIMEPERIOD, "in the evening", "TEV"); }
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"); }
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)"); }
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 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> /// Init different locales format, /// Supporting English, French, Deutsche and Chinese Locales. /// </summary> private void InitLocales() { if (_mapLocaleToFunction.Count > 0 && _cacheDateTimeModel.Count > 0) { return; } DateAndTimeLocaleFormat yearMonthDay = new DateAndTimeLocaleFormat { TimeFormat = "{0:hh:mm tt}", DateFormat = "{0:yyyy-MM-dd}" }; DateAndTimeLocaleFormat dayMonthYear = new DateAndTimeLocaleFormat { TimeFormat = "{0:hh:mm tt}", DateFormat = "{0:dd/MM/yyyy}" }; DateAndTimeLocaleFormat monthDayYEar = new DateAndTimeLocaleFormat { TimeFormat = "{0:hh:mm tt}", DateFormat = "{0:MM/dd/yyyy}" }; foreach (string locale in new string[] { "en-za", "en-ie", "en-gb", "en-ca", "fr-ca", "zh-cn", "zh-sg", "zh-hk", "zh-mo", "zh-tw" }) { _mapLocaleToFunction[locale] = yearMonthDay; } foreach (string locale in new string[] { "en-au", "fr-be", "fr-ch", "fr-fr", "fr-lu", "fr-mc", "de-at", "de-ch", "de-de", "de-lu", "de-li" }) { _mapLocaleToFunction[locale] = dayMonthYear; } foreach (string fromLocale in _mapLocaleToFunction.Keys) { string key = fromLocale.Split('-')[0]; if (_cacheDateTimeModel.ContainsKey(key)) { continue; } if (fromLocale.StartsWith("fr")) { _cacheDateTimeModel[key] = DateTimeRecognizer.GetInstance().GetDateTimeModel(Culture.French); } else if (fromLocale.StartsWith("de")) { _cacheDateTimeModel[key] = DateTimeRecognizer.GetInstance().GetDateTimeModel(Culture.German); } else if (fromLocale.StartsWith("pt")) { _cacheDateTimeModel[key] = DateTimeRecognizer.GetInstance().GetDateTimeModel(Culture.Portuguese); } else if (fromLocale.StartsWith("zh")) { _cacheDateTimeModel[key] = DateTimeRecognizer.GetInstance().GetDateTimeModel(Culture.Chinese); } else if (fromLocale.StartsWith("es")) { _cacheDateTimeModel[key] = DateTimeRecognizer.GetInstance().GetDateTimeModel(Culture.Spanish); } else if (fromLocale.StartsWith("en")) { _cacheDateTimeModel[key] = DateTimeRecognizer.GetInstance().GetDateTimeModel(Culture.English); } } _mapLocaleToFunction["en-us"] = monthDayYEar; }
public static TimeValues GetDateTimes(this ITurnContext context) { //IList<DateTime> times = new List<DateTime>(); // Get DateTime model for English var model = DateTimeRecognizer.GetInstance().GetDateTimeModel(context.Activity.AsMessageActivity().Locale ?? "en-us"); var results = model.Parse(context.Activity.AsMessageActivity().Text); // Check there are valid results if (results.Any() && 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"])) .Where(x => x.Year >= DateTime.Now.Year) .FirstOrDefault(); //today var tomorrow = DateTime.Now.AddDays(1); if (moment >= tomorrow) { return(new TimeValues { isValid = true, dt = moment.Date, duration = 0 }); } else { return(new TimeValues { isValid = false, dt = null, duration = 0 }); } } else if (subType.Contains("date") && subType.Contains("range")) { // range var from = DateTime.Parse(resolutionValues.First()["start"]); var to = DateTime.Parse(resolutionValues.First()["end"]); if (to > from) { var tomorrow = DateTime.Now.AddDays(1); if (from >= tomorrow && to >= tomorrow) { return(new TimeValues { isValid = true, dt = from.Date, duration = to.Day - from.Day }); } else { return(new TimeValues { isValid = false, dt = null, duration = 0 }); } } else { return(new TimeValues { isValid = false, dt = null, duration = 0 }); } } } else { return new TimeValues { isValid = false, dt = null, duration = 0 } }; return(new TimeValues { isValid = false, dt = null, duration = 0 }); } }