All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ocpsoft.prettytime.nlp.PrettyTimeParser Maven / Gradle / Ivy

There is a newer version: 5.0.9.Final
Show newest version
package org.ocpsoft.prettytime.nlp;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TimeZone;

import org.ocpsoft.prettytime.nlp.parse.DateGroup;

import com.joestelmach.natty.Parser;

/**
 * A utility for parsing natural language date and time expressions. (e.g. "Let's get lunch at two pm", "I did it 3 days
 * ago")
 * 

* Usage: *

* * PrettyTimeParser p = new PrettyTimeParser();
* List<Date> parsed = p.parse("I'll be there at two");
* //result: Date - 2:00PM *

* * * @author 10) key += numNames[ten + unit]; else key += tensNames[ten / 10] + numNames[unit]; } return key.trim(); } /** * Parse the given language and return a {@link List} with all discovered {@link Date} instances. */ public List parse(String language) { return parse(language, new Date()); } /** * Parse the given language and return a {@link List} with all discovered {@link Date} instances. * * @param referenceDate date to use as a reference for the parsing. */ public List parse(String language, Date referenceDate) { language = words2numbers(language); List result = new ArrayList<>(); List groups = parser.parse(language, referenceDate); for (com.joestelmach.natty.DateGroup group : groups) { result.addAll(group.getDates()); } return result; } /** * Parse the given language and return a {@link List} with all discovered {@link DateGroup} instances. */ public List parseSyntax(String language) { language = words2numbers(language); List result = new ArrayList(); List groups = parser.parse(language); Date now = new Date(); for (com.joestelmach.natty.DateGroup group : groups) { result.add(new DateGroupImpl(now, group)); } return result; } private String words2numbers(String language) { for (Entry entry : translations.entrySet()) { language = language.replaceAll("\\b" + entry.getKey() + "\\b", entry.getValue()); } return language; } private class DateGroupImpl implements DateGroup { private List dates; private int line; private int position; private Date recursUntil; private String text; private boolean recurring; private Date now; public DateGroupImpl(Date now, com.joestelmach.natty.DateGroup group) { this.now = now; dates = group.getDates(); line = group.getLine(); position = group.getPosition(); recursUntil = group.getRecursUntil(); text = group.getText(); recurring = group.isRecurring(); } @Override public List getDates() { return dates; } @Override public int getLine() { return line; } @Override public int getPosition() { return position; } @Override public Date getRecursUntil() { return recursUntil; } @Override public String getText() { return text; } @Override public boolean isRecurring() { return recurring; } @Override public long getRecurInterval() { if (isRecurring()) return getDates().get(0).getTime() - now.getTime(); else return -1; } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy