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

问题描述 投票:0回答:2

我想做什么 我想通过使用NLP库将动词从现在的时态转换为过去时态。

As she leaves the kitchen, his voice follows her. #output As she left the kitchen, his voice followed her.

问题
没有办法从现在时转变为过去时态。
我已经检查了以下类似的问题,但它们仅引入了转变的方式
过去时态以表达时态。

使用NLTK和WordNet;如何将简单的时态动词转换为当前,过去或过去的分词形式?

我试图做什么 我能够使用

Python3.7.0

Spacy2.3.1

版本

要将动词更改为“过去时态”形式,可以使用解决此类问题并为
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)
python python-3.x nlp stanford-nlp spacy
2个回答
9
投票
输出:

As she left the kitchen, his voice followed her.


注:我正在使用Spacy3.

据我所知,Spacy对这种类型的转换没有任何内置功能,但是您可以在绘制映射时/过去时态以及没有适当的对'ED'后缀的延伸范围内使用以下弱动词的``Ed 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)

输出:

leave leave VERB VBP follows follow VERB VBZ As she left the kitchen, his voice followed her.


2
投票
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.