将英文缩写扩展成正常英文的方法

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

posted @ 2021-04-09 12:01  bigdog  阅读(144)  评论(0编辑  收藏  举报