First of all, I'm using Eclipse on MacBook for Java coding. I wrote a Python code that uses NLTK "for natural language processing" and it worked nicely. I tried to call a simple Python code from Java and it also worked as expected.
BUT when I tried to call the Python code that uses the NLTK, the import statement fails: "ImportError: No module named nltk"
It seems that Python was able to locate the NLTK library, but from Java it couldn't.
I tried to have the import statement in both the Python code and the Java code but with no luck.
Here is the Python code:
#!/usr/local/bin/python
# coding: UTF-8
import os, sys
from nltk.tokenize import word_tokenize # Tokenizer
class RTE:
def WordPhraseRate(self, Text):
T_tokens = word_tokenize(Text)
.
.
.
And Here is the Java Code:
import org.python.util.*;
import org.python.core.*;
import org.python.util.PythonInterpreter;
public class NLU_RTE
{
public PythonInterpreter interpreter = null;
public NLU_RTE()
{
PythonInterpreter.initialize(System.getProperties(), System.getProperties(), new String[0]);
this.interpreter = new PythonInterpreter();
}
void execfile( final String fileName )
{
this.interpreter.execfile(fileName);
}
PyInstance createClass( final String className, final String opts )
{
return (PyInstance) this.interpreter.eval(className + "(" + opts + ")");
}
public static void main( String gargs[] )
{
NLU_RTE ie = new NLU_RTE();
ie.execfile("/Users/Desktop/nlu.py");
ie.interpreter.exec("from nltk.tokenize import word_tokenize # Tokenizer");
String T="About two weeks before the trial started, I was in Shapiro's office in Century City."; // Text
ie.interpreter.set("T", T);
PyObject answer = ie.interpreter.eval("RTE('None').WordPhraseRate(T)");
System.out.print(answer.toString());
}
}