简体   繁体   中英

Stanford parser doesn't work with python (Windows 7)

I am trying to use this solution Stanford Parser and NLTK , but it just cannot work. This is basically calling Stanford parser from python and then getting the output in the python. The parser is written in java.

import os
sentence =  '''I shot an elephant in my pajamas'''
os.popen("echo '"+sentence+"' > ~/stanfordtemp.txt")
parser_out = os.popen("C:/Python27/stanford-parser-2012-11-12/lexparser.sh   ~/stanfordtemp.txt").readlines()
print parser_out

It works strange because when the patch isn't correct it doesn't report an error and when the patch to the software is correct windows asks in which program do i want to open the application. After i do that i am still getting just the blank output as before. Maybe this has something to do with me running windows 7 and not Unix?

Update: Tried to install CoreNLP and I cannot ... the file location is accurate.
corenlp = StanfordCoreNLP(corenlp_dir)  # wait a few minutes...
File "C:\Python27\lib\site-packages\corenlp\corenlp.py", line 430, in __init__
self._spawn_corenlp()
File "C:\Python27\lib\site-packages\corenlp\corenlp.py", line 399, in _spawn_corenlp
self.corenlp = pexpect.spawn(self.start_corenlp, timeout=60, maxread=8192,   searchwindowsize=80)
File "C:\Python27\lib\site-packages\winpexpect-1.5-py2.7.egg\pexpect.py", line 429, in __init__
self._spawn (command, args)
File "C:\Python27\lib\site-packages\winpexpect-1.5-py2.7.egg\pexpect.py", line 516, in _spawn
raise ExceptionPexpect ('The command was not found or was not executable: %s.' % self.command)
pexpect.ExceptionPexpect: The command was not found or was not executable: java.
Exception AttributeError: "StanfordCoreNLP instance has no attribute 'corenlp'" in    <bound method StanfordCoreNLP.__del__ of <corenlp.corenlp.StanfordCoreNLP instance at  0x021DDA08>> ignored

Exception AttributeError: "StanfordCoreNLP instance has no attribute 'corenlp'" in <bound method StanfordCoreNLP.__del__ of <corenlp.corenlp.StanfordCoreNLP instance at 0x0228DA08>> ignored

If you're looking to use the Stanford NLP parser, I'd take the easy route and follow these instructions.

https://bitbucket.org/torotoki/corenlp-python

Once you throw up the NLP parser as a server (note that default port is 8080), open another python session and type in the following.

Just tried it and it works well :-)

import jsonrpclib
import json

server = jsonrpclib.Server("http://localhost:8080")

result = json.loads(server.parse("What is the airspeed velocity of an unladen swallow?"))
print result

This is the printout:

{u'sentences': [{u'parsetree': u'(ROOT (SBARQ (WHNP (WP What)) (SQ (VBZ is) (NP (NP (DT the) (NN airspeed) (NN velocity)) (PP (IN of) (NP (DT an) (JJ unladen)))) (VP (VB swallow))) (. ?)))', u'text': u'What is the airspeed velocity of an unladen swallow?', u'dependencies': [[u'root', u'ROOT', u'swallow'], [u'dobj', u'swallow', u'What'], [u'aux', u'swallow', u'is'], [u'det', u'velocity', u'the'], [u'nn', u'velocity', u'airspeed'], [u'nsubj', u'swallow', u'velocity'], [u'det', u'unladen', u'an'], [u'prep_of', u'velocity', u'unladen']], u'words': [[u'What', {u'NamedEntityTag': u'O', u'CharacterOffsetEnd': u'4', u'CharacterOffsetBegin': u'0', u'PartOfSpeech': u'WP', u'Lemma': u'what'}], [u'is', {u'NamedEntityTag': u'O', u'CharacterOffsetEnd': u'7', u'CharacterOffsetBegin': u'5', u'PartOfSpeech': u'VBZ', u'Lemma': u'be'}], [u'the', {u'NamedEntityTag': u'O', u'CharacterOffsetEnd': u'11', u'CharacterOffsetBegin': u'8', u'PartOfSpeech': u'DT', u'Lemma': u'the'}], [u'airspeed', {u'NamedEntityTa g': u'O', u'CharacterOffsetEnd': u'20', u'CharacterOffsetBegin': u'12', u'PartOfSpeech': u'NN', u'Lemma': u'airspeed'}], [u'velocity', {u'NamedEntityTag': u'O', u'CharacterOffsetEnd': u'29', u'CharacterOffsetBegin': u'21', u'PartOfSpeech': u'NN', u'Lemma': u'velocity'}], [u'of', {u'NamedEntityTag': u'O', u'CharacterOffsetEnd': u'32', u'CharacterOffsetBegin': u'30', u'PartOfSpeech': u'IN', u'Lemma': u'of'}], [u'an', {u'NamedEntityTag': u'O', u'CharacterOffsetEnd': u'35', u'CharacterOffsetBegin': u'33', u'PartOfSpeech': u'DT', u'Lemma': u'a'}], [u'unladen', {u'NamedEntityTag': u'O', u'CharacterOffsetEnd': u'43', u'CharacterOffsetBegin': u'36', u'PartOfSpeech': u'JJ', u'Lemma': u'unladen'}], [u'swallow', {u'NamedEntityTag': u'O', u'CharacterOffsetEnd': u'51', u'CharacterOffsetBegin': u'44', u'PartOfSpeech': u'VB', u'Lemma': u'swallow'}], [u'?', {u'NamedEntityTag': u'O', u'CharacterOffsetEnd': u'52', u'CharacterOffsetBegin': u'51', u'PartOfSpeech': u'.', u'Lemma': u'?'}]], u'indexeddepende ncies': [[u'root', u'ROOT-0', u'swallow-9'], [u'dobj', u'swallow-9', u'What-1'], [u'aux', u'swallow-9', u'is-2'], [u'det', u'velocity-5', u'the-3'], [u'nn', u'velocity-5', u'airspeed-4'], [u'nsubj', u'swallow-9', u'velocity-5'], [u'det', u'unladen-8', u'an-7'], [u'prep_of', u'velocity-5', u'unladen-8']]}]}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM