如何使用python nltk获取解析树? [英] how to get parse tree using python nltk?

查看:166
本文介绍了如何使用python nltk获取解析树?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下句子:

The old oak tree from India fell down.

如何使用python NLTK获得以下句子的解析树表示形式?

How can I get the following parse tree representation of the sentence using python NLTK?

(ROOT (S (NP (NP (DT The) (JJ old) (NN oak) (NN tree)) (PP (IN from) (NP (NNP India)))) (VP (VBD fell) (PRT (RP down)))))

我需要一个在网络上找不到的完整示例!

I need a complete example which I couldn't find in web!

修改

我已经遍历本书的章节,以了解使用NLTK进行解析的问题,是的,我需要一种语法来解析我没有的句子或短语.我发现了此stackoverflow帖子,其中也询问了语法分析问题,但是有那里没有令人信服的答案.

I have gone through this book chapter to learn about parsing using NLTK but the problem is, I need a grammar to parse sentences or phrases which I do not have. I have found this stackoverflow post which also asked about grammar for parsing but there is no convincing answer there.

所以,我正在寻找一个完整的答案,可以给我一个句子的解析树.

So, I am looking for a complete answer that can give me the parse tree given a sentence.

推荐答案

以下是使用StanfordCoreNLP而不是nltk的替代解决方案.在StanfordCoreNLP之上构建的库很少,我个人使用 pycorenlp 来解析句子.

Here is alternative solution using StanfordCoreNLP instead of nltk. There are few library that build on top of StanfordCoreNLP, I personally use pycorenlp to parse the sentence.

首先,您必须下载 stanford-corenlp-full 文件夹,其中具有*.jar文件里面.并在文件夹中运行服务器(默认端口为9000).

First you have to download stanford-corenlp-full folder where you have *.jar file inside. And run the server inside the folder (default port is 9000).

export CLASSPATH="`find . -name '*.jar'`"
java -mx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer [port?] # run server

然后在Python中,您可以运行以下命令来标记句子.

Then in Python, you can run the following in order to tag the sentence.

from pycorenlp import StanfordCoreNLP
nlp = StanfordCoreNLP('http://localhost:9000')

text = "The old oak tree from India fell down."

output = nlp.annotate(text, properties={
  'annotators': 'parse',
  'outputFormat': 'json'
})

print(output['sentences'][0]['parse']) # tagged output sentence

这篇关于如何使用python nltk获取解析树?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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