3

I'm calling a python code from a java code using jython by PythonInterpreter. the python code just tag the sentence:

import nltk
import pprint

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 PrintToText(tagged):
    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()  

def main():
    sentences = """What is the salary of Jamie"""  
    tagged = tag(sentences)
    PrintToText(tagged)
    pprint.pprint(tagged)

if __name__ == 'main':    
    main()

I got this error:

run:
Traceback (innermost last):
  (no code object) at line 0
  File "/Users/ha/NetBeansProjects/JythonNLTK/src/jythonnltk/Code.py", line 42
        output_file.writelines( "%s\n" % item for item in tagged )
                                              ^
SyntaxError: invalid syntax
BUILD SUCCESSFUL (total time: 1 second)

this code works very fine if I opened it in a python project but calling it from java fire this error. How can I solve it?

Thanks in advance

UPDATE: I have edit the line to output_file.writelines( ["%s\n" % item for item in tagged] ) as @User sugeested but I received another error message:

Traceback (innermost last):
  File "/Users/ha/NetBeansProjects/JythonNLTK/src/jythonnltk/Code.py", line 5, in ?
ImportError: no module named nltk
BUILD SUCCESSFUL (total time: 1 second)
2
  • This looks like a bug to me. Try output_file.writelines(( "%s\n" % item for item in tagged )) and output_file.writelines([ "%s\n" % item for item in tagged ]) and consider to report it. Commented May 28, 2014 at 11:22
  • @User I did what u said and I got a new error message. I have updated my question with the new error. Thanks! Commented May 28, 2014 at 11:41

1 Answer 1

1

Now that the compile-time syntax error is solved, you are getting run-time errors. What is nltk? Where is nltk? The ImportError implies that nltk is not in your import path.

Try writing a small simple program and examine sys.path ; you might need to append location of nltk before importing it.

### The import fails if nltk is not in the system path:
>>> import nltk
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named nltk

### Try inspecting the system path:
>>> import sys
>>> sys.path
['', '/usr/lib/site-python', '/usr/share/jython/Lib', '__classpath__', '__pyclasspath__/', '/usr/share/jython/Lib/site-packages']

### Try appending the location of nltk to the system path:
>>> sys.path.append("/path/to/nltk")

#### Now try the import again.
Sign up to request clarification or add additional context in comments.

1 Comment

Hello @Jacob , I've checked the system path and it still gives me the same error message! it seems that it has something with calling my code from Java since it works very fine from python code. Thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.