macromanprober.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. ######################## BEGIN LICENSE BLOCK ########################
  2. # This code was modified from latin1prober.py by Rob Speer <rob@lumino.so>.
  3. # The Original Code is Mozilla Universal charset detector code.
  4. #
  5. # The Initial Developer of the Original Code is
  6. # Netscape Communications Corporation.
  7. # Portions created by the Initial Developer are Copyright (C) 2001
  8. # the Initial Developer. All Rights Reserved.
  9. #
  10. # Contributor(s):
  11. # Rob Speer - adapt to MacRoman encoding
  12. # Mark Pilgrim - port to Python
  13. # Shy Shalom - original C code
  14. #
  15. # This library is free software; you can redistribute it and/or
  16. # modify it under the terms of the GNU Lesser General Public
  17. # License as published by the Free Software Foundation; either
  18. # version 2.1 of the License, or (at your option) any later version.
  19. #
  20. # This library is distributed in the hope that it will be useful,
  21. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  23. # Lesser General Public License for more details.
  24. #
  25. # You should have received a copy of the GNU Lesser General Public
  26. # License along with this library; if not, write to the Free Software
  27. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  28. # 02110-1301 USA
  29. ######################### END LICENSE BLOCK #########################
  30. from typing import List, Union
  31. from .charsetprober import CharSetProber
  32. from .enums import ProbingState
  33. FREQ_CAT_NUM = 4
  34. UDF = 0 # undefined
  35. OTH = 1 # other
  36. ASC = 2 # ascii capital letter
  37. ASS = 3 # ascii small letter
  38. ACV = 4 # accent capital vowel
  39. ACO = 5 # accent capital other
  40. ASV = 6 # accent small vowel
  41. ASO = 7 # accent small other
  42. ODD = 8 # character that is unlikely to appear
  43. CLASS_NUM = 9 # total classes
  44. # The change from Latin1 is that we explicitly look for extended characters
  45. # that are infrequently-occurring symbols, and consider them to always be
  46. # improbable. This should let MacRoman get out of the way of more likely
  47. # encodings in most situations.
  48. # fmt: off
  49. MacRoman_CharToClass = (
  50. OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, # 00 - 07
  51. OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, # 08 - 0F
  52. OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, # 10 - 17
  53. OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, # 18 - 1F
  54. OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, # 20 - 27
  55. OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, # 28 - 2F
  56. OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, # 30 - 37
  57. OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, # 38 - 3F
  58. OTH, ASC, ASC, ASC, ASC, ASC, ASC, ASC, # 40 - 47
  59. ASC, ASC, ASC, ASC, ASC, ASC, ASC, ASC, # 48 - 4F
  60. ASC, ASC, ASC, ASC, ASC, ASC, ASC, ASC, # 50 - 57
  61. ASC, ASC, ASC, OTH, OTH, OTH, OTH, OTH, # 58 - 5F
  62. OTH, ASS, ASS, ASS, ASS, ASS, ASS, ASS, # 60 - 67
  63. ASS, ASS, ASS, ASS, ASS, ASS, ASS, ASS, # 68 - 6F
  64. ASS, ASS, ASS, ASS, ASS, ASS, ASS, ASS, # 70 - 77
  65. ASS, ASS, ASS, OTH, OTH, OTH, OTH, OTH, # 78 - 7F
  66. ACV, ACV, ACO, ACV, ACO, ACV, ACV, ASV, # 80 - 87
  67. ASV, ASV, ASV, ASV, ASV, ASO, ASV, ASV, # 88 - 8F
  68. ASV, ASV, ASV, ASV, ASV, ASV, ASO, ASV, # 90 - 97
  69. ASV, ASV, ASV, ASV, ASV, ASV, ASV, ASV, # 98 - 9F
  70. OTH, OTH, OTH, OTH, OTH, OTH, OTH, ASO, # A0 - A7
  71. OTH, OTH, ODD, ODD, OTH, OTH, ACV, ACV, # A8 - AF
  72. OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, # B0 - B7
  73. OTH, OTH, OTH, OTH, OTH, OTH, ASV, ASV, # B8 - BF
  74. OTH, OTH, ODD, OTH, ODD, OTH, OTH, OTH, # C0 - C7
  75. OTH, OTH, OTH, ACV, ACV, ACV, ACV, ASV, # C8 - CF
  76. OTH, OTH, OTH, OTH, OTH, OTH, OTH, ODD, # D0 - D7
  77. ASV, ACV, ODD, OTH, OTH, OTH, OTH, OTH, # D8 - DF
  78. OTH, OTH, OTH, OTH, OTH, ACV, ACV, ACV, # E0 - E7
  79. ACV, ACV, ACV, ACV, ACV, ACV, ACV, ACV, # E8 - EF
  80. ODD, ACV, ACV, ACV, ACV, ASV, ODD, ODD, # F0 - F7
  81. ODD, ODD, ODD, ODD, ODD, ODD, ODD, ODD, # F8 - FF
  82. )
  83. # 0 : illegal
  84. # 1 : very unlikely
  85. # 2 : normal
  86. # 3 : very likely
  87. MacRomanClassModel = (
  88. # UDF OTH ASC ASS ACV ACO ASV ASO ODD
  89. 0, 0, 0, 0, 0, 0, 0, 0, 0, # UDF
  90. 0, 3, 3, 3, 3, 3, 3, 3, 1, # OTH
  91. 0, 3, 3, 3, 3, 3, 3, 3, 1, # ASC
  92. 0, 3, 3, 3, 1, 1, 3, 3, 1, # ASS
  93. 0, 3, 3, 3, 1, 2, 1, 2, 1, # ACV
  94. 0, 3, 3, 3, 3, 3, 3, 3, 1, # ACO
  95. 0, 3, 1, 3, 1, 1, 1, 3, 1, # ASV
  96. 0, 3, 1, 3, 1, 1, 3, 3, 1, # ASO
  97. 0, 1, 1, 1, 1, 1, 1, 1, 1, # ODD
  98. )
  99. # fmt: on
  100. class MacRomanProber(CharSetProber):
  101. def __init__(self) -> None:
  102. super().__init__()
  103. self._last_char_class = OTH
  104. self._freq_counter: List[int] = []
  105. self.reset()
  106. def reset(self) -> None:
  107. self._last_char_class = OTH
  108. self._freq_counter = [0] * FREQ_CAT_NUM
  109. # express the prior that MacRoman is a somewhat rare encoding;
  110. # this can be done by starting out in a slightly improbable state
  111. # that must be overcome
  112. self._freq_counter[2] = 10
  113. super().reset()
  114. @property
  115. def charset_name(self) -> str:
  116. return "MacRoman"
  117. @property
  118. def language(self) -> str:
  119. return ""
  120. def feed(self, byte_str: Union[bytes, bytearray]) -> ProbingState:
  121. byte_str = self.remove_xml_tags(byte_str)
  122. for c in byte_str:
  123. char_class = MacRoman_CharToClass[c]
  124. freq = MacRomanClassModel[(self._last_char_class * CLASS_NUM) + char_class]
  125. if freq == 0:
  126. self._state = ProbingState.NOT_ME
  127. break
  128. self._freq_counter[freq] += 1
  129. self._last_char_class = char_class
  130. return self.state
  131. def get_confidence(self) -> float:
  132. if self.state == ProbingState.NOT_ME:
  133. return 0.01
  134. total = sum(self._freq_counter)
  135. confidence = (
  136. 0.0
  137. if total < 0.01
  138. else (self._freq_counter[3] - self._freq_counter[1] * 20.0) / total
  139. )
  140. confidence = max(confidence, 0.0)
  141. # lower the confidence of MacRoman so that other more accurate
  142. # detector can take priority.
  143. confidence *= 0.73
  144. return confidence