我有两个单词列表(word_wrong和word_ok),我需要将word_wrong列表中的单词替换为word_ok中的正确单词。我需要搜索整个单词,并且不区分大小写。
仅用一个词,我就在使用(并且正在工作):
fixed = re.sub(r'\bprot\b','profit', fixed, flags=re.IGNORECASE)
我改为:
def fix_fibase(texts):
word_wrong= ['prot','protability','protable','protably','prots']
word_ok = ['profit','profitability','profitable','profitably','profits']
fixed = texts
for k in range(0,5):
fixed = re.sub(r'\b' + word_wrong[k] + r'\b',word_ok[k], fixed, flags=re.IGNORECASE)
此想法是根据元素编号用列表word_ok中的相应元素替换列表word_wrong中的元素。所以prot-> profit,protability-> profitability ...
例如:
a="prots went up"
b=fix_fibase(a)
print(b)
输出:“教授上升” 应该是“利润上升”
但不起作用...没有错误,但没有进行替换!
有什么建议吗?
感谢一百万!
答案 0 :(得分:0)
您的代码可以正常工作,您只需要返回fixed
:
import re
def fix_fibase(texts):
word_wrong= ['prot','protability','protable','protably','prots']
word_ok = ['profit','profitability','profitable','profitably','profits']
fixed = texts
for k in range(0,5):
fixed = re.sub(r'\b' + word_wrong[k] + r'\b',word_ok[k], fixed, flags=re.IGNORECASE)
return fixed
t = fix_fibase('prot is not evil')
print(t)
输出:
profit is not evil
答案 1 :(得分:0)
尝试这个:
def fix_fibase(text):
word_wrong = ['prot', 'protability', 'protable', 'protably', 'prots']
word_ok = ['profit', 'profitability', 'profitable', 'profitably', 'profits']
dict_words = dict(
zip(word_wrong, word_ok)) # it will create a dictionary with wrong word as a key and correct one as a value
words = text.split(" ")
replaced_text = " ".join([dict_words.get(word.lower(), word) for word in words])
return replaced_text
print(fix_fibase("blah prot blah")) # output: blah profit blah