Thursday, September 4, 2025
HomeLanguagesNLP | Swapping Verb Phrases and Noun Cardinals

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 the 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 the first_chunk_index() function defined in order to find the verb to pivot around. 

Code #1: swap_verb_phrase class to swap verb 

Python3




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]


  Code #2 : Evaluating swap_verb_phrase 

Python3




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


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 : 

Python3




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


Output :

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

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

Code #4: Swapping noun cardinals 

Python3




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


Output :

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

[('the', 'DT'), ('10', 'CD'), ('top', 'NN')]
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32263 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11799 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11857 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6696 POSTS0 COMMENTS
Umr Jansen
6716 POSTS0 COMMENTS