使用StanfordParser从解析的句子中获取类型化的依存关系 [英] Using StanfordParser to get typed dependencies from a parsed sentence

查看:480
本文介绍了使用StanfordParser从解析的句子中获取类型化的依存关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用NLTK的StanfordParser,我可以解析这样的句子:

Using NLTK's StanfordParser, I can parse a sentence like this:

os.environ['STANFORD_PARSER'] = 'C:\jars' 
os.environ['STANFORD_MODELS'] = 'C:\jars'  
os.environ['JAVAHOME'] ='C:\ProgramData\Oracle\Java\javapath' 
parser = stanford.StanfordParser(model_path="C:\jars\englishPCFG.ser.gz")
sentences = parser.parse(("bring me a red ball",)) 
for sentence in sentences:
    sentence    

结果是:

Tree('ROOT', [Tree('S', [Tree('VP', [Tree('VB', ['Bring']),
Tree('NP', [Tree('DT', ['a']), Tree('NN', ['red'])]), Tree('NP',
[Tree('NN', ['ball'])])]), Tree('.', ['.'])])])

除上面的图形外,如何使用斯坦福解析器获取类型化的依赖关系?像这样:

How can I use the Stanford parser to get typed dependencies in addition to the above graph? Something like:

  1. root(ROOT-0,带1)
  2. iobj(bring-1,me-2)
  3. det(ball-5,a-3)
  4. amod(球5,红色4)
  5. dobj(bring-1,ball-5)

推荐答案

NLTK的StanfordParser模块没有(当前)将树包装为Stanford Dependencies转换代码.您可以使用我的库 PyStanfordDependencies ,它包装了依赖转换器.

NLTK's StanfordParser module doesn't (currently) wrap the tree to Stanford Dependencies conversion code. You can use my library PyStanfordDependencies, which wraps the dependency converter.

如果问题代码段中的nltk_treesentence,则此方法有效:

If nltk_tree is sentence from the question's code snippet, then this works:

#!/usr/bin/python3
import StanfordDependencies

# Use str() to convert the NLTK tree to Penn Treebank format
penn_treebank_tree = str(nltk_tree) 

sd = StanfordDependencies.get_instance(jar_filename='point to Stanford Parser JAR file')
converted_tree = sd.convert_tree(penn_treebank_tree)

# Print Typed Dependencies
for node in converted_tree:
    print('{}({}-{},{}-{})'.format(
            node.deprel,
            converted_tree[node.head - 1].form if node.head != 0 else 'ROOT',
            node.head,
            node.form,
            node.index))

这篇关于使用StanfordParser从解析的句子中获取类型化的依存关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆