我想做什么
我想用NLP库把动词从现在时态转换成过去式,如下所示。
As she leaves the kitchen, his voice follows her.
#output
As she left the kitchen, his voice followed her.
问题
从现在时到过去式是没有办法的。
我检查过以下类似的问题,但他们只介绍了从过去时到现在时态的转换方式。
我想做的事
我能够用spaCy把动词从过去时转换成现在时态。然而,从现在时到过去式,并没有提示去做同样的事情。
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.'
发展环境
Python 3.7.0
spaCy版本2.3.1
发布于 2021-04-28 13:38:01
我今天遇到了同样的问题。我怎样才能把动词改成“过去式”。我找到了上述解决方案的替代方案。有一个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)
产出:As she left the kitchen, his voice followed her.
注意:我用的是spacy 3
https://stackoverflow.com/questions/62945590
复制相似问题