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`)]