def __init__(self, words_or_file): self._model = load_pin_yin(False) # 加载拼音模型 self._words = {} if (not isinstance(words_or_file, str)) and isinstance(words_or_file, Iterable): for word in words_or_file: ls = chinese_to_pin_yin(self._model, word) self._words[word] = ' '.join(ls) elif os.path.exists(words_or_file): with open(words_or_file, encoding='utf-8') as f: for line in f: ls = chinese_to_pin_yin(self._model, line) self._words[line] = ' '.join(ls) else: raise TypeError('输入一个纠错列表或者文件地址') self.fuzzy_tone = fuzzy_tone self._word = None
def error_word(self, word): """将错误的词语转为正确的词语 如果有纠错文字,将纠错文字返回,没有返回原文字。 >>> ce = ChineseError(['六盘水钟山区']) >>> print(ce.error_word('我在六盘谁中三区里面')) :param word: 纠错文字 :return: 返回纠错文本或原文本 """ ls = chinese_to_pin_yin(self._model, word) total = self._flag(ls, word) return total
def error_word(self, word): """将错误的词语转为正确的词语""" ls = chinese_to_pin_yin(self._model, word) self._word = word total = self._flag(ls) return total