want990
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 :)

分类:

技术点:

相关文章: