1

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
Community
  • 1
  • 1
Brana
  • 1,197
  • 3
  • 17
  • 38

1 Answers1

1

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'NamedEntityTag': 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'indexeddependencies': [[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']]}]}

Korbonits
  • 101
  • 5
  • Why do you call it easy route? If i am not missing something, this things cannot be installed on windows 7 because the requied pexpect module doesn't work on any windows. On which is did you install it? – Brana Feb 17 '14 at 22:45
  • Try this [link] (https://github.com/pexpect/pexpect/archive/master.zip) :) (Note: I am using OS X on a Mac Mini, sorry, missed you were using Windows 7. You may be right that it is not possible.) – Korbonits Feb 17 '14 at 22:52
  • Tried to install and it it is only supports unix like OS's. Anyway i am going to try it with Cygwin – Brana Feb 17 '14 at 23:05
  • 1
    I have found wexpect and before that i have installed pywin32. Worked better but i still have an error. – Brana Feb 18 '14 at 02:41
  • I did the install as in the URL except these command: git clone https://bitbucket.org/torotoki/corenlp-python.git cd corenlp-python wget http://nlp.stanford.edu/software/stanford-corenlp-full-2013-11-12.zip unzip stanford-corenlp-full-2013-11-12.zip – Brana Feb 18 '14 at 02:43
  • So i manually installed CoreNLP in C:\Python27\Lib\site-packages\corenlp and copied the stanford CoreNLP there. Is this ok? – Brana Feb 18 '14 at 02:48
  • Should be. Read the "README". I ran everything from the corenlp-python directory rather than the corenlp subdirectory: "python corenlp\corenlp.py" with the stanford-corenlp-full-2013-11-12 directory in the corenlp-python directory. @Brana – Korbonits Feb 18 '14 at 20:18