A few months ago, I wrote a patch for Tasque which is a really neat TODO list manager. This allowed you to create duedates on tasks with absolutely no effort
Since my current project (TaskForce) is sort of a TODO list on steroids, I thought I’d pull the patch back out of the deepest corners of gnome bugzilla and use it in my own project, and also decouple it and release it while I’m at it. It accepts a string, and extracts a task and a date from it.
This is a natural date parser for C# – it allows you to specify a task with a due date and it extracts a valid DateTime object and returns it.
Examples:
I’m pasting the XML documentation of the source code.
/// Original Task string: "GSoC Proposals due before 3rd april"
/// The Task is:"GSoC Proposals due" and the due 4/3/2009 3:51:09 PM
///
/// Original Task string: "Lab reports due on monday"
/// The Task is:"Lab reports due" and the due 3/30/2009 3:51:09 PM
///
/// Original Task string: "Date at 8PM this saturday"
/// The Task is:"Date at 8PM" and the due 3/28/2009 3:51:09 PM
///
/// Original Task string: "New years' on January 1st"
/// The Task is:"New years'" and the due 1/1/2010 3:51:09 PM
///
/// Original Task string: "Study solid state pysics on tuesday"
/// The Task is:"Study solid state pysics" and the due 3/31/2009 3:51:09 PM
///
/// Original Task string: "Solid state physics test next tuesday"
/// The Task is:"Solid state physics test" and the due 4/7/2009 3:51:09 PM
///
/// Original Task string: "April fools' on 1st"
/// The Task is:"April fools'" and the due 4/1/2009 3:51:09 PM
///
/// Original Task string: "Friend's birthday on Feb 5th"
/// The Task is:"Friend's birthday" and the due 2/5/2010 3:51:09 PM
Features: * Extremely robust guessing – if you say “Mid term eval due on 1st”, it looks for the first of next month and adds it. * Supports natural constructs – if you say “some task due next tuesday”, the task string is “some task”. If you use on/before/during/this/due, those are treated as date descriptors. Moreover if you say “Fix this bug due tommorow”, the task is “fix this bug”. If you say “Fix bug this friday”, the task is “fix bug”.
Usage: This is written as a simple, uncoupled, highly cohesive class “NaturalDateParser”. Since the name of the class or namespace isn’t mentioned in the functions of interest, you can always extract it and place it in your own code.
To use:
string input, task;
DateTime dueDate;
input = "Mid term evaluations on 6th";</p>
<pre><code>NaturalDateParser.GuessDateFromString(input, out task, out dueDate); // this has to be in the same namespace
Console.WriteLine("Output: {0}, DueDate: {1}", task, dueDate); //Output: Mid term evaluations, DueDate: 07/06/2009 11:28:21
</code></pre>
<p>
Download: To get the latest version of this class, clone the github gist:
git clone git://gist.github.com/140483.git
or you can view it from: http://gist.github.com/140483
The code is licensed under the liberal MIT/X11 license. Use it in anything you wish at your own risk.
Unit tests: I wrote NUnit tests to practice writing the C# tests. If you need to have unit tests for this class, email me and I will send them to you.

0 Response to “Easy and powerful natural date parser.”