簡體   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