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

Viewed 2706

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 clew 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

2 Answers

As far as I know Spacy does not have 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.

I came across the same issue today. How can I change my verbs to the "past tense" form. I found an alternative solution to the one above. There is a pyinflect package, that solves such problems and was created for spacy. It only needs to be installed pip install pyinflect and imported. There is no need to add extensions.

import spacy
import pyinflect

nlp = spacy.load("en_core_web_sm")

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._.inflect("VBD"))
print(text)

Outputs: As she left the kitchen, his voice followed her.

NOTE: I'm using spacy 3

Related