python - 斯坦福解析器不适用于 python (Windows 7)

标签 python python-2.7 nlp stanford-nlp

我正在尝试使用此解决方案 Stanford Parser and NLTK ,但它就是行不通。这基本上是从 python 调用斯坦福解析器,然后在 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

最佳答案

如果您正在寻找使用斯坦福 NLP 解析器,我会采取简单的方法并按照这些说明进行操作。

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

将 NLP 解析器作为服务器启动后(注意默认端口为 8080),打开另一个 python session 并输入以下内容。

刚试了一下,效果很好:-)

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'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 燕子))) (. ?)))', 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'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 '引理': 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'引理': u'the'}], [u'airspeed', {u'NamedEntityTa g': u'O', u'CharacterOffsetEnd': u'20', u'CharacterOffsetBegin': u'12', u'PartOfSpeech': u'NN', u'引理': u'airspeed'}], [u'velocity', {u'NamedEntityTag': u'O', u'CharacterOffsetEnd': u'29', u'CharacterOffsetBegin': u'21', u'PartOfSpeech': u'NN', u'引理': 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'引理': u'unladen'}], [u'swallow', {u'NamedEntityTag': u' O', u'CharacterOffsetEnd': u'51', u'CharacterOffsetBegin': u'44', u'PartOfSpeech': u'VB', u'引理': u'swallow'}], [u'?' , {u'NamedEntityTag': u'O', u'CharacterOffsetEnd': u'52', u'CharacterOffsetBegin': u'51', u'PartOfSpeech': u'.', u'引理': 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']]}]}

关于python - 斯坦福解析器不适用于 python (Windows 7),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21839217/

相关文章:

Python 自然语言处理 : TypeError: not all arguments converted during string formatting

python - 为什么你可以将列表切片超过总索引计数,但不能直接检索所述索引?

linux - python : using function

python-2.7 - 我怎样才能在我的 x/ylabel 附近放一个箭头?

python - 只抓取 PDF 嵌入 URL 中包含特定单词的段落

java - 维基百科解析器

python - 非法指令 : 4 when importing tensorflow in python 3. 6

python - 使用 Python 将表从 Cloud Storage 加载到 BigQuery

python - 查看与可查看并显示小部件

python - 发送具有正确上下文的电子邮件任务