pythonpython-3.xnlpstanford-nlpspacy

How can I transform verbs from present tense to past tense with using NLP library?


What I would like to do

I would like to transform verbs from present tense to past tense with using NLP library like below.

As she leaves the kitchen, his voice follows her.

#output
As she left the kitchen, his voice followed her.

Problem

There is no way to transform from present tense to past tense.

I've checked the following similar question, but they only introduced the way to transform from past tense to present tense.

What I tried to do

I was able to transform verbs from past tense to present tense using spaCy. However, there is no way to do the same thing from present tense to past tense.

text = "As she left the kitchen, his voice followed her."
doc_dep = nlp(text)
for i in range(len(doc_dep)):
    token = doc_dep[i]
    #print(token.text, token.lemma_, token.pos_, token.tag_, token.dep_) 
    if token.pos_== 'VERB':
        print(token.text)
        print(token.lemma_)
        text = text.replace(token.text, token.lemma_)
print(text)

#output
'As she leave the kitchen, his voice follow her.'

Development Environment

Python 3.7.0

spaCy version 2.3.1


Solution

  • As far as I know spaCy does not have any built-in function for this type of transformation, but you can use an extension where you map present/past tense pairs, and where you don't have the appropriate pairs 'ed' suffix for the past participle of weak verbs as below:

    verb_map = {'leave': 'left'}
    
    def make_past(token):
        return verb_map.get(token.text, token.lemma_ + 'ed')
    
    spacy.tokens.Token.set_extension('make_past', getter=make_past, force=True)
    
    text = "As she leave the kitchen, his voice follows her."
    doc_dep = nlp(text)
    for i in range(len(doc_dep)):
        token = doc_dep[i]
        if token.tag_ in ['VBP', 'VBZ']:
            print(token.text, token.lemma_, token.pos_, token.tag_) 
            text = text.replace(token.text, token._.make_past)
    print(text)
    

    Output:

    leave leave VERB VBP
    follows follow VERB VBZ
    As she left the kitchen, his voice followed her.