edu.stanford.nlp.util.ReflectionLoading将 CoreNLP 与 Jython 结合使用时出现 $ReflectionLoadingException

edu.stanford.nlp.util.ReflectionLoading$ReflectionLoadingException when using CoreNLP with Jython

我正在尝试使用 Jython 2.7.1 的 CoreNLP 运行,运行 使用 IntelliJ IDEA,测试代码如下:

from edu.stanford.nlp.simple import *
s = Sentence("This is a test.")
ss = s.nerTags()
print(s)
print(ss)

日志:

Connected to pydev debugger (build 172.4155.5)
[MainThread] INFO edu.stanford.nlp.tagger.maxent.MaxentTagger - Loading POS tagger from edu/stanford/nlp/models/pos-tagger/english-left3words/english-left3words-distsim.tagger ... done [0.4 sec].
[MainThread] INFO edu.stanford.nlp.ie.AbstractSequenceClassifier - Loading classifier from edu/stanford/nlp/models/ner/english.all.3class.distsim.crf.ser.gz ... done [0.8 sec].
[MainThread] INFO edu.stanford.nlp.ie.AbstractSequenceClassifier - Loading classifier from edu/stanford/nlp/models/ner/english.muc.7class.distsim.crf.ser.gz ... done [0.4 sec].
[MainThread] INFO edu.stanford.nlp.ie.AbstractSequenceClassifier - Loading classifier from edu/stanford/nlp/models/ner/english.conll.4class.distsim.crf.ser.gz ... done [0.4 sec].
[MainThread] INFO edu.stanford.nlp.time.JollyDayHolidays - Initializing JollyDayHoliday for SUTime from classpath edu/stanford/nlp/models/sutime/jollyday/Holidays_sutime.xml as sutime.binder.1.
Traceback (most recent call last):
  File "C:\Users\user\.IntelliJIdea2017.2\config\plugins\python\helpers\pydev\pydevd.py", line 1599, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "C:\Users\user\.IntelliJIdea2017.2\config\plugins\python\helpers\pydev\pydevd.py", line 1026, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:/Users/user/IdeaProjects/nlptest/src/test.py", line 3, in <module>
    ss = s.nerTags()
    at edu.stanford.nlp.util.ReflectionLoading.loadByReflection(ReflectionLoading.java:40)
    at edu.stanford.nlp.time.TimeExpressionExtractorFactory.create(TimeExpressionExtractorFactory.java:57)
    at edu.stanford.nlp.time.TimeExpressionExtractorFactory.createExtractor(TimeExpressionExtractorFactory.java:38)
    at edu.stanford.nlp.ie.regexp.NumberSequenceClassifier.<init>(NumberSequenceClassifier.java:86)
    at edu.stanford.nlp.ie.NERClassifierCombiner.<init>(NERClassifierCombiner.java:136)
    at edu.stanford.nlp.pipeline.NERCombinerAnnotator.<init>(NERCombinerAnnotator.java:91)
    at edu.stanford.nlp.pipeline.AnnotatorImplementations.ner(AnnotatorImplementations.java:70)
    at edu.stanford.nlp.simple.Document.get(Document.java:115)
    at edu.stanford.nlp.simple.Document.get(Document.java:109)
    at edu.stanford.nlp.simple.Document.runNER(Document.java:886)
    at edu.stanford.nlp.simple.Sentence.nerTags(Sentence.java:528)
    at edu.stanford.nlp.simple.Sentence.nerTags(Sentence.java:536)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
edu.stanford.nlp.util.ReflectionLoading$ReflectionLoadingException: edu.stanford.nlp.util.ReflectionLoading$ReflectionLoadingException: Error creating edu.stanford.nlp.time.TimeExpressionExtractorImpl

我使用 IDE 添加了 CoreNLP 目录到我的 CLASSPATH 中,Jython 似乎可以找到它。 SO 上有几个关于错误的类似问题,但 none 他们的解决方案(如果有的话)实际上对我有用。有没有其他人找到解决这个问题的方法? (根据记录,CoreNLP 在 运行 关闭命令行时工作正常。)

编辑(我尝试在 Windows 上使用 Bash):

user@user:~/uh$ export JYTHONPATH="/home/user/uh/stanford-corenlp-full-2017-06-09/*:"    
user@user:~/uh$ jython
Jython 2.7.0 (default:9987c746f838, Apr 29 2015, 02:25:11)
[OpenJDK 64-Bit Server VM (Oracle Corporation)] on java1.8.0_131
Type "help", "copyright", "credits" or "license" for more information.
>>> from edu.stanford.nlp.simple import *
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named edu
>>>

我不确定,这是怎么回事。您使用的是什么版本的 CoreNLP?在命令行下工作,我无法重现这个问题。见下文。所以,我的前两个问题是:你使用的是最新的 CoreNLP 吗? (如果没有,请尝试升级。)这在命令行对您有用吗? (在这种情况下,只有在 IntelliJ 中使用才有问题——不确定那里会发生什么,但与反射和类加载器的工作方式有关?)

==> Pouring jython-2.7.1.sierra.bottle.tar.gz
  /usr/local/Cellar/jython/2.7.1: 3,797 files, 147.4MB
$ export JYTHONPATH="/Users/manning/Software/stanford-corenlp-full-2017-06-09/*:"
$ jython
Jython 2.7.1 (default:0df7adb1b397, Jun 30 2017, 19:02:43) 
[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)] on java1.8.0_65
Type "help", "copyright", "credits" or "license" for more information.
>>> from edu.stanford.nlp.simple import *
>>> s = Sentence("This is a test.")
>>> ss = s.nerTags()
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/Users/manning/git/javanlp/projects/core/lib/slf4j-simple.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/manning/Software/stanford-corenlp-full-2017-06-09/slf4j-simple.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.SimpleLoggerFactory]
[main] INFO edu.stanford.nlp.tagger.maxent.MaxentTagger - Loading POS tagger from edu/stanford/nlp/models/pos-tagger/english-left3words/english-left3words-distsim.tagger ... done [0.7 sec].
[main] INFO edu.stanford.nlp.ie.AbstractSequenceClassifier - Loading classifier from edu/stanford/nlp/models/ner/english.all.3class.distsim.crf.ser.gz ... done [0.9 sec].
[main] INFO edu.stanford.nlp.ie.AbstractSequenceClassifier - Loading classifier from edu/stanford/nlp/models/ner/english.muc.7class.distsim.crf.ser.gz ... done [0.6 sec].
[main] INFO edu.stanford.nlp.ie.AbstractSequenceClassifier - Loading classifier from edu/stanford/nlp/models/ner/english.conll.4class.distsim.crf.ser.gz ... done [2.4 sec].
[main] INFO edu.stanford.nlp.time.JollyDayHolidays - Initializing JollyDayHoliday for SUTime from classpath edu/stanford/nlp/models/sutime/jollyday/Holidays_sutime.xml as sutime.binder.1.
>>> print(s)
This is a test.
>>> print(ss)
[O, O, O, O, O]
>>> s2 = Sentence("Chris joined Stanford in 1999");
>>> ss2 = s2.nerTags()
>>> print(ss2)
[PERSON, O, ORGANIZATION, O, DATE]
>>> 

我的进一步测试表明这纯粹是一个 Jython 问题,而且是一个跨平台问题。我在 Windows、Ubuntu 和 Mac 上测试了类似的设置,导致了同样的错误。即使 运行 纯 Java 代码设计为 return Jython return 的初始管道也会出现同样的错误。但是,可以通过以下方式将 "ner.useSUTime" 设置为 false 来避免该错误:

props.setProperty("ner.useSUTime", "0")