sjisprober.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 typing import Union
  28. from .chardistribution import SJISDistributionAnalysis
  29. from .codingstatemachine import CodingStateMachine
  30. from .enums import MachineState, ProbingState
  31. from .jpcntx import SJISContextAnalysis
  32. from .mbcharsetprober import MultiByteCharSetProber
  33. from .mbcssm import SJIS_SM_MODEL
  34. class SJISProber(MultiByteCharSetProber):
  35. def __init__(self) -> None:
  36. super().__init__()
  37. self.coding_sm = CodingStateMachine(SJIS_SM_MODEL)
  38. self.distribution_analyzer = SJISDistributionAnalysis()
  39. self.context_analyzer = SJISContextAnalysis()
  40. self.reset()
  41. def reset(self) -> None:
  42. super().reset()
  43. self.context_analyzer.reset()
  44. @property
  45. def charset_name(self) -> str:
  46. return self.context_analyzer.charset_name
  47. @property
  48. def language(self) -> str:
  49. return "Japanese"
  50. def feed(self, byte_str: Union[bytes, bytearray]) -> ProbingState:
  51. assert self.coding_sm is not None
  52. assert self.distribution_analyzer is not None
  53. for i, byte in enumerate(byte_str):
  54. coding_state = self.coding_sm.next_state(byte)
  55. if coding_state == MachineState.ERROR:
  56. self.logger.debug(
  57. "%s %s prober hit error at byte %s",
  58. self.charset_name,
  59. self.language,
  60. i,
  61. )
  62. self._state = ProbingState.NOT_ME
  63. break
  64. if coding_state == MachineState.ITS_ME:
  65. self._state = ProbingState.FOUND_IT
  66. break
  67. if coding_state == MachineState.START:
  68. char_len = self.coding_sm.get_current_charlen()
  69. if i == 0:
  70. self._last_char[1] = byte
  71. self.context_analyzer.feed(
  72. self._last_char[2 - char_len :], char_len
  73. )
  74. self.distribution_analyzer.feed(self._last_char, char_len)
  75. else:
  76. self.context_analyzer.feed(
  77. byte_str[i + 1 - char_len : i + 3 - char_len], char_len
  78. )
  79. self.distribution_analyzer.feed(byte_str[i - 1 : i + 1], char_len)
  80. self._last_char[0] = byte_str[-1]
  81. if self.state == ProbingState.DETECTING:
  82. if self.context_analyzer.got_enough_data() and (
  83. self.get_confidence() > self.SHORTCUT_THRESHOLD
  84. ):
  85. self._state = ProbingState.FOUND_IT
  86. return self.state
  87. def get_confidence(self) -> float:
  88. assert self.distribution_analyzer is not None
  89. context_conf = self.context_analyzer.get_confidence()
  90. distrib_conf = self.distribution_analyzer.get_confidence()
  91. return max(context_conf, distrib_conf)