0

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

    }
}
user1483799
  • 409
  • 4
  • 8
  • 17

1 Answers1

2

I'm not sure what the differences are in performance, but you can just call the script directly in Java if you don't want to worry about bridging or other problems. Something like the following.

Python, in a file named 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();
        }
    }
}
Ehtesh Choudhury
  • 7,452
  • 5
  • 42
  • 48
  • I tried this approach, but I wasn't able to call a specific python method and capture the return that value in Java? – user1483799 Dec 05 '12 at 18:57
  • You would have to `print` out the captured value in python, and then read the information in from stdout. Or you can write the data to a file and reopen that file in Java. – Ehtesh Choudhury Dec 05 '12 at 19:07
  • But can you give an example of the code you weren't able to run? – Ehtesh Choudhury Dec 05 '12 at 19:07
  • It is the exact code that I have in my question. I used your method, but how to send the variable that is declated in Java to the called python code. In java I have this variable: String T="About two weeks before the trial started, I was in Shapiro's office in Century City."; // Text – user1483799 Dec 05 '12 at 19:26
  • And how to edit my python code to accept that variable from java – user1483799 Dec 05 '12 at 19:28
  • It would be a command line parameter (either a filename or the data directly) that the python script would run. – Ehtesh Choudhury Dec 05 '12 at 19:28
  • You should look into stdin, stdout, and writing/reading to files to pass around data if you're interested in the method I proposed. – Ehtesh Choudhury Dec 05 '12 at 19:36
  • In my case it has to be a Java variable, as I'm processing the data inside the Java code itself. – user1483799 Dec 05 '12 at 19:42