我如何获得依赖解析输出的更多信息?

问题描述 投票:3回答:1

我使用Stanford Dependency Parser和NLTK解析句子“我在睡觉时拍了一头大象”这是我的代码:

from nltk.parse.stanford import StanfordDependencyParser
path_to_jar = 'path_to/stanford-parser-full-2014-08-27/stanford-parser.jar'
path_to_models_jar = 'path_to/stanford-parser-full-2014-08-27/stanford-
parser-3.4.1-models.jar'
dependency_parser = StanfordDependencyParser(path_to_jar=path_to_jar, 
path_to_models_jar=path_to_models_jar)
result = dependency_parser.raw_parse('I shot an elephant in my sleep')
dep = result.next()
list(dep.triples())

输出:

[((u'shot', u'VBD'), u'nsubj', (u'I', u'PRP')),
((u'shot', u'VBD'), u'dobj', (u'elephant', u'NN')),
((u'elephant', u'NN'), u'det', (u'an', u'DT')),
((u'shot', u'VBD'), u'prep', (u'in', u'IN')),
((u'in', u'IN'), u'pobj', (u'sleep', u'NN')),
((u'sleep', u'NN'), u'poss', (u'my', u'PRP$'))]

但输出没有句子中的单词索引:ex我希望它应该返回如下内容:

nsubj(shot-2, I-1)
det(elephant-4, an-3)
dobj(shot-2, elephant-4)
prep(shot-2, in-5)
poss(sleep-7, my-6)
pobj(in-5, sleep-7)

射击指数是2或句子中的大象是4。谢谢..

python dependencies nltk stanford-nlp
1个回答
0
投票

这可能会得到你想要的:

from stanfordcorenlp import StanfordCoreNLP

nlp = StanfordCoreNLP(r'/path/to/stanford-corenlp-full-2018-02-27')
# can be download at https://stanfordnlp.github.io/CoreNLP/#download

sent = 'For six years, T. Marshall Hahn Jr. has made corporate acquisitions in the George Bush mode: kind and gentle.'

print('Dependency Parsing:', nlp.dependency_parse(sentence))

nlp.close()

此代码改编自https://blog.csdn.net/qq_35203425/article/details/80451243

© www.soinside.com 2019 - 2024. All rights reserved.