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.

101 lines
3.9 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 .charsetprober import CharSetProber
  28. from .codingstatemachine import CodingStateMachine
  29. from .enums import LanguageFilter, ProbingState, MachineState
  30. from .escsm import (HZ_SM_MODEL, ISO2022CN_SM_MODEL, ISO2022JP_SM_MODEL,
  31. ISO2022KR_SM_MODEL)
  32. class EscCharSetProber(CharSetProber):
  33. """
  34. This CharSetProber uses a "code scheme" approach for detecting encodings,
  35. whereby easily recognizable escape or shift sequences are relied on to
  36. identify these encodings.
  37. """
  38. def __init__(self, lang_filter=None):
  39. super(EscCharSetProber, self).__init__(lang_filter=lang_filter)
  40. self.coding_sm = []
  41. if self.lang_filter & LanguageFilter.CHINESE_SIMPLIFIED:
  42. self.coding_sm.append(CodingStateMachine(HZ_SM_MODEL))
  43. self.coding_sm.append(CodingStateMachine(ISO2022CN_SM_MODEL))
  44. if self.lang_filter & LanguageFilter.JAPANESE:
  45. self.coding_sm.append(CodingStateMachine(ISO2022JP_SM_MODEL))
  46. if self.lang_filter & LanguageFilter.KOREAN:
  47. self.coding_sm.append(CodingStateMachine(ISO2022KR_SM_MODEL))
  48. self.active_sm_count = None
  49. self._detected_charset = None
  50. self._detected_language = None
  51. self._state = None
  52. self.reset()
  53. def reset(self):
  54. super(EscCharSetProber, self).reset()
  55. for coding_sm in self.coding_sm:
  56. if not coding_sm:
  57. continue
  58. coding_sm.active = True
  59. coding_sm.reset()
  60. self.active_sm_count = len(self.coding_sm)
  61. self._detected_charset = None
  62. self._detected_language = None
  63. @property
  64. def charset_name(self):
  65. return self._detected_charset
  66. @property
  67. def language(self):
  68. return self._detected_language
  69. def get_confidence(self):
  70. if self._detected_charset:
  71. return 0.99
  72. else:
  73. return 0.00
  74. def feed(self, byte_str):
  75. for c in byte_str:
  76. for coding_sm in self.coding_sm:
  77. if not coding_sm or 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