Below are the steps involved for Chunking –
- Conversion of sentence to a flat tree.

- Creation of Chunk string using this tree.
- Creation of RegexpChunkParser by parsing the grammar using RegexpParser.
- Applying the created chunk rule to the ChunkString that matches the sentence into a chunk.

- Splitting the bigger chunk to a smaller chunk using the defined chunk rules.

- ChunkString is then converted back to tree, with two chunk subtrees.

Code #1: ChunkString getting modified by applying each rule.
Python3
from nltk.chunk.regexp import ChunkString, ChunkRule, ChinkRule
from nltk.tree import Tree
tree = Tree('S', [('the', 'DT'), ('book', 'NN'),
('has', 'VBZ'), ('many', 'JJ'), ('chapters', 'NNS')])
chunk_string = ChunkString(tree)
print ("Chunk String : ", chunk_string)
chunk_rule = ChunkRule('<DT><NN.*><.*>*<NN.*>', 'chunk determiners and nouns')
chunk_rule.apply(chunk_string)
print ("\nApplied ChunkRule : ", chunk_string)
ir = ChinkRule('<VB.*>', 'chink verbs')
ir.apply(chunk_string)
print ("\nApplied ChinkRule : ", chunk_string, "\n")
chunk_string.to_chunkstruct()
|
Output:
Chunk String : <<DT> <NN> <VBZ> <JJ> <NNS>
Applied ChunkRule : {<DT> <NN> <VBZ> <JJ> <NNS>}
Applied ChinkRule : {<DT> <NN>} <VBZ> {<JJ> <NNS>}
Tree('S', [Tree('CHUNK', [('the', 'DT'), ('book', 'NN')]),
('has', 'VBZ'), Tree('CHUNK', [('many', 'JJ'), ('chapters', 'NNS')])])
Note: This code works exactly in the same manner as explained in the ChunkRule steps above.
Code #2: How to this task directly with RegexpChunkParser.
Python3
from nltk.chunk.regexp import ChunkString, ChunkRule, ChinkRule
from nltk.tree import Tree
from nltk.chunk import RegexpChunkParser
tree = Tree('S', [('the', 'DT'), ('book', 'NN'),
('has', 'VBZ'), ('many', 'JJ'), ('chapters', 'NNS')])
chunk_rule = ChunkRule('<DT><NN.*><.*>*<NN.*>', 'chunk determiners and nouns')
chink_rule = ChinkRule('<VB.*>', 'chink verbs')
chunker = RegexpChunkParser([chunk_rule, chink_rule])
chunker.parse(tree)
|
Output:
Tree('S', [Tree('CHUNK', [('the', 'DT'), ('book', 'NN')]),
('has', 'VBZ'), Tree('CHUNK', [('many', 'JJ'), ('chapters', 'NNS')])])
Code #3 : Parsing with different ChunkType.
Python3
from nltk.chunk.regexp import ChunkString, ChunkRule, ChinkRule
from nltk.tree import Tree
from nltk.chunk import RegexpChunkParser
tree = Tree('S', [('the', 'DT'), ('book', 'NN'),
('has', 'VBZ'), ('many', 'JJ'), ('chapters', 'NNS')])
chunk_rule = ChunkRule('<DT><NN.*><.*>*<NN.*>', 'chunk determiners and nouns')
chink_rule = ChinkRule('<VB.*>', 'chink verbs')
chunker = RegexpChunkParser([chunk_rule, chink_rule], chunk_label ='CP')
chunker.parse(tree)
|
Output:
Tree('S', [Tree('CP', [('the', 'DT'), ('book', 'NN')]), ('has', 'VBZ'),
Tree('CP', [('many', 'JJ'), ('chapters', 'NNS')])])
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
29 Jul, 2021
Like Article
Save Article