2015-10-30 26 views
0

我使用Stanford NLPCore SU時間庫來構建時間分析器。我在設置參考時間方面遇到問題。這裏是我的代碼:如何在斯坦福設置參考時間SUTime

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"); 
} 

輸出顯示:

令牌文字:2015-07-20T00:20

的Timex:從現在

時空價值20分鐘2015-07 -20T00:20

它選擇的參考時間是00:00。當我輸入「20分鐘後」時輸出相同

回答

0

您提供的日期時間字符串需要採用date parser code中接受的ISO格式。嘗試分離的日期和時間與T,而不是一個空間(未測試):

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

我自己跑了這個例子與Jon的建議,並得到了想要的結果:標記文本:20分鐘現在 時空價值:2015年-07-20T10:20 Timex:2015-07-20T10:20 – StanfordNLPHelp