





















import re class RegexpReplacer(object): def __init__(self): self.patterns = [ (r"won\'t", "will not"), (r"can\'t", "can not"), (r"n\'t", " not"), (r"\'re", " are"), (r"\'s", " is"), (r"\'d", " would"), (r"\'ll", " will"), (r"\'t", " not"), (r"\'ve", " have"), (r"\'m", " am"), (r"CBA", "China Basketball Association")] def replace(self,text): s = text for(pattern,repl) in self.patterns: (s,count)=re.subn(pattern,repl,s) return s
调用方法:
replacer= RegexpReplacer() str = replacer.replace("She must've gone to the market but she didn't go") print(str)
Enjoy :)
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。