latin1prober.py 5.3 KB

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