如何在nltk中使用stanford NLP的“Universal dependencies,enhanced”解析器?

2024-05-16 13:22:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我可以在NLTK中使用Stanford的universal dependencies解析器,但是有没有办法在NLTK中使用universal dependencies,enhanced?如图所示Stanford Parser

谢谢


Tags: parser解析器dependenciesuniversalenhancednltkstanford办法
1条回答
网友
1楼 · 发布于 2024-05-16 13:22:11

我的方法是绕过NLTK提供的接口,直接查看它们的实现。在

找到源代码./nltk/parse/corenlp.py。在

GenericCoreNLPParser类中,有一个名为api_call的方法。当您构造依赖关系分析器对象时。您可以调用此方法来获取原始JSON对象。在

您将得到一个带有键的JSON对象:basic dependenciesenhancedDependenciesenhancedPlusPlusDependenciestokens。当得到结果的时候。我们可以编写一个简单的函数将结果解析为与调用它们的接口相同的格式。在

以下是一些片段

    def parse_sentence(self, sentence: str) -> object:
        """
        Parse a sentence for given sentence and with user-defined properties
        :type properties: object
        :type sentence: str
        The pizza is overpriced
        return : Json Object from the NLP server.
        """
        return self.get_parser().api_call(sentence)["sentences"][0]

一旦你得到结果。在

^{pr2}$

在源代码中,他们将把JSON对象转换为树结构,在大多数情况下,树结构更通用。在

Here is a demo picture

希望我的帖子能有所帮助。在

相关问题 更多 >