我正在使用斯坦福 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 分钟后”时,输出相同