The ``UniversalDetector`` class underlies the ``chardet.detect`` function and coordinates all of the different charset probers. To get a ``dict`` containing an encoding and its confidence, you can simply run: .. code:: u = UniversalDetector() u.feed(so
| 49 | |
| 50 | |
| 51 | class UniversalDetector(object): |
| 52 | """ |
| 53 | The ``UniversalDetector`` class underlies the ``chardet.detect`` function |
| 54 | and coordinates all of the different charset probers. |
| 55 | |
| 56 | To get a ``dict`` containing an encoding and its confidence, you can simply |
| 57 | run: |
| 58 | |
| 59 | .. code:: |
| 60 | |
| 61 | u = UniversalDetector() |
| 62 | u.feed(some_bytes) |
| 63 | u.close() |
| 64 | detected = u.result |
| 65 | |
| 66 | """ |
| 67 | |
| 68 | MINIMUM_THRESHOLD = 0.20 |
| 69 | HIGH_BYTE_DETECTOR = re.compile(b'[\x80-\xFF]') |
| 70 | ESC_DETECTOR = re.compile(b'(\033|~{)') |
| 71 | WIN_BYTE_DETECTOR = re.compile(b'[\x80-\x9F]') |
| 72 | ISO_WIN_MAP = {'iso-8859-1': 'Windows-1252', |
| 73 | 'iso-8859-2': 'Windows-1250', |
| 74 | 'iso-8859-5': 'Windows-1251', |
| 75 | 'iso-8859-6': 'Windows-1256', |
| 76 | 'iso-8859-7': 'Windows-1253', |
| 77 | 'iso-8859-8': 'Windows-1255', |
| 78 | 'iso-8859-9': 'Windows-1254', |
| 79 | 'iso-8859-13': 'Windows-1257'} |
| 80 | |
| 81 | def __init__(self, lang_filter=LanguageFilter.ALL): |
| 82 | self._esc_charset_prober = None |
| 83 | self._charset_probers = [] |
| 84 | self.result = None |
| 85 | self.done = None |
| 86 | self._got_data = None |
| 87 | self._input_state = None |
| 88 | self._last_char = None |
| 89 | self.lang_filter = lang_filter |
| 90 | self.logger = logging.getLogger(__name__) |
| 91 | self._has_win_bytes = None |
| 92 | self.reset() |
| 93 | |
| 94 | def reset(self): |
| 95 | """ |
| 96 | Reset the UniversalDetector and all of its probers back to their |
| 97 | initial states. This is called by ``__init__``, so you only need to |
| 98 | call this directly in between analyses of different documents. |
| 99 | """ |
| 100 | self.result = {'encoding': None, 'confidence': 0.0, 'language': None} |
| 101 | self.done = False |
| 102 | self._got_data = False |
| 103 | self._has_win_bytes = False |
| 104 | self._input_state = InputState.PURE_ASCII |
| 105 | self._last_char = b'' |
| 106 | if self._esc_charset_prober: |
| 107 | self._esc_charset_prober.reset() |
| 108 | for prober in self._charset_probers: |
no outgoing calls
no test coverage detected
searching dependent graphs…