Exemple #1
0
        static void Main(string[] args)
        {
            Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("fr");

            var parser = new Chronic.Parser();

            Span parsedObj = parser.Parse("dans 1h");

            DateTime?parsedDateTime = parsedObj.Start;


            Console.ReadLine();
        }
        private static DateTime?RecognizeTime(string utterance)
        {
            try
            {
                var parser = new Chronic.Parser();
                if (!string.IsNullOrEmpty(utterance))
                {
                    Span     parsedObj      = parser.Parse(utterance);
                    DateTime?parsedDateTime = parsedObj?.Start;
                    return(parsedDateTime);
                }

                return(null);
            }
            catch (Exception)
            {
                return(null);
            }
        }
        protected override void Execute(CodeActivityContext context)
        {
            string[]         seperatingChars = { " to ", ". ", " from ", "? ", "! " };
            string[]         stringToParse   = inString.Get(context).Split(seperatingChars, System.StringSplitOptions.RemoveEmptyEntries);
            List <DateTime?> listDates       = new List <DateTime?>();

            foreach (string word in stringToParse)
            {
                if (string.IsNullOrEmpty(word))
                {
                    Console.WriteLine("String is null or empty.");
                }
                else
                {
                    try
                    {
                        var  parser    = new Chronic.Parser();
                        Span parsedObj = parser.Parse(word);

                        if (parsedObj == null)
                        {
                            listDates.Add(null);
                        }
                        else
                        {
                            DateTime?parsedDateTime = parsedObj.Start;
                            Console.WriteLine(parsedDateTime);
                            listDates.Add(parsedDateTime);
                        }
                    }
                    catch (Exception e)
                    {
                        listDates.Add(null);
                        Console.WriteLine("\"" + word + "\" was not recognized");
                    }
                }
            }

            ParsedDates.Set(context, listDates);
        }
        private async Task DatesDone(IDialogContext context, IAwaitable <IMessageActivity> result)
        {
            var r = await result;

            var p    = new Chronic.Parser();
            var time = p.Parse(r.Text);

            if (time?.Start != null)
            {
                BudgetData.To = time.Start.Value;
            }
            else
            {
                BudgetData.To = DateTime.Now.AddDays(4).Date;
            }

            await context.Typing();

            await context.PostAsync("What is your budget?");

            context.Wait(BudgetDone);
        }
        private async Task DateFrom(IDialogContext context, IAwaitable <IMessageActivity> result)
        {
            var r = await result;

            var p    = new Chronic.Parser();
            var time = p.Parse(r.Text);

            if (time?.Start != null)
            {
                BudgetData.From = time.Start.Value;
            }
            else
            {
                BudgetData.From = DateTime.Now.AddDays(1).Date;
            }

            await context.Typing();

            await context.PostAsync("When do you get back home?");

            context.Wait(DatesDone);
        }
Exemple #6
0
        public GithubSearchModule()
        {
            var parser = new Parser();

            // Converts query string expressions from relative datetime to absolute datetime and redirects to github search api
            Get["/search"] = x =>
            {
                var query = HttpUtility.UrlDecode(this.Context.Request.Url.Query);

                var updatedQuery = Regex.Replace(query, @"(\[(.*?)\])",
                    m =>
                    {
                        var originalValue = m.Result("$2");

                        var span = parser.Parse(HttpUtility.UrlDecode(originalValue));

                        var absoluteDate = span?.Start?.Date.ToString("yyyy-MM-dd") ?? originalValue;
                        return absoluteDate;
                    });

                return new RedirectResponse($"https://github.com/search{updatedQuery}", RedirectResponse.RedirectType.Temporary);
            };
        }
 /// <summary>
 /// Construct a date/time recognizer.
 /// </summary>
 /// <param name="field">DateTime field.</param>
 public RecognizeDateTime(IField <T> field)
     : base(field)
 {
     _parser = new Chronic.Parser();
 }
Exemple #8
0
 /// <summary>
 /// Construct a date/time recognizer.
 /// </summary>
 /// <param name="field">DateTime field.</param>
 /// <param name="culture">Culture to use for parsing.</param>
 /// <param name="configuration">Form configuration to use for choice and no preference.</param>
 public RecognizeDateTime(IField <T> field, CultureInfo culture, FormConfiguration configuration = null)
     : base(field, configuration)
 {
     _culture = culture;
     _parser  = new Chronic.Parser();
 }