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.Calendar;
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) { language = words2numbers(language); List result = new ArrayList(); List groups = parser.parse(language); for (com.joestelmach.natty.DateGroup group : groups) { List dates = relativize(group); result.addAll(dates); } 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 List relativize(com.joestelmach.natty.DateGroup group) { String matchingValue = group.getText(); boolean ambiguous = true; for (String qualifier : periods) { if (matchingValue.contains(qualifier)) ambiguous = false; } List result = group.getDates(); if (ambiguous) { Date now = new Date(); for (int i = 0; i < result.size(); i++) { Date date = result.get(i); while (date.before(now)) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.HOUR_OF_DAY, 12); date = calendar.getTime(); result.set(i, date); } } } return result; } 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(); } public List getDates() { return dates; } public int getLine() { return line; } public int getPosition() { return position; } public Date getRecursUntil() { return recursUntil; } 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 - 2024 Weber Informatics LLC | Privacy Policy