Monday, March 9, 2009

Natural Language .NET Date Parser.

As part of the Smpl application, we (Precision Software) built a natural language date and time parser for .NET. This is not just another DateTime.Parse("...") class. It is built to handle the majority of ways that humans communicate dates, times, date spans, time spans, etc. in English. And, by the way, English is a much simpler and compact way to communicate complex time specifications than form based UI. If you don't believe me, take a look at Outlook's Recurring date form. Wow. All this to describe what English describes in a few words (ex: "The 9th of every month").

Have you needed a date parser that can handle recurring (repeating) dates?
How about one that can parse something like, "Every Thursday, 2 PM".
Need to parse dates strings that occur once per month, like "The first Tuesday of every month". If you searched the web, you probably found the Perl Date::Manip module. If you are writing a Unix script, that is great, but for those of us C#, Visual Basic, or other .NET languages, calling Perl to parse dates is not a practical option.


This parser treats all dates and times as spans, with optional recurrence. Thus, "Tuesday, 10AM" is treated as a span from 10-11AM. This creates a really convenient API: date.Overlaps(otherDate) will check if two dates overlap. This will return true, for example, if date = "Jan 5", and otherDate = "Tuesday, 10AM", assuming this Tuesday is January 5th.

The optional recurrence allows enter dates in the form of "Every Monday, 8PM", or "Every January 5", "Every January", or even "Everyday, 5-9PM". To iterate these recurrences, one simply asks for date.Next("today"), for example, which will return the next occurrences of a given date after today. Combining this with Overlaps makes a very tight, easy to read program.

To deal with ambiguous situations, the parser has a set of flags to control preferences. For example, "Tuesday from 3-5 PM" could mean *this* Tuesday, or it could mean *every* Tuesday. By default, this causes a parse error, but by preferring recurring or not, the error can be eliminated. In such a case, one can still, for example, explicitly state "Every Tuesday, 3-5PM".

The smart date parser is a .NET module, and therefore can be used with any .NET language, including ASP and other web based languages.

You can see it in action here.

If this module would be of use to you, let us know by e-mail: contact@precisionsoftware.us. We will respond within 1-2 business days of your e-mail. For faster response, call us at 208-882-5980.

2 comments:

MIYU said...

Th link doesnt work for your example?

Ryan said...

I would really be interested in this code. If you can contact me at ryan@ryanhelms.com, I would greatly appreciate it.

Thanks
Ryan