做多个string.replace的快速方法是什么?我正在尝试添加空格以缩短英语单词,例如
he'll -> he 'll
he's -> he 's
we're -> we 're
we've -> we 've
我也在之前和标点符号之间添加空格:
"his majesty" -> " his majesty "
his; majesty -> his ; majesty
有更快更清洁的方法吗?就目的而言,这有点太慢了,但我一直在这样做:
def removeDoubleSpace(sentence):
sentence.replace(" ", " ")
if " " in sentence:
removeDoubleSpace(sentence)
def prepro(sentence):
sentence = sentence.replace(",", " ,")
sentence = sentence.replace(";", " ; ")
sentence = sentence.replace(":", " : ")
sentence = sentence.replace("(", " ( ")
sentence = sentence.replace("(", " ) ")
sentence = sentence.replace("‘"," ‘ ")
sentence = sentence.replace('"',' " ')
sentence = sentence.replace("'re", " 're")
sentence = sentence.replace("'s", " 's")
sentence = sentence.replace("'ll", " 'll")
sentence = removeDoubleSpace(sentence)
return sentence