Esempio n. 1
0
        /// <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.Split('.').Last();
                DateTime moment           = new DateTime();
                if (type.Contains("date") && !type.Contains("range"))
                {
                    moment = resolutionValues.Select(v => DateTime.Parse(v["value"])).FirstOrDefault();
                }
                else if (type.Contains("date") && type.Contains("range"))
                {
                    moment = DateTime.Parse(resolutionValues.First()["start"]);
                }
                else
                {
                    continue;
                }
                var curDateTimeText = new TextAndDateTime
                {
                    dateTime = moment,
                    Text     = result.Text
                };
                fndDates.Add(curDateTimeText);
            }
            return(fndDates);
        }
        /// <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);
        }