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

Discussão em 'Python' iniciado por Stack, Março 13, 2025.

  1. Stack

    Stack Membro Participativo

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

    Continue reading...

Compartilhe esta Página