Change language

NLP | Chunking rules

| |

Following are the steps for Chunking —

  • Convert a sentence to a plane tree. 
  • Creation Chunk strings using this tree.
  • Create a RegexpChunkParser by parsing grammar using RegexpParser.
  • Applying the generated chunk rule to the ChunkString that matches the clause in the chunk. 
  • Separation a larger chunk to a smaller chunk using certain chunk rules. 
  • Then ChunkString is converted back to a tree with two chunk subtrees. 

Code # 1: ChunkString is modified by applying each rule.

# Loading libraries

from nltk.chunk.regexp import ChunkString, ChunkRule, ChinkRule

from nltk.tree import Tree

 
# ChunkString () starts with a flat tree

tree = Tree ( ’ S’ , [( ’the’ , ’DT’ ), ( ’ book’ , ’ NN’ ),

( ’ has’ , ’VBZ’ ), ( ’ many’ , ’JJ’ ), ( ’ chapters’ , ’NNS’ )])

 
# Initializing ChunkString ()

chunk_string = ChunkString (tree)

print ( "Chunk String:" , chunk_string)

 
# ChunkRule initialization

chunk_rule = ChunkRule ( ’"DT" "NN. *" & lt;. *" * "NN. *"’ , ’ chunk determiners and nouns’ )

chunk_rule. apply (chunk_string)

print ( " Applied ChunkRule: " , chunk_string)

 
# Another ChinkRule

ir = ChinkRule ( ’ "VB. *" ’ , ’chink verbs’ )

ir . apply (chunk_string)

print ( "Applied ChinkRule:" , chunk_string, "" )

 
# Return to chunk subtree
chunk_string.to_chunkstruct ()

Exit:

 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 as described in the ChunkRule steps above.

Code # 2: How to perform this task directly with RegexpChunkParser.

# Loading libraries

from nltk.chunk.regexp import ChunkString, ChunkRule, ChinkRule

from nltk.tree import Tree

from nltk.chunk import RegexpChunkParser

  
# ChunkString () starts with a flat tree

tree = Tree ( ’S’ , [( ’the’ , ’ DT ’ ), ( ’ book’ , ’NN’ ),

  ( ’has’ , ’ VBZ’ ), ( ’many’ , ’JJ’ ), ( ’ chapters’ , ’NNS’ )])

  
# ChunkRule initialization

chunk_rule = ChunkRule ( ’"DT" "NN. *" & lt ;. *" * "NN. *" ’ , ’ chunk determiners and nouns’ )

 

 
# Other ChinkRule

chink_rule = ChinkRule ( ’"VB. *"’ , ’chink verbs’ )

  
# Applying RegexpChunkParser

chunker = RegexpChunkParser ([chunk_rule, chink_rule])

chunker.parse (tree)

Exit:

 Tree (’S’, [Tree (’ CHUNK’, [(’the’,’ DT’), (’book’,’ NN’)]), (’has’,’ VBZ’), Tree (’CHUNK’, [(’ many’, ’JJ ’), (’ chapters’, ’NNS’)])]) 

Code # 3: Parse with a different ChunkType.

# Loading libraries

from nltk.chunk.regexp import ChunkString, ChunkRule, ChinkRule

from nltk.tree import Tree

from nltk.chunk import RegexpChunkParser

 
# ChunkString () starts with a flat tree

tree = Tree ( ’S’ , [( ’ the’ , ’ DT’ ), ( ’book’ , ’NN’ ),

( ’has’ , ’VBZ’ ), ( ’ many’ , ’JJ’ ), ( ’ chapters’ , ’NNS’ )])

 
# ChunkRule initialization

chunk_rule = ChunkRule ( ’"DT" "NN. *" & lt;. *" * "NN. *"’ , ’chunk determiners and nouns’ )

  

  
# Another ChinkRule

chink_rule = ChinkRule ( ’"VB. *"’ , ’chink verbs’ )

 
# Using RegexpChunkParser

chunker = RegexpChunkParser ([chunk_rule, chink_rule], chunk_label = ’CP’ )

chunker.parse (tree)

Exit:

 Tree (’S’, [Tree (’ CP’, [(’the’,’ DT’), (’book’,’ NN’)]), (’has’,’ VBZ’), Tree ( ’CP’, [(’ many’, ’JJ’), (’ chapters’, ’NNS’)])]) 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically