Change language

NLP | Expanding and Removing Fragments with RegEx

| | |

This article focuses on 3 such classes:

ExpandRightRule: it adds chink (no chunk) words to the right of the chunk. 
ExpandLeftRule: it adds chink words (no chunk) to the left of the chunk. 
For ExpandLeftRule and ExpandRightRule takes — the right and left chink patterns respectively, which we want to add to the beginning and end of the chunk respectively.

UnChunkRule: it cuts off any matching chunk and becomes a chink.

Code # 1: How the code works

# Loading libraries

from nltk.chunk.regexp import ChunkRule, ExpandLeftRule

from nltk.chunk.regexp import ExpandRightRule, UnChunkRule

from nltk.chunk import RegexpChunkParser

 
# ChunkRule initialization

ur = ChunkRule ( ’ "NN" ’ , ’ single noun ’ )

  
# Initializing ExpandLeftRule

el = ExpandLeftRule ( ’"DT"’ , ’"NN"’ , ’get left determiner’ )

 
# Initializing ExpandRightRule

er = ExpandRightRule ( ’"NN"’ , ’"NNS"’ , ’get right plural noun’ )

  
# Initializing UnChunkRule

un = UnChunkRule ( ’ "DT" "NN. *" * ’ , ’unchunk everything’ )

  

chunker = RegexpChunkParser ([ur, el, er, un])

 

sent = [( ’ the’ , ’DT’ ), (  ’sushi’ , ’ NN’ ), ( ’rolls’ , ’ NNS’ )]

 
chunker.parse (sent)

Output:

 Tree (’S’, [(’ the’, ’DT’), (’ sushi’, ’NN’), (’ rolls’, ’NNS’)]) 

Note. Output — this is a flat sentence, since the UnChunkRule undoes the chunk created according to the previous rules.

How does it work?

  • Make the chunk with a noun. 
  • Extension left determinants to pieces that start with a noun. 
  • Extension regular plural nouns into pieces ending in a noun. 
  • Finally , it frees every fragment that is qualifier + noun + plural noun, resulting in the original sentence tree. 
    ​​

Code # 2: Step-by-Step Code Schema Explain.

# Loading libraries

from nltk.chunk.regexp import ChunkRule, ExpandLeftRule

from nltk.chunk.regexp import ExpandRightRule, UnChunkRule

from nltk.chunk import RegexpChunkParser

from nltk.chunk.regexp import ChunkString

from nltk.tree import Tree

  

chunk_string = ChunkString (Tree ( ’S’ , sent))

print ( "Chunk String:" , chunk_string)

 
# ChunkRule initialization

ur = ChunkRule ( ’"NN"’ , ’single noun’ )

ur. apply (chunk_string)

print ( "step 1:" , chunk_string)

 
# Initializing ExpandLeftRule

el = ExpandLeftRule ( ’"DT"’ , ’"NN"’ , ’get left determiner’ )

el. apply (chunk_string)

print ( "step 2:" , chunk_string)

 
# Initializing ExpandRightRule

er = ExpandRightRule ( ’"NN"’ , ’ "NNS" ’ , ’ get right plural noun’ )

er. apply (chunk_string)

print ( "step 3:" , chunk_string)

 
# Initializing UnChunkRule

un = UnChunkRule ( ’"DT" "NN. *" *’ , ’unchunk everything’ )

un. apply (chunk_string)

print ( "step 4:" , chunk_string)

Output:

 Chunk String: "DT" "NN" "NNS" step 1: "DT" {"NN"} "NNS" step 2: {"DT" "NN"} "NNS" step 3: {"DT" "NN" "NNS"} step 4: "DT" "NN" "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