I want to analyze sentences with NLTK and display their chunks as a tree. NLTK offers the method tree.draw()
to draw a tree. This following code draws a tree for the sentence "the little yellow dog barked at the cat" :
import nltk
sentence = [("the", "DT"), ("little", "JJ"), ("yellow", "JJ"), ("dog", "NN"), ("barked","VBD"), ("at", "IN"), ("the", "DT"), ("cat", "NN")]
pattern = "NP: {<DT>?<JJ>*<NN>}"
NPChunker = nltk.RegexpParser(pattern)
result = NPChunker.parse(sentence)
result.draw()
The result is this tree:
How do i get a tree with one more level like this?
You need to "level up" your non-NP words, here's a hack:
import nltk
sentence = [("the", "DT"), ("little", "JJ"), ("yellow", "JJ"), ("dog", "NN"), ("barked","VBD"), ("at", "IN"), ("the", "DT"), ("cat", "NN")]
pattern = """NP: {<DT>?<JJ>*<NN>}
VBD: {<VBD>}
IN: {<IN>}"""
NPChunker = nltk.RegexpParser(pattern)
result = NPChunker.parse(sentence)
result.draw()
[out]:
I know it's little too late to answer. But here is how I've done it. The idea is you need to convert your sentence into a tree.
import nltk
sentence = list(map(lambda sent: Tree(sent[1], children=[sent[0]]), sentence))
Then you can do the chunking afterwards.
NPChunker = nltk.RegexpParser(pattern)
result = NPChunker.parse(sentence)
result.draw()
Here's my result Tree
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.