You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
3.3 KiB

4 years ago
  1. ######################## BEGIN LICENSE BLOCK ########################
  2. # The Original Code is Mozilla Universal charset detector code.
  3. #
  4. # The Initial Developer of the Original Code is
  5. # Netscape Communications Corporation.
  6. # Portions created by the Initial Developer are Copyright (C) 2001
  7. # the Initial Developer. All Rights Reserved.
  8. #
  9. # Contributor(s):
  10. # Mark Pilgrim - port to Python
  11. # Shy Shalom - original C code
  12. # Proofpoint, Inc.
  13. #
  14. # This library is free software; you can redistribute it and/or
  15. # modify it under the terms of the GNU Lesser General Public
  16. # License as published by the Free Software Foundation; either
  17. # version 2.1 of the License, or (at your option) any later version.
  18. #
  19. # This library is distributed in the hope that it will be useful,
  20. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  22. # Lesser General Public License for more details.
  23. #
  24. # You should have received a copy of the GNU Lesser General Public
  25. # License along with this library; if not, write to the Free Software
  26. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  27. # 02110-1301 USA
  28. ######################### END LICENSE BLOCK #########################
  29. from .charsetprober import CharSetProber
  30. from .enums import ProbingState, MachineState
  31. class MultiByteCharSetProber(CharSetProber):
  32. """
  33. MultiByteCharSetProber
  34. """
  35. def __init__(self, lang_filter=None):
  36. super(MultiByteCharSetProber, self).__init__(lang_filter=lang_filter)
  37. self.distribution_analyzer = None
  38. self.coding_sm = None
  39. self._last_char = [0, 0]
  40. def reset(self):
  41. super(MultiByteCharSetProber, self).reset()
  42. if self.coding_sm:
  43. self.coding_sm.reset()
  44. if self.distribution_analyzer:
  45. self.distribution_analyzer.reset()
  46. self._last_char = [0, 0]
  47. @property
  48. def charset_name(self):
  49. raise NotImplementedError
  50. @property
  51. def language(self):
  52. raise NotImplementedError
  53. def feed(self, byte_str):
  54. for i in range(len(byte_str)):
  55. coding_state = self.coding_sm.next_state(byte_str[i])
  56. if coding_state == MachineState.ERROR:
  57. self.logger.debug('%s %s prober hit error at byte %s',
  58. self.charset_name, self.language, i)
  59. self._state = ProbingState.NOT_ME
  60. break
  61. elif coding_state == MachineState.ITS_ME:
  62. self._state = ProbingState.FOUND_IT
  63. break
  64. elif coding_state == MachineState.START:
  65. char_len = self.coding_sm.get_current_charlen()
  66. if i == 0:
  67. self._last_char[1] = byte_str[0]
  68. self.distribution_analyzer.feed(self._last_char, char_len)
  69. else:
  70. self.distribution_analyzer.feed(byte_str[i - 1:i + 1],
  71. char_len)
  72. self._last_char[0] = byte_str[-1]
  73. if self.state == ProbingState.DETECTING:
  74. if (self.distribution_analyzer.got_enough_data() and
  75. (self.get_confidence() > self.SHORTCUT_THRESHOLD)):
  76. self._state = ProbingState.FOUND_IT
  77. return self.state
  78. def get_confidence(self):
  79. return self.distribution_analyzer.get_confidence()