我正在嘗試使用 text.replace() 替換字串中的單詞。它運作良好,直到使用復數替換詞如下:
def replacing():
texter = []
del texter[:]
repl = ['diabetes', 'mellitus', 'dm', ]
it = ''
try:
it = iter(np.array(repl))
except:
pass
txt = "tell me if its can also cause coronavirus"
for i in range(len(np.array(repl1))):
try:
p = it.__next__()
x = txt.replace("its", p)
texter.append(x)
x = txt.replace("it", p)
texter.append(x)
xxx = txt.replace("them", p)
texter.append(xxx)
xxxx = txt.replace("the same", p)
texter.append(xxx)
xxxxx = txt.replace("this", p)
texter.append(xxx)
except StopIteration:
break
mm = list(OrderedDict.fromkeys(texter))
print (mm)
replacing()
這是這段代碼的結果:
['tell me if diabetes can also cause coronavirus', 'tell me if diabetess can also cause coronavirus', 'tell me if mellitus can also cause coronavirus', 'tell me if mellituss can also cause coronavirus', 'tell me if dm can also cause coronavirus', 'tell me if dms can also cause coronavirus']
請注意,拼寫錯誤將單詞替換為“diabetess”而不是“diabetes”,將“mellituss”替換為“mellitus”,將“dms”替換為“dm”。
我注意到關鍵字“它和它的”,因為相似最終會帶來錯誤。
我怎樣才能避免這種情況
uj5u.com熱心網友回復:
問題是您要分別替換“它”和“其”。txt.replace("it", p)創建一個txt用 p 替換“it”的副本,所以“its”變成了“diabetess”。使用re模塊指定要替換“it”或“its”。您的 for 回圈如下所示:
for i in range(len(np.array(repl))):
try:
p = it.__next__()
x = re.sub("its|it", p, txt)
texter.append(x)
xxx = txt.replace("them", p)
texter.append(xxx)
xxxx = txt.replace("the same", p)
texter.append(xxx)
xxxxx = txt.replace("this", p)
texter.append(xxx)
except StopIteration:
break
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/343568.html
