escprober.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 Optional, Union
  28. from .charsetprober import CharSetProber
  29. from .codingstatemachine import CodingStateMachine
  30. from .enums import LanguageFilter, MachineState, ProbingState
  31. from .escsm import (
  32. HZ_SM_MODEL,
  33. ISO2022CN_SM_MODEL,
  34. ISO2022JP_SM_MODEL,
  35. ISO2022KR_SM_MODEL,
  36. )
  37. class EscCharSetProber(CharSetProber):
  38. """
  39. This CharSetProber uses a "code scheme" approach for detecting encodings,
  40. whereby easily recognizable escape or shift sequences are relied on to
  41. identify these encodings.
  42. """
  43. def __init__(self, lang_filter: LanguageFilter = LanguageFilter.NONE) -> None:
  44. super().__init__(lang_filter=lang_filter)
  45. self.coding_sm = []
  46. if self.lang_filter & LanguageFilter.CHINESE_SIMPLIFIED:
  47. self.coding_sm.append(CodingStateMachine(HZ_SM_MODEL))
  48. self.coding_sm.append(CodingStateMachine(ISO2022CN_SM_MODEL))
  49. if self.lang_filter & LanguageFilter.JAPANESE:
  50. self.coding_sm.append(CodingStateMachine(ISO2022JP_SM_MODEL))
  51. if self.lang_filter & LanguageFilter.KOREAN:
  52. self.coding_sm.append(CodingStateMachine(ISO2022KR_SM_MODEL))
  53. self.active_sm_count = 0
  54. self._detected_charset: Optional[str] = None
  55. self._detected_language: Optional[str] = None
  56. self._state = ProbingState.DETECTING
  57. self.reset()
  58. def reset(self) -> None:
  59. super().reset()
  60. for coding_sm in self.coding_sm:
  61. coding_sm.active = True
  62. coding_sm.reset()
  63. self.active_sm_count = len(self.coding_sm)
  64. self._detected_charset = None
  65. self._detected_language = None
  66. @property
  67. def charset_name(self) -> Optional[str]:
  68. return self._detected_charset
  69. @property
  70. def language(self) -> Optional[str]:
  71. return self._detected_language
  72. def get_confidence(self) -> float:
  73. return 0.99 if self._detected_charset else 0.00
  74. def feed(self, byte_str: Union[bytes, bytearray]) -> ProbingState:
  75. for c in byte_str:
  76. for coding_sm in self.coding_sm:
  77. if not coding_sm.active:
  78. continue
  79. coding_state = coding_sm.next_state(c)
  80. if coding_state == MachineState.ERROR:
  81. coding_sm.active = False
  82. self.active_sm_count -= 1
  83. if self.active_sm_count <= 0:
  84. self._state = ProbingState.NOT_ME
  85. return self.state
  86. elif coding_state == MachineState.ITS_ME:
  87. self._state = ProbingState.FOUND_IT
  88. self._detected_charset = coding_sm.get_coding_state_machine()
  89. self._detected_language = coding_sm.language
  90. return self.state
  91. return self.state