Search in sources :

Example 1 with Temporal

use of edu.stanford.nlp.time.SUTime.Temporal in project lucida by claritylab.

the class TextProcessor method parse.

/** 
	 *  Returns the date range string by parsing text. 
	 *
	 *  @param text String representing the original query
	 */
public String[] parse(String text) {
    // The pipelines may produce same results, so store results in a set.
    TreeSet<Temporal> has_seen = new TreeSet<Temporal>(new TemporalComparator());
    // Time is comparable, so add temporal of type time and date into a TreeSet to get
    // the minimum and maximum time which define the range for event retrieval.
    TreeSet<Time> times = new TreeSet<Time>();
    for (AnnotationPipeline pipeline : pieplines) {
        Annotation annotation = new Annotation(text);
        annotation.set(CoreAnnotations.DocDateAnnotation.class, new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        pipeline.annotate(annotation);
        List<CoreMap> timexAnnsAll = annotation.get(TimeAnnotations.TimexAnnotations.class);
        for (CoreMap cm : timexAnnsAll) {
            Temporal temporal = cm.get(TimeExpression.Annotation.class).getTemporal();
            temporal.getTime();
            if (has_seen.contains(temporal)) {
                continue;
            }
            has_seen.add(temporal);
            if (temporal.getTimexType().name().equals("TIME") || temporal.getTimexType().name().equals("DATE")) {
                if (temporal.getTime() != null) {
                    try {
                        times.add(temporal.getTime());
                    } catch (NullPointerException e) {
                    }
                }
            }
        }
    }
    // Get the minimum and maximum time only if there are at least two Time objects in times. 
    if (times.size() >= 2) {
        return new String[] { regexNormalize(Collections.min(times).toString(), 0), regexNormalize(Collections.max(times).toString(), 1) };
    }
    // Since the range couldn't be defined by times, define the range from has_seen.
    for (Temporal temporal : has_seen) {
        // Due to a bug (?) in coreNLP, getRange() for "current week" will result in year 2015.
        // Thus, try parsing as week before getRange().
        String[] try_parse_as_week = parseAsWeek(temporal.toString(), text);
        if (try_parse_as_week != null) {
            return try_parse_as_week;
        }
        if (isReadbleTime(temporal.getRange().toString())) {
            List<String> string_list = Arrays.asList(temporal.getRange().toString().split(","));
            String s1 = regexNormalize(string_list.get(0), 0);
            String s2 = regexNormalize(string_list.get(1), 1);
            if (s1.length() >= 10 && s2.length() >= 10 && s1.substring(0, 10).equals(s2.substring(0, 10))) {
                if (text.contains("from") || text.contains("start") || text.contains("begin")) {
                    s2 = null;
                } else if (text.contains("until")) {
                    s1 = null;
                }
            }
            return new String[] { s1, s2 };
        }
    }
    // No temporal expression is found by any pipeline.
    return new String[] { null, null };
}
Also used : Time(edu.stanford.nlp.time.SUTime.Time) Date(java.util.Date) Temporal(edu.stanford.nlp.time.SUTime.Temporal) TreeSet(java.util.TreeSet) CoreAnnotations(edu.stanford.nlp.ling.CoreAnnotations) SimpleDateFormat(java.text.SimpleDateFormat) CoreMap(edu.stanford.nlp.util.CoreMap)

Example 2 with Temporal

use of edu.stanford.nlp.time.SUTime.Temporal in project CoreNLP by stanfordnlp.

the class SUTimeSimpleParser method main.

public static void main(String[] args) throws SUTimeParsingError {
    for (String s : new String[] { "1972", "1972-07-05", "0712", "1972-04" }) {
        System.out.println("String: " + s);
        Temporal timeExpression = parse(s);
        System.out.println("Parsed: " + timeExpression);
        System.out.println();
    }
}
Also used : Temporal(edu.stanford.nlp.time.SUTime.Temporal)

Aggregations

Temporal (edu.stanford.nlp.time.SUTime.Temporal)2 CoreAnnotations (edu.stanford.nlp.ling.CoreAnnotations)1 Time (edu.stanford.nlp.time.SUTime.Time)1 CoreMap (edu.stanford.nlp.util.CoreMap)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 TreeSet (java.util.TreeSet)1