斯坦福CoreNLP pars的树结构

2024-06-01 01:31:09 发布

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

我正在尝试运行StanfordCoreNLP解析器,我有以下代码:

from pycorenlp import StanfordCoreNLP

nlp = StanfordCoreNLP('http://localhost:9000')

def depparse(text):
    parsed=""
    output = nlp.annotate(text, properties={
      'annotators': 'depparse',
      'outputFormat': 'json'
      })

    for i in output["sentences"]:
        for j in i["basicDependencies"]:
            parsed=parsed+str(j["dep"]+'('+ j["governorGloss"]+' ')+str(j["dependentGloss"]+')'+' ')
        return parsed
text='I shot an elephant in my sleep'
depparse(text)

输出如下: 'ROOT(ROOT shot) nsubj(shot I) det(elephant an) dobj(shot elephant) case(sleep in) nmod:poss(sleep my) nmod(shot sleep) '

为了将关系转换为树,我遇到了一个stackoverflow postStanford NLP parse tree format。但是,解析器的输出在“括号解析(树)”中。因此,我不知道如何才能实现它。我也尝试更改outputformat,但它给出了一个错误。在

我还发现了Python - Generate a dictionary(tree) from a list of tuples并实现了它

^{pr2}$

我得到以下输出[{'Name': 'shot', 'Relationship': 'ROOT', 'children': [{'Name': 'I', 'Relationship': 'nsubj'}, {'Name': 'elephant', 'Relationship': 'dobj', 'children': [{'Name': 'an', 'Relationship': 'det'}]}, {'Name': 'sleep', 'Relationship': 'nmod', 'children': [{'Name': 'in', 'Relationship': 'case'}, {'Name': 'my', 'Relationship': 'nmod:poss'}]}]}]


Tags: textnameinanmysleeprootparsed
1条回答
网友
1楼 · 发布于 2024-06-01 01:31:09

确实有点离题(这不是对你最初的问题的回答,而是对你最后一条评论的回答)。将其作为答案发布,因为代码不太适合注释。但只需稍微更改depparse函数,就可以获得所需的格式:

def depparse(text):
parsed=""
output = nlp.annotate(text, properties={
  'annotators': 'depparse',
  'outputFormat': 'json'
  })
for i in output['sentences']: # not sure if there can be multiple items here. If so, it just returns the first one currently.
    return [tuple((dep['dep'], dep['governorGloss'], dep['dependentGloss'])) for dep in i['basicDependencies']]

相关问题 更多 >