我使用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$'))]但是输出在句子中没有单词的索引:例如,我想它应该返回如下内容:
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。谢谢你..
发布于 2019-04-19 01:33:14
这可能会得到你想要的:
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
https://stackoverflow.com/questions/46046942
复制相似问题