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

    }
}
1

1 Answer 1

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();
        }
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

I tried this approach, but I wasn't able to call a specific python method and capture the return that value in Java?
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.
But can you give an example of the code you weren't able to run?
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
And how to edit my python code to accept that variable from java
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.