简体   繁体   English

Stanford解析器不适用于python(Windows 7)

[英]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. 我正在尝试使用Stanford Parser和NLTK的解决方案,但它无法正常工作。 This is basically calling Stanford parser from python and then getting the output in the python. 这基本上是从python调用Stanford解析器,然后在python中获取输出。 The parser is written in java. 解析器是用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. 它的工作原理很奇怪,因为当补丁程序不正确时,它不会报告错误;当软件补丁程序正确时,Windows会询问我要在哪个程序中打开应用程序。 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? 也许这与我运行Windows 7而不是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. 如果您要使用Stanford NLP解析器,我将采用简单的方法并按照以下说明进行操作。

https://bitbucket.org/torotoki/corenlp-python 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. 将NLP解析器作为服务器使用后(请注意,默认端口为8080),打开另一个python会话,然后输入以下内容。

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 {u'entents':[{uuparsetree':u'(ROOT(SBARQ(WHNP(WP What)))(SQ(VBZ is)(NP(NP(DT the))(NN空速)(NN速度))( PP(IN of)(NP(DT an)(JJ空载)))(VP(VB吞咽)))(。?)))',u'text':u'空载吞咽的空速是多少?',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'sallow',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 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']]}]} 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']]}]}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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