NLP | Verb Forms Correction

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 : Defining the verb correction mappings



filter_none

edit
close

play_arrow

link
brightness_4
code

# singular to plural mapping
  
plural_verb_forms = {
        ('is', 'VBZ'): ('are', 'VBP'),
        ('was', 'VBD'): ('were', 'VBD')
        }
  
# plural to singular mapping
singular_verb_forms = {
        ('are', 'VBP'): ('is', 'VBZ'),
        ('were', 'VBD'): ('was', 'VBD')
        }

chevron_right


We are searching the chunk for the position of the first tagged word using the first_chunk_index() method. This method had a parameter ‘pred’ that takes a (word, tag) tuple and returns True or False.

Code #2 : first_chunk_index()

filter_none

edit
close

play_arrow

link
brightness_4
code

def first_chunk_index(chunk, pred, start = 0, step = 1):
      
    l = len(chunk)
    end = l if step > 0 else -1
      
    for i in range(start, end, step):
        if pred(chunk[i]):
            return i
          
    return None

chevron_right


The predicate function in the code below returns True if the tag in the (word, tag) argument starts with a given tag prefix. Else, false.

Code #3 :

filter_none

edit
close

play_arrow

link
brightness_4
code

def tag_startswith(prefix):
    def f(wt):
        return wt[1].startswith(prefix)
    return f

chevron_right


Code #4 : Let’s correct the verb forms

filter_none

edit
close

play_arrow

link
brightness_4
code

from transforms import correct_verbs
  
print ("Corrected verb forms : \n"
       correct_verbs([('is', 'VBZ'), ('our', 'PRP$'), 
                      ('children', 'NNS'), ('learning', 'VBG')]))

chevron_right


Output :

Corrected verb forms : 
[('are', 'VBP'), ('our', 'PRP$'), ('children', 'NNS'), ('learning',
'VBG')]


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.