|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +from __future__ import with_statement |
| 4 | + |
| 5 | +from .pyver import * |
| 6 | +import json |
| 7 | + |
| 8 | +__all__ = ['BaseRecoder'] |
| 9 | + |
| 10 | +html_parser = HTMLParser.HTMLParser() |
| 11 | + |
| 12 | +class BaseRecoder(object): |
| 13 | + base_dir = None |
| 14 | + file_3grams = None |
| 15 | + file_plus_words = None |
| 16 | + codings = None |
| 17 | + |
| 18 | + funcs = [ |
| 19 | + [lambda *args, **kwargs: kwargs['text'].encode(kwargs['coding'], errors=kwargs['errors']), True, (unicode_type,)], |
| 20 | + [lambda *args, **kwargs: kwargs['text'].decode(kwargs['coding'], errors=kwargs['errors']), True, (encoded_type,)], |
| 21 | + [lambda *args, **kwargs: unquote_plus(kwargs['text']), False, (encoded_type,)], |
| 22 | + [lambda *args, **kwargs: unquote_plus(kwargs['text'].replace('=', '%')), False, (encoded_type,)], |
| 23 | + [lambda *args, **kwargs: html_parser.unescape(kwargs['text']), False, (unicode_type,)], |
| 24 | + ] |
| 25 | + |
| 26 | + regular_error_classes = ( |
| 27 | + UnicodeError, |
| 28 | + AttributeError, # for py3 support |
| 29 | + ) |
| 30 | + |
| 31 | + def __init__(self, depth=2, errors='ignore', use_plus_words=False): |
| 32 | + self.depth = depth |
| 33 | + self.errors = errors |
| 34 | + self.use_plus_words = use_plus_words |
| 35 | + self.last_transform = None |
| 36 | + |
| 37 | + with open(self.file_3grams) as f: |
| 38 | + self.grams = json.load(f) |
| 39 | + |
| 40 | + with open(self.file_plus_words) as f: |
| 41 | + self.plus_words = set(json.load(f)) |
| 42 | + |
| 43 | + def _contains_plus_word(self, text): |
| 44 | + for word in self.plus_words: |
| 45 | + if (' ' + word + ' ') in text: |
| 46 | + return True |
| 47 | + return False |
| 48 | + |
| 49 | + def _iter(self, text, depth, transform=lambda _text: _text): |
| 50 | + if depth <= 0: |
| 51 | + raise StopIteration |
| 52 | + for func, coding_dependent, allowed_types in self.funcs: |
| 53 | + if not isinstance(text, allowed_types): continue |
| 54 | + for coding in (self.codings if coding_dependent else ['fake_coding']): |
| 55 | + try: |
| 56 | + fixed_text = func(text=text, coding=coding, errors=self.errors) |
| 57 | + new_transform = lambda _text: func(text=transform(_text), coding=coding, errors=self.errors) |
| 58 | + yield fixed_text, new_transform |
| 59 | + for sub_fixed_text, sub_new_transform in self._iter(fixed_text, depth - 1, new_transform): |
| 60 | + yield sub_fixed_text, sub_new_transform |
| 61 | + except self.regular_error_classes: |
| 62 | + pass |
| 63 | + |
| 64 | + def _calc_weight(self, text): |
| 65 | + weight = 0.0 |
| 66 | + count = 0 |
| 67 | + |
| 68 | + for i in range_iterator(len(text) - 2): |
| 69 | + gram = text[i:i+3] |
| 70 | + weight += self.grams.get(gram, 0.0) |
| 71 | + count += 1 |
| 72 | + return (weight / count) if count else 0.0 |
| 73 | + |
| 74 | + def fix(self, unicode_text): |
| 75 | + max_weight = self._calc_weight(unicode_text.lower()) |
| 76 | + max_text = unicode_text |
| 77 | + for fixed_text, transform in self._iter(unicode_text, self.depth): |
| 78 | + if not isinstance(fixed_text, unicode_type): |
| 79 | + continue |
| 80 | + fixed_text = fixed_text.lower() |
| 81 | + weight = self._calc_weight(fixed_text) |
| 82 | + if weight > max_weight and (not self.use_plus_words or self._contains_plus_word(fixed_text)): |
| 83 | + max_weight = weight |
| 84 | + max_text = transform(unicode_text) |
| 85 | + self.last_transform = transform |
| 86 | + return max_text |
| 87 | + |
| 88 | + def fix_common(self, unicode_text): |
| 89 | + max_weight = self._calc_weight(unicode_text.lower()) |
| 90 | + max_text = unicode_text |
| 91 | + |
| 92 | + for ce in self.codings: |
| 93 | + for cd in self.codings: |
| 94 | + if ce == cd: continue |
| 95 | + try: |
| 96 | + fixed_text = unicode_text.encode(ce, errors=self.errors).decode(cd, errors=self.errors).lower() |
| 97 | + weight = self._calc_weight(fixed_text) |
| 98 | + if weight > max_weight and (not self.use_plus_words or self._contains_plus_word(fixed_text)): |
| 99 | + max_weight = weight |
| 100 | + max_text = unicode_text.encode(ce, errors=self.errors).decode(cd, errors=self.errors) |
| 101 | + self.last_transform = lambda text: text.encode(ce, errors=self.errors).decode(cd, errors=self.errors) |
| 102 | + except self.regular_error_classes: |
| 103 | + pass |
| 104 | + return max_text |
0 commit comments