gpt4 book ai didi

java - 从 Java 调用 Python 方法时导入 NLTK 失败

转载 作者:行者123 更新时间:2023-12-02 07:24:36 25 4
gpt4 key购买 nike

首先,我在 MacBook 上使用 Eclipse 进行 Java 编码。我编写了一个使用 NLTK“进行自然语言处理”的 Python 代码,并且运行良好。我尝试从 Java 调用一个简单的 Python 代码,它也按预期工作。

但是当我尝试调用使用 NLTK 的 Python 代码时,导入语句失败:“导入错误:没有名为 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());

}
}

最佳答案

我不确定性能上有什么差异,但是如果您不想担心桥接或其他问题,可以直接用 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

Java:

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();
}
}
}

关于java - 从 Java 调用 Python 方法时导入 NLTK 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13716229/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com