chardistribution.py 9.0 KB

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