2

做多个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
4

1 回答 1

5

您可以使用一些正则表达式来完成相同的任务:

import re

# Replace multiple consecutive spaces with a single space
# Example: "One Two  Three    Four!" -> "One Two Three Four!"
sentence = re.sub(' +', ' ', sentence)    

# Surround each instance ; : ( ) ‘ and " with spaces
# Example: '"Hello;(w)o:r‘ld"' -> " Hello ;  ( w ) o : r ‘ ld "
sentence = re.sub('([;:()‘"])', ' \\1 ', sentence)

# Insert a space before each instance of , 's 're and 'll
# Example: "you'll they're, we're" -> "you 'll they 're , we 're"
sentence = re.sub("(,|'s|'re|'ll)", ' \\1', sentence)

return sentence
于 2012-10-03T03:40:05.520 回答