简体   繁体   English

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

[英]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.我想使用 NLP 库将动词从现在时转换为过去时,如下所示。

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 .我能够使用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 Python 3.7.0

spaCy version 2.3.1 spaCy 版本 2.3.1

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:据我所知,Spacy 没有内置 function 用于这种类型的转换,但您可以使用 map 现在/过去时对的扩展,并且您没有适当的“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)

Output: 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 .有一个pyinflect package,它解决了这些问题,是为spacy创建的。 It only needs to be installed pip install pyinflect and imported.只需要安装pip install pyinflect并导入即可。 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.输出: As she left the kitchen, his voice followed her.

NOTE: I'm using spacy 3注意:我正在使用 spacy 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM