簡體   English   中英

Stanford解析器不適用於python(Windows 7)

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

我正在嘗試使用Stanford Parser和NLTK的解決方案,但它無法正常工作。 這基本上是從python調用Stanford解析器,然后在python中獲取輸出。 解析器是用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

它的工作原理很奇怪,因為當補丁程序不正確時,它不會報告錯誤;當軟件補丁程序正確時,Windows會詢問我要在哪個程序中打開應用程序。 我這樣做之后,我仍然像以前一樣只得到空白輸出。 也許這與我運行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

如果您要使用Stanford NLP解析器,我將采用簡單的方法並按照以下說明進行操作。

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

將NLP解析器作為服務器使用后(請注意,默認端口為8080),打開另一個python會話,然后輸入以下內容。

剛剛嘗試過,效果很好:-)

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

這是打印輸出:

{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 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