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.

92 lines
3.7 KiB

4 years ago
  1. ######################## BEGIN LICENSE BLOCK ########################
  2. # The Original Code is mozilla.org 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) 1998
  7. # the Initial Developer. All Rights Reserved.
  8. #
  9. # Contributor(s):
  10. # Mark Pilgrim - port to Python
  11. #
  12. # This library is free software; you can redistribute it and/or
  13. # modify it under the terms of the GNU Lesser General Public
  14. # License as published by the Free Software Foundation; either
  15. # version 2.1 of the License, or (at your option) any later version.
  16. #
  17. # This library is distributed in the hope that it will be useful,
  18. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. # Lesser General Public License for more details.
  21. #
  22. # You should have received a copy of the GNU Lesser General Public
  23. # License along with this library; if not, write to the Free Software
  24. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  25. # 02110-1301 USA
  26. ######################### END LICENSE BLOCK #########################
  27. from .mbcharsetprober import MultiByteCharSetProber
  28. from .codingstatemachine import CodingStateMachine
  29. from .chardistribution import SJISDistributionAnalysis
  30. from .jpcntx import SJISContextAnalysis
  31. from .mbcssm import SJIS_SM_MODEL
  32. from .enums import ProbingState, MachineState
  33. class SJISProber(MultiByteCharSetProber):
  34. def __init__(self):
  35. super(SJISProber, self).__init__()
  36. self.coding_sm = CodingStateMachine(SJIS_SM_MODEL)
  37. self.distribution_analyzer = SJISDistributionAnalysis()
  38. self.context_analyzer = SJISContextAnalysis()
  39. self.reset()
  40. def reset(self):
  41. super(SJISProber, self).reset()
  42. self.context_analyzer.reset()
  43. @property
  44. def charset_name(self):
  45. return self.context_analyzer.charset_name
  46. @property
  47. def language(self):
  48. return "Japanese"
  49. def feed(self, byte_str):
  50. for i in range(len(byte_str)):
  51. coding_state = self.coding_sm.next_state(byte_str[i])
  52. if coding_state == MachineState.ERROR:
  53. self.logger.debug('%s %s prober hit error at byte %s',
  54. self.charset_name, self.language, i)
  55. self._state = ProbingState.NOT_ME
  56. break
  57. elif coding_state == MachineState.ITS_ME:
  58. self._state = ProbingState.FOUND_IT
  59. break
  60. elif coding_state == MachineState.START:
  61. char_len = self.coding_sm.get_current_charlen()
  62. if i == 0:
  63. self._last_char[1] = byte_str[0]
  64. self.context_analyzer.feed(self._last_char[2 - char_len:],
  65. char_len)
  66. self.distribution_analyzer.feed(self._last_char, char_len)
  67. else:
  68. self.context_analyzer.feed(byte_str[i + 1 - char_len:i + 3
  69. - char_len], char_len)
  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.context_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. context_conf = self.context_analyzer.get_confidence()
  80. distrib_conf = self.distribution_analyzer.get_confidence()
  81. return max(context_conf, distrib_conf)