意外的日期/日期时间字符串导致 Stanford CoreNLP 出现异常

Unexpected Date / DateTime Strings cause exception in Stanford CoreNLP

根据 CoreNLP's Git,该问题已在某些版本的 CoreNLP 中得到修复,根据我的猜测可能是 3.5.1,因为 NER 在更改说明中被列为更改模块之一。但是,3.5.x 需要跳转到 Java 1.8,目前我们不准备这样做。

另外,免责声明,我也post解决了那个问题,但可能没有看到,因为问题已经解决了。鉴于 SO 是支持 CoreNLP 的官方论坛,我在这里问一下。

所以我想问一下,解决这个问题的改变是什么?它实际上是否存在于当前版本中,或者是否还有其他需要做的事情。我需要在不从我当前使用的 3.4.1 升级的情况下解决这个问题。

郑重声明,下面的字符串应该代表 2009 年 12 月 3 日 10:00(该字符串中没有给出秒数,因此我们也假设为 00)。

这是堆栈跟踪。

java.lang.NumberFormatException: For input string: "200912031000"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:583)
at java.lang.Integer.valueOf(Integer.java:766)
at edu.stanford.nlp.ie.pascal.ISODateInstance.extractDay(ISODateInstance.java:1107)
at edu.stanford.nlp.ie.pascal.ISODateInstance.extractFields(ISODateInstance.java:398)
at edu.stanford.nlp.ie.pascal.ISODateInstance.<init>(ISODateInstance.java:82)
at edu.stanford.nlp.ie.QuantifiableEntityNormalizer.normalizedDateString(QuantifiableEntityNormalizer.java:363)
at edu.stanford.nlp.ie.QuantifiableEntityNormalizer.normalizedDateString(QuantifiableEntityNormalizer.java:338)
at edu.stanford.nlp.ie.QuantifiableEntityNormalizer.processEntity(QuantifiableEntityNormalizer.java:1018)
at edu.stanford.nlp.ie.QuantifiableEntityNormalizer.addNormalizedQuantitiesToEntities(QuantifiableEntityNormalizer.java:1320)
at edu.stanford.nlp.ie.NERClassifierCombiner.classifyWithGlobalInformation(NERClassifierCombiner.java:145)
at edu.stanford.nlp.ie.AbstractSequenceClassifier.classifySentenceWithGlobalInformation(AbstractSequenceClassifier.java:322)
at edu.stanford.nlp.pipeline.NERCombinerAnnotator.doOneSentence(NERCombinerAnnotator.java:148)
at edu.stanford.nlp.pipeline.SentenceAnnotator.annotate(SentenceAnnotator.java:95)
at edu.stanford.nlp.pipeline.NERCombinerAnnotator.annotate(NERCombinerAnnotator.java:137)
at edu.stanford.nlp.pipeline.AnnotationPipeline.annotate(AnnotationPipeline.java:67)
at edu.stanford.nlp.pipeline.StanfordCoreNLP.annotate(StanfordCoreNLP.java:847)

编辑

我正在再次查看这个,因为我目前正在处理我的代码的一些 sutime 部分,我可以通过简单地执行以下操作来重现:

    ISODateInstance idi = new ISODateInstance();
    boolean fields = idi.extractFields("200912031000");
    System.out.println(fields);

注意true是打印出来的值。

我在 Stanford CoreNLP 3.4.1 中没有发现这个问题。我下载了 3.4.1 发行版和 运行 一个非常长的数字的句子,没有出现任何崩溃。

你能给我一个导致崩溃的例句吗?

好的,那我来说说为什么会出现这个问题。 3.4.1 中的 extractDay() 有两个问题:

  1. Integer.valueOf 在第 1107 行中使用。这会产生我们看到的错误,因为如果将字符串解释为数字,则它肯定是长整型。 Long.valueOf以后版本使用
  2. extractDay 应该返回 False,因为它无法对该字符串执行任何操作。但是,try 块(第 1106 行)位于 for 循环(第 1097 行)内,这意味着在失败后,可以检查更多标记,从而导致该方法最终返回 true。这将允许创建注释,即使在技术上由于解析失败不应创建注释。在以后的版本中,try 被移到了 for 块之外。

所以唯一的答案就是更新到更高版本(虽然我现在还不能更新到更高版本)。