user11749375
user11749375

Reputation:

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

Upvotes: 8

Views: 5899

Answers (3)

Franck Dernoncourt
Franck Dernoncourt

Reputation: 83437

One can use https://github.com/bendichter/tenseflow:

From the readme, to install:

git clone https://github.com/bendichter/tenseflow.git
cd tenseflow
pip install .
sudo python -m spacy download en

To run:

from tenseflow import change_tense
change_tense('I will go to the store.', 'past')
u'I went to the store.'

Note, also from the readme:

Unfortunately, the package is only compatible with python 2.7 and 3.6.

Upvotes: 0

krisograbek
krisograbek

Reputation: 1792

To change verbs to the "past tense" form, one can use the 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.

Upvotes: 9

nimbous
nimbous

Reputation: 1537

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.

Upvotes: 3

Related Questions