NLP | Swapping Verb Phrases and Noun Cardinals

Need to swap verb phrases?
To eliminate the passive voice from particular phrases. This normalization is helpful with frequency analysis, by counting two apparently different phrases as the same phrase.

The code below is swap_verb_phrase class that swaps the left-hand side of the chunk with the right-hand side, using the verb as the pivot point. It uses first_chunk_index() function defined in order to find the verb to pivot around.

Code #1 : swap_verb_phrase class to swap verb



filter_none

edit
close

play_arrow

link
brightness_4
code

def swap_verb_phrase(chunk):
    def vbpred(wt):
        word, tag = wt
        return tag != 'VBG' and tag.startswith('VB') and len(tag) > 2
      
    vbidx = first_chunk_index(chunk, vbpred)
      
    if vbidx is None:
        return chunk
      
    return chunk[vbidx + 1:] + chunk[:vbidx]

chevron_right


 
Code #2 : Evaluating swap_verb_phrase

filter_none

edit
close

play_arrow

link
brightness_4
code

swap_verb_phrase([('the', 'DT'), ('book', 'NN'),
               ('was', 'VBD'), ('great', 'JJ')])

chevron_right


Output :

[('great', 'JJ'), ('the', 'DT'), ('book', 'NN')]

The code is not pivoting around a gerund because they are commonly used to describe nouns.

Code #3 :

filter_none

edit
close

play_arrow

link
brightness_4
code

swap_verb_phrase([('this', 'DT'), 
                  ('gripping', 'VBG'), ('book', 'NN'), 
                  ('is', 'VBZ'), ('fantastic', 'JJ')])

chevron_right


Output :

[('fantastic', 'JJ'), ('this', 'DT'), ('gripping', 'VBG'), ('book', 'NN')]

Swapping noun cardinals :
Cardinals in a chunk refer to a number and are tagged as CD. These cardinals occur before acardinals noun. Swapping noun cardianals is useful to put the cardinal before the noun.

Code #4 : Swapping noun cardinals

filter_none

edit
close

play_arrow

link
brightness_4
code

swap_noun_cardinal([('Dec.', 'NNP'), ('10', 'CD')])
  
swap_noun_cardinal([('the', 'DT'), ('top', 'NN'), ('10', 'CD')])

chevron_right


Output :

[('10', 'CD'), ('Dec.', 'NNP')]

[('the', 'DT'), ('10', 'CD'), ('top', '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.