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

时间:2017-09-05 04:16:17

标签: python dependencies nltk stanford-nlp

我使用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。 谢谢你...

1 个答案:

答案 0 :(得分: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