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)