考虑到这些转换,我正在实现一些字符串替换器
'thou sittest' → 'you sit'
'thou walkest' → 'you walk'
'thou liest' → 'you lie'
'thou risest' → 'you rise'
如果我保持天真,在这种情况下可以使用正则表达式来查找和替换,例如 thou [a-z]+est
但问题来自以 e
结尾的英语动词,因为根据上下文我需要修剪 est
in some & trim just st
在休息
实现此目标的快速解决方案是什么?
最佳答案
可能是最快最脏的:
import nltk
words = set(nltk.corpus.words.words())
for old in 'sittest walkest liest risest'.split():
new = old[:-2]
while new and new not in words:
new = new[:-1]
print(old, new)
输出:
sittest sit
walkest walk
liest lie
risest rise
更新。稍微不那么快速和肮脏(例如适用于 rotest
→ 动词 rot
,而不是名词 rote
):
from nltk.corpus import wordnet as wn
for old in 'sittest walkest liest risest rotest'.split():
new = old[:-2]
while new and not wn.synsets(new, pos='v'):
new = new[:-1]
print(old, new)
输出:
sittest sit
walkest walk
liest lie
risest rise
rotest rot
关于python - 以 'e'结尾的英语动词处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42089517/