首页 > 解决方案 > 在 Python 中使用 Stanfrod Parser 构建折叠树

问题描述

我正在按照本教程使用 Python 为句子构建依赖关系树。本教程展示了如何将 Stanfrod Parser 与 NLTK 一起使用。我已经完成了这一步,我可以构建树,如下面的代码所示。

from config import config 
from nltk.parse.stanford import StanfordDependencyParser
class ExtractTree:

def __init__(self):
    """
    Init Stanford parse tree
    """
    path_to_jar = config['libs_root'] + "stanford-parser-full-2018-10-17/stanford-parser.jar"
    path_to_models_jar = config['libs_root'] + "stanford-parser-full-2018-10-17/stanford-parser-3.9.2-models.jar"

    self.dependency_parser = StanfordDependencyParser(
        path_to_jar=path_to_jar, path_to_models_jar=path_to_models_jar,java_options=[]
    )

def extract_tree(self, sentence):
    """
    Extract the dependency tree for a sentence
    :param sentence:
    :return:
    """
    result = self.dependency_parser.raw_parse(sentence)

    return next(result)

但是,我无法弄清楚如何构建一个折叠的依赖树,如下所述:(在此处输入图像描述图像和引用来自Stanford Parser Documentation page.20)

在折叠表示中,涉及介词、连词以及关系从句的所指信息的依赖关系被折叠,以获得实词之间的直接依赖关系。

似乎java有一个API,但我找不到将它集成到Python中的方法,有什么想法吗?

标签: pythonnlpstanford-nlp

解决方案


推荐阅读