繁体   English   中英

NLTK Python中的类型化依赖项解析

[英]Typed Dependency Parsing in NLTK Python

我有一句话

"I shot an elephant in my sleep"

句子的类型依赖性是

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)

在Python中,如何使用Stanford Parser(或任何解析器)和NLTK(最好是,什么都可以)来获得类型依赖?

注意-我知道这与这个问题非常相似。 但是,没有好的答案。

斯坦福解析器存在一个python包装器,您可以在此处获得。

它将为您提供句子的依存关系树。


编辑:

我这里假设你推出了服务器说这里 我还假定您已经安装了jsonrpclib。

以下代码将产生您想要的:

import json
import jsonrpclib

class StanfordNLP:
    def __init__(self, port_number=8080):
        self.server = jsonrpclib.Server("http://localhost:%d" % port_number)

    def parse(self, text):
        return json.loads(self.server.parse(text))

nlp = StanfordNLP()
sentence = 'I shot an elephant in my sleep'
result = nlp.parse(sentence)
result['sentences'][0]['indexeddependencies']

>>>
['root', 'ROOT-0', 'shot-2']
['nsubj', 'shot-2', 'I-1']
['det', 'elephant-4', 'an-3']
['dobj', 'shot-2', 'elephant-4']
['poss', 'sleep-7', 'my-6']
['prep_in', 'shot-2', 'sleep-7']

编辑2:

现在,斯坦福解析器具有HTTP API 因此,不再需要python包装器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM