我已经阅读了很多关于这件事的内容,但似乎没有什么对我有用!我正在使用Netbeans 8.0
and Python 2.6.5
(Jython 2.5.1
将 Python 和 Jython 插件添加到 netbeans 的默认设置)我是mac os x 10.9.3 user
我试图打开一个新的 Jython 代码,在其中添加 python 的路径以使其工作:/Library/Frameworks/Python.framework/Versions/2.7/bin/python
它确实如此。
我还尝试使用 Jython 从 Java 代码调用 Python 代码,它适用于简单的打印代码。但是当我添加一个 nltk POS 代码(在 Python 解释器中工作得很好)时,它会抛出一个错误:ImportError: No module named nltk
我尝试了这些路径:/Library/Frameworks/Python.framework/Versions/2.7/bin/python /Library/Python/2.7/site-package
当我从终端调用 python 时,nltk 导入工作正常:
Sosys-MacBook-Pro:~ ha$ echo $PATH
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Users/ha/apache-maven-3.2.1/bin:/Users/ha/apache-opennlp-1.5.3/bin:/Users/ha/jython2.5.4rc1:/Users/ha/nltk_data:/Library/Python/2.7/site-packages:/usr/lib/python2.7/
Sosys-MacBook-Pro:downloads ha$ python
Python 2.7.5 (default, Mar 9 2014, 22:15:05)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import nltk
>>>
然后我得到另一个错误: ImportError: No module named signal. 依此类推,那些没有命名的模块...我还尝试在库的类路径中添加 jython-standalone-2.7-b21.jar ,但它不能解决这个问题。
我怎样才能让它工作?
附录: 我的 Java 代码:
package jythonprojecttes;
import java.lang.*;
import org.python.util.PythonInterpreter;
public class JythonProjectTes {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
try
{
System.out.print(java.lang.System.getProperty("java.class.path"));
PythonInterpreter.initialize(System.getProperties(), System.getProperties(), new String[0]);
PythonInterpreter interp = new PythonInterpreter();
interp.execfile("/Users/ha/NetBeansProjects/JythonProjectTes/src/jythonprojecttes/Code.py");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
我的 Python 代码:
import nltk
tokenizer = None
tagger = None
def tag(sentences):
global tokenizer
global tagger
tagged = nltk.sent_tokenize(sentences.strip())
tagged = [nltk.word_tokenize(sent) for sent in tagged]
tagged = [nltk.pos_tag(sent) for sent in tagged]
return tagged
def ReadFromTXT():
# Reading input from a file
Question = open('/Users/ha/NetBeansProjects/JythonNLTK/src/jythonnltk/input.txt', 'r')
return Question.read()
def PrintToText(tagged):
# Writing output to file
output_file = open('/Users/ha/NetBeansProjects/JythonNLTK/src/jythonnltk/output.txt', 'w')
output_file.writelines(["%s\n" % item for item in tagged])
output_file.close()
print "Printed to Output.txt"
def main():
#Only 'What be' kind of questions.
sentences = ReadFromTXT()
print "The Question is:"+sentences
tagged = tag(sentences)
PrintToText(tagged)
print tagged
if __name__ == '__main__':
print sys.path
main()
提前致谢