chardistribution.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. ######################## BEGIN LICENSE BLOCK ########################
  2. # The Original Code is Mozilla Communicator client 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 .euctwfreq import (EUCTW_CHAR_TO_FREQ_ORDER, EUCTW_TABLE_SIZE,
  28. EUCTW_TYPICAL_DISTRIBUTION_RATIO)
  29. from .euckrfreq import (EUCKR_CHAR_TO_FREQ_ORDER, EUCKR_TABLE_SIZE,
  30. EUCKR_TYPICAL_DISTRIBUTION_RATIO)
  31. from .gb2312freq import (GB2312_CHAR_TO_FREQ_ORDER, GB2312_TABLE_SIZE,
  32. GB2312_TYPICAL_DISTRIBUTION_RATIO)
  33. from .big5freq import (BIG5_CHAR_TO_FREQ_ORDER, BIG5_TABLE_SIZE,
  34. BIG5_TYPICAL_DISTRIBUTION_RATIO)
  35. from .jisfreq import (JIS_CHAR_TO_FREQ_ORDER, JIS_TABLE_SIZE,
  36. JIS_TYPICAL_DISTRIBUTION_RATIO)
  37. class CharDistributionAnalysis(object):
  38. ENOUGH_DATA_THRESHOLD = 1024
  39. SURE_YES = 0.99
  40. SURE_NO = 0.01
  41. MINIMUM_DATA_THRESHOLD = 3
  42. def __init__(self):
  43. # Mapping table to get frequency order from char order (get from
  44. # GetOrder())
  45. self._char_to_freq_order = None
  46. self._table_size = None # Size of above table
  47. # This is a constant value which varies from language to language,
  48. # used in calculating confidence. See
  49. # http://www.mozilla.org/projects/intl/UniversalCharsetDetection.html
  50. # for further detail.
  51. self.typical_distribution_ratio = None
  52. self._done = None
  53. self._total_chars = None
  54. self._freq_chars = None
  55. self.reset()
  56. def reset(self):
  57. """reset analyser, clear any state"""
  58. # If this flag is set to True, detection is done and conclusion has
  59. # been made
  60. self._done = False
  61. self._total_chars = 0 # Total characters encountered
  62. # The number of characters whose frequency order is less than 512
  63. self._freq_chars = 0
  64. def feed(self, char, char_len):
  65. """feed a character with known length"""
  66. if char_len == 2:
  67. # we only care about 2-bytes character in our distribution analysis
  68. order = self.get_order(char)
  69. else:
  70. order = -1
  71. if order >= 0:
  72. self._total_chars += 1
  73. # order is valid
  74. if order < self._table_size:
  75. if 512 > self._char_to_freq_order[order]:
  76. self._freq_chars += 1
  77. def get_confidence(self):
  78. """return confidence based on existing data"""
  79. # if we didn't receive any character in our consideration range,
  80. # return negative answer
  81. if self._total_chars <= 0 or self._freq_chars <= self.MINIMUM_DATA_THRESHOLD:
  82. return self.SURE_NO
  83. if self._total_chars != self._freq_chars:
  84. r = (self._freq_chars / ((self._total_chars - self._freq_chars)
  85. * self.typical_distribution_ratio))
  86. if r < self.SURE_YES:
  87. return r
  88. # normalize confidence (we don't want to be 100% sure)
  89. return self.SURE_YES
  90. def got_enough_data(self):
  91. # It is not necessary to receive all data to draw conclusion.
  92. # For charset detection, certain amount of data is enough
  93. return self._total_chars > self.ENOUGH_DATA_THRESHOLD
  94. def get_order(self, byte_str):
  95. # We do not handle characters based on the original encoding string,
  96. # but convert this encoding string to a number, here called order.
  97. # This allows multiple encodings of a language to share one frequency
  98. # table.
  99. return -1
  100. class EUCTWDistributionAnalysis(CharDistributionAnalysis):
  101. def __init__(self):
  102. super(EUCTWDistributionAnalysis, self).__init__()
  103. self._char_to_freq_order = EUCTW_CHAR_TO_FREQ_ORDER
  104. self._table_size = EUCTW_TABLE_SIZE
  105. self.typical_distribution_ratio = EUCTW_TYPICAL_DISTRIBUTION_RATIO
  106. def get_order(self, byte_str):
  107. # for euc-TW encoding, we are interested
  108. # first byte range: 0xc4 -- 0xfe
  109. # second byte range: 0xa1 -- 0xfe
  110. # no validation needed here. State machine has done that
  111. first_char = byte_str[0]
  112. if first_char >= 0xC4:
  113. return 94 * (first_char - 0xC4) + byte_str[1] - 0xA1
  114. else:
  115. return -1
  116. class EUCKRDistributionAnalysis(CharDistributionAnalysis):
  117. def __init__(self):
  118. super(EUCKRDistributionAnalysis, self).__init__()
  119. self._char_to_freq_order = EUCKR_CHAR_TO_FREQ_ORDER
  120. self._table_size = EUCKR_TABLE_SIZE
  121. self.typical_distribution_ratio = EUCKR_TYPICAL_DISTRIBUTION_RATIO
  122. def get_order(self, byte_str):
  123. # for euc-KR encoding, we are interested
  124. # first byte range: 0xb0 -- 0xfe
  125. # second byte range: 0xa1 -- 0xfe
  126. # no validation needed here. State machine has done that
  127. first_char = byte_str[0]
  128. if first_char >= 0xB0:
  129. return 94 * (first_char - 0xB0) + byte_str[1] - 0xA1
  130. else:
  131. return -1
  132. class GB2312DistributionAnalysis(CharDistributionAnalysis):
  133. def __init__(self):
  134. super(GB2312DistributionAnalysis, self).__init__()
  135. self._char_to_freq_order = GB2312_CHAR_TO_FREQ_ORDER
  136. self._table_size = GB2312_TABLE_SIZE
  137. self.typical_distribution_ratio = GB2312_TYPICAL_DISTRIBUTION_RATIO
  138. def get_order(self, byte_str):
  139. # for GB2312 encoding, we are interested
  140. # first byte range: 0xb0 -- 0xfe
  141. # second byte range: 0xa1 -- 0xfe
  142. # no validation needed here. State machine has done that
  143. first_char, second_char = byte_str[0], byte_str[1]
  144. if (first_char >= 0xB0) and (second_char >= 0xA1):
  145. return 94 * (first_char - 0xB0) + second_char - 0xA1
  146. else:
  147. return -1
  148. class Big5DistributionAnalysis(CharDistributionAnalysis):
  149. def __init__(self):
  150. super(Big5DistributionAnalysis, self).__init__()
  151. self._char_to_freq_order = BIG5_CHAR_TO_FREQ_ORDER
  152. self._table_size = BIG5_TABLE_SIZE
  153. self.typical_distribution_ratio = BIG5_TYPICAL_DISTRIBUTION_RATIO
  154. def get_order(self, byte_str):
  155. # for big5 encoding, we are interested
  156. # first byte range: 0xa4 -- 0xfe
  157. # second byte range: 0x40 -- 0x7e , 0xa1 -- 0xfe
  158. # no validation needed here. State machine has done that
  159. first_char, second_char = byte_str[0], byte_str[1]
  160. if first_char >= 0xA4:
  161. if second_char >= 0xA1:
  162. return 157 * (first_char - 0xA4) + second_char - 0xA1 + 63
  163. else:
  164. return 157 * (first_char - 0xA4) + second_char - 0x40
  165. else:
  166. return -1
  167. class SJISDistributionAnalysis(CharDistributionAnalysis):
  168. def __init__(self):
  169. super(SJISDistributionAnalysis, self).__init__()
  170. self._char_to_freq_order = JIS_CHAR_TO_FREQ_ORDER
  171. self._table_size = JIS_TABLE_SIZE
  172. self.typical_distribution_ratio = JIS_TYPICAL_DISTRIBUTION_RATIO
  173. def get_order(self, byte_str):
  174. # for sjis encoding, we are interested
  175. # first byte range: 0x81 -- 0x9f , 0xe0 -- 0xfe
  176. # second byte range: 0x40 -- 0x7e, 0x81 -- oxfe
  177. # no validation needed here. State machine has done that
  178. first_char, second_char = byte_str[0], byte_str[1]
  179. if (first_char >= 0x81) and (first_char <= 0x9F):
  180. order = 188 * (first_char - 0x81)
  181. elif (first_char >= 0xE0) and (first_char <= 0xEF):
  182. order = 188 * (first_char - 0xE0 + 31)
  183. else:
  184. return -1
  185. order = order + second_char - 0x40
  186. if second_char > 0x7F:
  187. order = -1
  188. return order
  189. class EUCJPDistributionAnalysis(CharDistributionAnalysis):
  190. def __init__(self):
  191. super(EUCJPDistributionAnalysis, self).__init__()
  192. self._char_to_freq_order = JIS_CHAR_TO_FREQ_ORDER
  193. self._table_size = JIS_TABLE_SIZE
  194. self.typical_distribution_ratio = JIS_TYPICAL_DISTRIBUTION_RATIO
  195. def get_order(self, byte_str):
  196. # for euc-JP encoding, we are interested
  197. # first byte range: 0xa0 -- 0xfe
  198. # second byte range: 0xa1 -- 0xfe
  199. # no validation needed here. State machine has done that
  200. char = byte_str[0]
  201. if char >= 0xA0:
  202. return 94 * (char - 0xA1) + byte_str[1] - 0xa1
  203. else:
  204. return -1