NLP | Singularizing Plural Nouns and Swapping Infinite Phrases

Let’s understand this with an example :

  1. Is our child training enough?
  2. Is are our child training enough?

The verb ‘is’ can only be used with singular nouns. For plural nouns, we use ‘are’. This problem is very common in the real world and we can correct this mistake by creating verb correction mappings that are used depending on whether there’s plural or singular noun in the chunk.

Code #1 : singularize_plural_noun() class



filter_none

edit
close

play_arrow

link
brightness_4
code

def singularize_plural_noun(chunk):
      
    nnsidx = first_chunk_index(chunk, tag_equals('NNS'))
      
    if nnsidx is not None and 
    nnsidx + 1 < len(chunk) and 
    chunk[nnsidx + 1][1][:2] == 'NN':
          
        noun, nnstag = chunk[nnsidx]
        chunk[nnsidx] = (noun.rstrip('s'), nnstag.rstrip('S'))
          
    return chunk

chevron_right


 
Code #2 : Singularizing Plurals

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :

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

The code above looks for the tag NNS to look for Plural Noun. After founding it, if the next word is a noun (determined by making sure the tag starts with NN), then we depluralize the plural noun by removing ‘s’ from the right side of both the tag and the word.

Swapping Infinite Phrases
An infinitive phrase is of the form A of B, such as ‘world of movies’. On can transform this to ‘movies world’ and it still holds the same meaning.

Code #3 : Let’s understand the swap_infinitive_phrase() class

filter_none

edit
close

play_arrow

link
brightness_4
code

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]

chevron_right


 
Code #4 : Let’s evaluate swap_infinitive_phrase

filter_none

edit
close

play_arrow

link
brightness_4
code

from transforms import swap_infinitive_phrase
  
swap_infinitive_phrase([('book', 'NN'), 
                        ('of', 'IN'), ('recipes', 'NNS')])

chevron_right


Output :

[('recipes', 'NNS'), ('book', 'NN')]


My Personal Notes arrow_drop_up

Aspire to Inspire before I expire

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.