0

我正在使用斯坦福 NLPCore SU 时间库来构建时间解析器。我在设置参考时间时遇到问题。这是我的代码:

public static String dateFormatString = "yyyy-MM-dd HH:mm";
private static void setup() {
    try {
        String defs_sutime = rulesFiles + "/defs.sutime.txt";
        String holiday_sutime = rulesFiles + "/english.holidays.sutime.txt";
        String _sutime = rulesFiles + "/english.sutime.txt";
        pipeline = new AnnotationPipeline();
        Properties props = new Properties();
        String sutimeRules = defs_sutime + "," + holiday_sutime
                + "," + _sutime;
        props.setProperty("sutime.rules", sutimeRules);
        props.setProperty("sutime.binders", "0");
        props.setProperty("sutime.markTimeRanges", "true");
        props.setProperty("sutime.includeRange", "true");
        pipeline.addAnnotator(new TokenizerAnnotator(false));
        pipeline.addAnnotator(new TimeAnnotator("sutime", props));
     } catch (Exception e) {
        e.printStackTrace();
     }
}
public void annotateText(String text, String referenceDate) {
    try {
        if (pipeline != null) {
            Annotation annotation = new Annotation(text);
            annotation.set(CoreAnnotations.DocDateAnnotation.class, referenceDate);
            pipeline.annotate(annotation);
            List<CoreMap> timexAnnsAll = annotation.get(TimeAnnotations.TimexAnnotations.class);
            for (CoreMap cm : timexAnnsAll) {
                try {
                    Temporal temporal = cm.get(TimeExpression.Annotation.class).getTemporal();

                    System.out.println("Token text : " + cm.toString());
                    System.out.println("Temporal Value : " + temporal.toString());
                    System.out.println("Timex : " + temporal.getTimexValue());

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } else {
            System.out.println("Annotation Pipeline object is NULL");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
} 

public static void main (String [] args) {
    UnderstandTime time = new UnderstandTime();
    setup();
    time.annotateText("20 minutes from now", "2015-07-20 10:00");
}

输出显示:

令牌文本:从现在起 20 分钟

时间价值:2015-07-20T00:20

天美时:2015-07-20T00:20

它选择的参考时间是 00:00。当我输入“20 分钟后”时,输出相同

4

1 回答 1

0

您提供的日期时间字符串需要采用date parser code. T尝试用 a而不是空格分隔日期和时间(未测试):

time.annotateText("20 minutes from now", "2015-07-20T10:00");
于 2015-11-01T16:16:17.487 回答