Java Date and Time Natural Language Parsing NLP

PrettyTime :: NLP

The simplest, smallest natural language java.util.Date and human time parsing library for Java: Computers understanding people.


1. Include PrettyTime :: NLP in your Project

Add PrettyTime :: NLP to your maven pom.xmlOr download the JARs
<dependency>
   <groupId>org.ocpsoft.prettytime</groupId>
   <artifactId>prettytime-nlp</artifactId>
   <version>${version.prettytime}</version>
</dependency>

<!-- To use snapshots, you must also use the Sonatype Snapshots respository -->
<repository>
   <id>sonatype-snapshots</id>
   <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
</repository>

2. Use PrettyTimeParser

PrettyTime NLP is simple, and requires interfacing with only a single class, PrettyTimeParser
Parse your first natural date
package com.example;

import org.ocpsoft.prettytime.nlp.PrettyTimeParser;

public class Example
{
   public static void main(String[] args)
   {
      List<Date> dates = new PrettyTimeParser().parse("I'm going to the beach in three days!");
      System.out.println(dates);
      // Prints: "[Sun Dec 12 13:45:12 CET 2013]"
   }
}

3. Get more detailed.

You can also get more detailed results from the text, including whether or not dates are recurring, and when they recur until:

Parse recurring dates
@Test
   public void testParseSyntaxRecurring()
   {
      List<DateGroup> parse = new PrettyTimeParser().parseSyntax("I eat fish every three days");
      Assert.assertFalse(parse.isEmpty());
      String formatted = new PrettyTime().format(parse.get(0).getDates().get(0));
      Assert.assertEquals("3 days from now", formatted);
      Assert.assertEquals(1, parse.get(0).getLine());
      Assert.assertEquals(14, parse.get(0).getPosition());
      Assert.assertEquals(1, parse.get(0).getDates().size());
      Assert.assertNull(parse.get(0).getRecursUntil());
      Assert.assertTrue(parse.get(0).isRecurring());
      Assert.assertEquals(1000 * 60 * 60 * 24 * 3, parse.get(0).getRecurInterval());
   }