如何使用 NLP 库将动词从现在时态转换为过去时态?
How can I transform verbs from present tense to past tense with using NLP library?
我想做什么
我想使用 NLP 库将动词从现在时转换为过去时。
As she leaves the kitchen, his voice follows her.
#output
As she left the kitchen, his voice followed her.
问题
无法从现在时转换为过去时。
我查了下面类似的问题,但是他们只是介绍了从
过去时到现在时。
- Using NLTK and WordNet; how do I convert simple tense verb into its present, past or past participle form?
我尝试做的事情
我能够使用 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
据我所知,Spacy 没有用于此类转换的内置函数,但您可以使用扩展来映射 present/past 时态对,以及您没有合适的扩展对 '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.
我今天遇到了同样的问题。我怎样才能把我的动词改成“过去时”的形式。我找到了上述解决方案的替代解决方案。有一个 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
我想做什么
我想使用 NLP 库将动词从现在时转换为过去时。
As she leaves the kitchen, his voice follows her.
#output
As she left the kitchen, his voice followed her.
问题
无法从现在时转换为过去时。
我查了下面类似的问题,但是他们只是介绍了从 过去时到现在时。
- Using NLTK and WordNet; how do I convert simple tense verb into its present, past or past participle form?
我尝试做的事情
我能够使用 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
据我所知,Spacy 没有用于此类转换的内置函数,但您可以使用扩展来映射 present/past 时态对,以及您没有合适的扩展对 '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.
我今天遇到了同样的问题。我怎样才能把我的动词改成“过去时”的形式。我找到了上述解决方案的替代解决方案。有一个 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