Change language

NLP | Splitting and combining fragments

| | |

The MergeRule: class merges two chunks based on the end of the first chunk and the beginning of the second chunk. It is indicated as "NN. *" {} & lt ;. *" i.e. the curly braces face each other.

An example of how the steps are performed

  • Starting with the sentence tree
  • Full sentence
  • The chunks are divided into several chunks
  • The chunk with the qualifier is broken into separate chunks.

Code # 1 — Building a tree

from nltk.chunk import RegexpParser

chunker = RegexpParser (r "" "

NP:
{"DT" & lt ;. *" * "NN. *"}
"NN. *"} {& lt ;. *"
& lt ;. *"} {"DT"
"NN. *" {} "NN. *"

"" " )

sent = [( ’the’ , ’ DT’ ), ( ’sushi’ , ’ NN’ ), ( ’roll’ , ’NN’ ), ( ’ was’ , ’VBD’ ), 

( ’filled’ , ’VBN’ ), ( ’ with’ , ’ IN’ ), ( ’the’ , ’DT’ ), ( ’fish’ , ’ NN’ )]

chunker.parse (sent)

Exit :

 Tree (’S’, [Tree (’ NP’, [(’the’,’ DT’), (’sushi’,’ NN’) , (’roll’,’ NN’)]), Tree (’NP’, [(’ was’, ’VBD’), (’ filled’, ’VBN’), (’ with’, ’IN’)] ), Tree (’NP’, [(’ the’, ’DT’), (’ fish’, ’NN’)])]) 

Code # 2 — Splitting and merging

# Loading libraries

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

from nltk.tree import Tree

from nltk.chunk.regexp import MergeRule, SplitRule

 
# Chunk String

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

print ( " Chunk String: " , chunk_string)

 
# Apply chunk rule

ur = ChunkRule ( ’& lt ; DT" & lt;. * & Gt; * "NN. * & Gt; ’ , ’ chunk determiner to noun’ )

ur. apply (chunk_string)

print ( "Applied ChunkRule:" , chunk_string)

 
# Splitting

sr1 = SplitRule ( ’"NN. *"’ , ’". *" ’ , ’ split after noun’ )

sr1. apply (chunk_string)

print ( "Splitting Chunk String:" , chunk_string)

 

 

sr2 = SplitRule ( ’& lt;. *"’ , ’"DT" ’ , ’ split before determiner’ )

sr2. apply (chunk_string)

print ( "Further Splitting Chunk String:" , chunk_string)

 
# Merge

mr = MergeRule ( ’"NN. *"’ , ’"NN. *"’ , ’merge nouns’ )

mr. apply (chunk_string)

print ( "Merging Chunk String:" , chunk_string)

  
# Vern go to the tree
chunk_string.to_chunkstruct ()

Exit :

 Chunk String: "DT" "NN" "NN" "VBD" "VBN" "IN" "DT" "NN" Applied ChunkRule: {"DT" "NN" "NN" "VBD" "VBN" "IN" "DT" "NN"} Splitting Chunk String: {"DT" "NN"} {"NN"} {"VBD" "VBN" "IN" "DT" "NN"} Further Splitting Chunk String: {"DT" "NN"} {"NN"} {"VBD" "VBN" "IN"} {"DT" "NN"} Merging Chunk String: {"DT" "NN" "NN"} {"VBD" "VBN" "IN"} {"DT" "NN"} Tree (’S’, [Tree (’ CHUNK’, [(’the’,’ DT’), (’sushi’,’ NN’), (’roll’,’ NN’)]) , Tree (’CHUNK’, [(’ was’, ’VBD’), (’ filled’, ’VBN’), (’ with’, ’IN’)]), Tree (’ CHUNK’, [(’the ’,’ DT’), (’fish’,’ NN’)])]) 

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