如何在斯坦福SUTime设置参考时间

时间:2015-10-30 03:30:05

标签: stanford-nlp sutime

我正在使用Stanford 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

     

Timex:2015-07-20T00:20

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

1 个答案:

答案 0 :(得分:0)

您提供的日期时间字符串需要采用date parser code中接受的ISO格式。尝试使用T而不是空格(未测试)分隔日期和时间:

time.annotateText("20 minutes from now", "2015-07-20T10:00");