Change language

NLP | Singularization of plural nouns and replacement of endless phrases

| | |

Let’s look at this with an example:

  1. Is our child exercising enough?
  2. Is our child being trained enough?

The verb "is" can only be used with singular nouns. For plural nouns, we use “is”. This problem is very common in the real world, and we can fix it by creating corrective verbs that are used depending on whether the fragment contains a plural or singular noun.

Code # 1: class singularize_plural_noun ()

def singularize_plural_noun (chunk):

 

nnsidx = first_chunk_index (chunk, tag_equals ( ’NNS’ ))

 

  if nnsidx is   not None and  

nnsidx + 1 & lt;  len (chunk) and  

chunk [nnsidx + 1 ] [ 1 ] [: 2 ] = = ’NN’ :

  

noun, nnstag = chunk [nnsidx]

chunk [nnsidx] = (noun.rstrip ( ’s’ ), nnstag.rstrip ( ’S’ ))

 

  return chunk

Code # 2: Plural Singularization

singularize_plural_noun ([( ’recipes’ , ’ NNS’ ), ( ’ book’ , ’NN’ )])

Output:

 [(’recipe’,’ NN’), (’book’,’ NN’)] 

The code above looks for an NNS tag for plural search. Once found, if the next word is a noun (determined by making sure the tag starts with NN), we remove the plural noun by removing the "s" from the right side of the tag and the word.

Swap endless phrases
An infinitive phrase has the form A of B , for example, "movie world". It can convert it to a "movie world" and it still has the same meaning.

Code # 3: Let’s get a handle on the swap_infinitive_phrase () class

def swap_infinitive_phrase (chunk):

  def inpred (wt):

  word, tag = wt

return tag = = ’IN’ and word! = ’like’

  

  inidx = first_chunk_index (chunk, inpred)

 

if inidx is None :

return chunk

 

nnidx = first_chunk_index (chunk, 

tag_startswith ( ’NN’ ), 

start = inidx, step = - 1 ) or 0

 

return chunk [: nnidx] + chunk [inidx + 1 :] + chunk [nnidx: inidx]

Code # 4: Let’s evaluate swap_infinitive_phrase

from transforms import swap_infini tive_phrase

 

swap_infinitive_phrase ([( ’book’ , ’ NN’ ) , 

( ’of’ , ’IN’ ), ( ’recipes’ , ’ NNS’ )])

Output:

 [(’recipes’,’ NNS’), (’book’,’ 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