繁体   English   中英

如何使用 python nltk 获取解析树?

[英]how to get parse tree using python nltk?

给定以下句子:

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)))))

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


编辑

我已经阅读了这本书的章节来学习使用 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

要使用 nltk 库获取解析树,您可以使用以下代码

# Import required libraries
import nltk
nltk.download('punkt')
nltk.download('averaged_perceptron_tagger')
from nltk import pos_tag, word_tokenize, RegexpParser

# Example text
sample_text = "The quick brown fox jumps over the lazy dog"

# Find all parts of speech in above sentence
tagged = pos_tag(word_tokenize(sample_text))

#Extract all parts of speech from any text
chunker = RegexpParser("""
                    NP: {<DT>?<JJ>*<NN>} #To extract Noun Phrases
                    P: {<IN>}            #To extract Prepositions
                    V: {<V.*>}           #To extract Verbs
                    PP: {<p> <NP>}       #To extract Prepositional Phrases
                    VP: {<V> <NP|PP>*}   #To extract Verb Phrases
                    """)

# Print all parts of speech in above sentence
output = chunker.parse(tagged)
print("After Extracting\n", output)
# output looks something like this
 (S
  (NP The/DT old/JJ oak/NN)
  (NP tree/NN)
  (P from/IN)
  India/NNP
  (VP (V fell/VBD))
  down/RB
  ./.)

您还可以获得这棵树的图表

# To draw the parse tree
output.draw()

Output 图看起来像这样在此处输入图像描述

旧的问题,但你可以一起使用NLTK bllipparser 这是nltk的一个较长的例子 经过一番摆弄后,我自己使用了以下内容:

要安装(已安装nltk):

sudo python3 -m nltk.downloader bllip_wsj_no_aux
pip3 install bllipparser

使用:

from nltk.data import find
from bllipparser import RerankingParser

model_dir = find('models/bllip_wsj_no_aux').path
parser = RerankingParser.from_unified_model_dir(model_dir)

best = parser.parse("The old oak tree from India fell down.")

print(best.get_reranker_best())
print(best.get_parser_best())

输出:

-80.435259246021 -23.831876011253 (S1 (S (NP (NP (DT The) (JJ old) (NN oak) (NN tree)) (PP (IN from) (NP (NNP India)))) (VP (VBD fell) (PRT (RP down))) (. .)))
-79.703612178593 -24.505514522222 (S1 (S (NP (NP (DT The) (JJ old) (NN oak) (NN tree)) (PP (IN from) (NP (NNP India)))) (VP (VBD fell) (ADVP (RB down))) (. .)))

可以在此线程中找到对 OP 问题的全面回答: https://stackoverflow.com/a/75122588/4293361

暂无
暂无

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

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