Search code examples
javastanford-nlpjythonbash-on-windows

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


I'm trying to use CoreNLP run off of Jython 2.7.1, ran using IntelliJ IDEA, with the following test code:

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

Log:

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$2.get(Document.java:115)
    at edu.stanford.nlp.simple.Document$2.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

I added, using my IDE, the CoreNLP directory to my CLASSPATH and it seems that Jython can find it. There are several similar questions on SO on about error but none of their solutions (if they had any) actually worked for me. Has anyone else found a solution to this problem? (For the record, CoreNLP works fine when run off of the command line.)

Edit (my attempt using Bash on Windows):

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
>>>

Solution

  • My further testing shows that this is purely a Jython problem and a cross-platform one at that. I tested similar setups on Windows, Ubuntu, and Mac resulting in the same error. Even running pure Java code designed to return the initial pipeline to Jython returns the same error. However, the error can be avoided by setting "ner.useSUTime" to false with the following:

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