簡體   English   中英

如何使用 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.

問題

沒有辦法從現在時轉換為過去時。

我檢查了以下類似的問題,但他們只介紹了從過去時態轉換為現在時態的方法。

我試圖做的

我能夠使用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 沒有內置 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:

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

我今天遇到了同樣的問題。 如何將動詞更改為“過去時”形式。 我找到了上述解決方案的替代解決方案。 有一個pyinflect package,它解決了這些問題,是為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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM