如何使用 NLP 库将动词从现在时转换为过去时?

6 python nlp stanford-nlp python-3.x spacy

我想做的事

我想使用 NLP 库将动词从现在时转换为过去时,如下所示。

As she leaves the kitchen, his voice follows her.

#output
As she left the kitchen, his voice followed her.
Run Code Online (Sandbox Code Playgroud)

问题

无法从现在时转换为过去时。

我检查了以下类似的问题,但他们只介绍了从过去时态转换为现在时态的方法。

我尝试做什么

我能够使用spaCy将动词从过去时态转换为现在时态。然而,从现在时到过去时,没有任何线索可以做同样的事情。

As she leaves the kitchen, his voice follows her.

#output
As she left the kitchen, his voice followed her.
Run Code Online (Sandbox Code Playgroud)

开发环境

Python 3.7.0

spaCy 版本 2.3.1

kri*_*bek 8

我今天遇到了同样的问题。如何将动词更改为“过去时”形式?我找到了上述解决方案的替代解决方案。有一个pyinflect包可以解决此类问题,并且是为spacy. 只需要安装pip install pyinflect并导入即可。无需添加扩展。

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)
Run Code Online (Sandbox Code Playgroud)

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

注意:我使用的是 spacy 3


nim*_*ous 1

据我所知,Spacy 没有用于这种类型转换的内置函数,但您可以使用扩展来映射现在/过去时对,并且您没有适当的对“ed”后缀弱动词的过去分词如下:

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)
Run Code Online (Sandbox Code Playgroud)

输出:

leave leave VERB VBP
follows follow VERB VBZ
As she left the kitchen, his voice followed her.
Run Code Online (Sandbox Code Playgroud)