首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在斯坦福大学SUTime中设置参考时间

如何在斯坦福大学SUTime中设置参考时间
EN

Stack Overflow用户
提问于 2015-10-30 03:30:05
回答 1查看 928关注 0票数 0

我正在使用斯坦福NLPCore SU时间库来构建一个时间解析器。我对设定参考时间有问题。这是我的代码:

代码语言:javascript
复制
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-20 T00:20 时间:2015-07-20 T00:20

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

EN

回答 1

Stack Overflow用户

发布于 2015-11-01 16:16:17

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

代码语言:javascript
复制
time.annotateText("20 minutes from now", "2015-07-20T10:00");
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33428189

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档