给出以下句子:
The old oak tree from India fell down.
如何使用 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)))))
我需要一个完整的例子,但我在网上找不到!
Edit
我已经经历过本书章节学习如何使用 NLTK 进行解析,但问题是,我需要一个语法来解析我没有的句子或短语。我已经发现这个 stackoverflow 帖子它还询问了解析语法,但没有令人信服的答案。
所以,我正在寻找一个完整的答案,它可以给我给出一个句子的解析树。
这是使用的替代解决方案StanfordCoreNLP
代替nltk
。很少有库是建立在StanfordCoreNLP
,我个人使用pycorenlp来解析句子。
首先你必须下载stanford-corenlp-full你所在的文件夹*.jar
里面的文件。并在文件夹内运行服务器(默认端口为 9000)。
export CLASSPATH="`find . -name '*.jar'`"
java -mx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer [port?] # run server
然后在 Python 中,您可以运行以下命令来标记句子。
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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)