Note that there are some explanatory texts on larger screens.

plurals
  1. POimport NLTK fails when calling Python method from Java
    primarykey
    data
    text
    <p>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.</p> <p><em>BUT when I tried to call the Python code that uses the NLTK, the import statement fails:</em> <strong>"ImportError: No module named nltk"</strong></p> <p>It seems that Python was able to locate the NLTK library, but from Java it couldn't.</p> <p>I tried to have the import statement in both the Python code and the Java code but with no luck.</p> <p><strong>Here is the Python code:</strong></p> <pre><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) . . . </code></pre> <p><strong>And Here is the Java Code:</strong></p> <pre><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()); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload