首先,我在MacBook上使用Eclipse进行Java编码。我编写了一个使用NLTK“进行自然语言处理”的Python代码,它工作得很好。我试图从Java调用一个简单的Python代码,它也按预期工作。
但是当我尝试调用使用NLTK的Python代码时,import语句失败了: “ImportError:没有名为nltk的模块”
似乎Python能够找到NLTK库,但是从Java中找不到。
我试图在Python代码和Java代码中都有import语句,但没有运气。
以下是Python代码:
#!/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)
.
.
.
以下是Java代码:
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());
}
}
答案 0 :(得分:2)
我不确定性能有何不同,但如果您不想担心桥接或其他问题,可以直接用Java调用脚本。如下所示。
Python ,位于名为testing.py的文件中:
#!/usr/local/bin/python
# coding: UTF-8
import os, sys
from nltk.tokenize import word_tokenize # Tokenizer
if __name__ == "__main__":
# If you want to read from a file instead of passing data
#text = open(sys.argv[1]).read()
# read the first argument passed to script
text = sys.argv[1]
tokens = word_tokenize(text)
print tokens
<强>爪哇强>:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.io.File;
public class pythonfromjava{
public static void main(String argv[]) {
try{
// for tilda expansion
//if (filepath.startsWith("~" + File.separator)) {
//filepath = System.getProperty("user.home") + filepath.substring(1);
//}
//ProcessBuilder builder = new ProcessBuilder("python", "-c", "import sys; import nltk; print \"whatever\"");
ProcessBuilder builder = new ProcessBuilder("python", "testing.py", "four scores and seven years ago");
builder.redirectErrorStream(true);
Process p = builder.start();
InputStream stdout = p.getInputStream();
BufferedReader reader = new BufferedReader (new InputStreamReader(stdout));
String line;
while ((line = reader.readLine ()) != null) {
System.out.println ("Stdout: " + line);
}
} catch (Exception e){
e.printStackTrace();
}
}
}