0

I have a Python function with an NLTK import which I am trying to invoke from Java using Jython. If anyone has succeeded in doing this could you please provide the versions of Python, Jython and NLTK that you are using as it appears to me that, if it can work at all, you have to get the right combination of versions.

I am using Python27, NLTK 3.2.1 and Jython 2.7-b2

I have no problem using Java and Jython to call a simple Python function that accepts some free text and returns a collection of the key words or concepts from the text. The Python function is "run()" within a class "KeyConceptParser" defined in a file "keyConceptParser.py" and the working Java/Jython code is as follows:

String text = "The cat sat on the mat";
PythonInterpreter interpreter = new PythonInterpreter(null);
PyString inputText = new PyString(text);
interpreter.execfile("python/keyConceptParser.py");
PyInstance parser = (PyInstance) interpreter.eval("KeyConceptParser()");
PyList keyConcepts = (PyList) parser.invoke("run", inputText);

However when I enhance keyConceptParser.py including an import of nltk, the above code fails as nltk cannot be imported.

I can fix this by following the advice in from Getting Python's nltk.wordnet module working for Jython by extending the code that constructs the PythonInterpreter class as follows...

PythonInterpreter interpreter = new PythonInterpreter(null, new PySystemState());
PySystemState sys = Py.getSystemState();
sys.path.append(new PyString("C:\\Python27\\Lib\\site-packages"));

Trying again, the nltk module is now found but this time the "future" module is not found. However since this in located in Python's Lib directory, this can be fixed by using two appends to the path instead of one, as follows...

 sys.path.append(new PyString("C:\\Python27\\Lib"));
 sys.path.append(new PyString("C:\\Python27\\Lib\\site-packages"));

Now "future" is found but the module "signal" is missing. This the point that I'm stuck at because there is no "signal" module anywhere in the Python27 downloaded directories (it must be using something from C I think).

From the StackOverflow post I linked to above (over 6 years old) it seems that people have got NLTK to work with Java in the past, but I can't find any information on what versions of the various components work together.

(My Python code using NLTK is working fine when executed outside of Java)

Thanks in advance for any help

Community
  • 1
  • 1
Martin Bamford
  • 379
  • 3
  • 13
  • are there nltk folder in C:\\Python27\\Lib\\site-packages ? – Mustafa DOGRU Aug 05 '16 at 08:36
  • Yes that's where the nltk folder is situated. That's why when I append "site-packages" to the path it fixes the problem with the nltk import not being found and moves the problem on to not being able to find other Python imports that are referenced by nltk. – Martin Bamford Aug 05 '16 at 09:02

0 Answers0