Path: blob/master/thirdparty/chardet/chardistribution.py
2992 views
######################## BEGIN LICENSE BLOCK ########################1# The Original Code is Mozilla Communicator client code.2#3# The Initial Developer of the Original Code is4# Netscape Communications Corporation.5# Portions created by the Initial Developer are Copyright (C) 19986# the Initial Developer. All Rights Reserved.7#8# Contributor(s):9# Mark Pilgrim - port to Python10#11# This library is free software; you can redistribute it and/or12# modify it under the terms of the GNU Lesser General Public13# License as published by the Free Software Foundation; either14# version 2.1 of the License, or (at your option) any later version.15#16# This library is distributed in the hope that it will be useful,17# but WITHOUT ANY WARRANTY; without even the implied warranty of18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU19# Lesser General Public License for more details.20#21# You should have received a copy of the GNU Lesser General Public22# License along with this library; if not, write to the Free Software23# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA24# 02110-1301 USA25######################### END LICENSE BLOCK #########################2627from .euctwfreq import (EUCTW_CHAR_TO_FREQ_ORDER, EUCTW_TABLE_SIZE,28EUCTW_TYPICAL_DISTRIBUTION_RATIO)29from .euckrfreq import (EUCKR_CHAR_TO_FREQ_ORDER, EUCKR_TABLE_SIZE,30EUCKR_TYPICAL_DISTRIBUTION_RATIO)31from .gb2312freq import (GB2312_CHAR_TO_FREQ_ORDER, GB2312_TABLE_SIZE,32GB2312_TYPICAL_DISTRIBUTION_RATIO)33from .big5freq import (BIG5_CHAR_TO_FREQ_ORDER, BIG5_TABLE_SIZE,34BIG5_TYPICAL_DISTRIBUTION_RATIO)35from .jisfreq import (JIS_CHAR_TO_FREQ_ORDER, JIS_TABLE_SIZE,36JIS_TYPICAL_DISTRIBUTION_RATIO)373839class CharDistributionAnalysis(object):40ENOUGH_DATA_THRESHOLD = 102441SURE_YES = 0.9942SURE_NO = 0.0143MINIMUM_DATA_THRESHOLD = 34445def __init__(self):46# Mapping table to get frequency order from char order (get from47# GetOrder())48self._char_to_freq_order = None49self._table_size = None # Size of above table50# This is a constant value which varies from language to language,51# used in calculating confidence. See52# http://www.mozilla.org/projects/intl/UniversalCharsetDetection.html53# for further detail.54self.typical_distribution_ratio = None55self._done = None56self._total_chars = None57self._freq_chars = None58self.reset()5960def reset(self):61"""reset analyser, clear any state"""62# If this flag is set to True, detection is done and conclusion has63# been made64self._done = False65self._total_chars = 0 # Total characters encountered66# The number of characters whose frequency order is less than 51267self._freq_chars = 06869def feed(self, char, char_len):70"""feed a character with known length"""71if char_len == 2:72# we only care about 2-bytes character in our distribution analysis73order = self.get_order(char)74else:75order = -176if order >= 0:77self._total_chars += 178# order is valid79if order < self._table_size:80if 512 > self._char_to_freq_order[order]:81self._freq_chars += 18283def get_confidence(self):84"""return confidence based on existing data"""85# if we didn't receive any character in our consideration range,86# return negative answer87if self._total_chars <= 0 or self._freq_chars <= self.MINIMUM_DATA_THRESHOLD:88return self.SURE_NO8990if self._total_chars != self._freq_chars:91r = (self._freq_chars / ((self._total_chars - self._freq_chars)92* self.typical_distribution_ratio))93if r < self.SURE_YES:94return r9596# normalize confidence (we don't want to be 100% sure)97return self.SURE_YES9899def got_enough_data(self):100# It is not necessary to receive all data to draw conclusion.101# For charset detection, certain amount of data is enough102return self._total_chars > self.ENOUGH_DATA_THRESHOLD103104def get_order(self, byte_str):105# We do not handle characters based on the original encoding string,106# but convert this encoding string to a number, here called order.107# This allows multiple encodings of a language to share one frequency108# table.109return -1110111112class EUCTWDistributionAnalysis(CharDistributionAnalysis):113def __init__(self):114super(EUCTWDistributionAnalysis, self).__init__()115self._char_to_freq_order = EUCTW_CHAR_TO_FREQ_ORDER116self._table_size = EUCTW_TABLE_SIZE117self.typical_distribution_ratio = EUCTW_TYPICAL_DISTRIBUTION_RATIO118119def get_order(self, byte_str):120# for euc-TW encoding, we are interested121# first byte range: 0xc4 -- 0xfe122# second byte range: 0xa1 -- 0xfe123# no validation needed here. State machine has done that124first_char = byte_str[0]125if first_char >= 0xC4:126return 94 * (first_char - 0xC4) + byte_str[1] - 0xA1127else:128return -1129130131class EUCKRDistributionAnalysis(CharDistributionAnalysis):132def __init__(self):133super(EUCKRDistributionAnalysis, self).__init__()134self._char_to_freq_order = EUCKR_CHAR_TO_FREQ_ORDER135self._table_size = EUCKR_TABLE_SIZE136self.typical_distribution_ratio = EUCKR_TYPICAL_DISTRIBUTION_RATIO137138def get_order(self, byte_str):139# for euc-KR encoding, we are interested140# first byte range: 0xb0 -- 0xfe141# second byte range: 0xa1 -- 0xfe142# no validation needed here. State machine has done that143first_char = byte_str[0]144if first_char >= 0xB0:145return 94 * (first_char - 0xB0) + byte_str[1] - 0xA1146else:147return -1148149150class GB2312DistributionAnalysis(CharDistributionAnalysis):151def __init__(self):152super(GB2312DistributionAnalysis, self).__init__()153self._char_to_freq_order = GB2312_CHAR_TO_FREQ_ORDER154self._table_size = GB2312_TABLE_SIZE155self.typical_distribution_ratio = GB2312_TYPICAL_DISTRIBUTION_RATIO156157def get_order(self, byte_str):158# for GB2312 encoding, we are interested159# first byte range: 0xb0 -- 0xfe160# second byte range: 0xa1 -- 0xfe161# no validation needed here. State machine has done that162first_char, second_char = byte_str[0], byte_str[1]163if (first_char >= 0xB0) and (second_char >= 0xA1):164return 94 * (first_char - 0xB0) + second_char - 0xA1165else:166return -1167168169class Big5DistributionAnalysis(CharDistributionAnalysis):170def __init__(self):171super(Big5DistributionAnalysis, self).__init__()172self._char_to_freq_order = BIG5_CHAR_TO_FREQ_ORDER173self._table_size = BIG5_TABLE_SIZE174self.typical_distribution_ratio = BIG5_TYPICAL_DISTRIBUTION_RATIO175176def get_order(self, byte_str):177# for big5 encoding, we are interested178# first byte range: 0xa4 -- 0xfe179# second byte range: 0x40 -- 0x7e , 0xa1 -- 0xfe180# no validation needed here. State machine has done that181first_char, second_char = byte_str[0], byte_str[1]182if first_char >= 0xA4:183if second_char >= 0xA1:184return 157 * (first_char - 0xA4) + second_char - 0xA1 + 63185else:186return 157 * (first_char - 0xA4) + second_char - 0x40187else:188return -1189190191class SJISDistributionAnalysis(CharDistributionAnalysis):192def __init__(self):193super(SJISDistributionAnalysis, self).__init__()194self._char_to_freq_order = JIS_CHAR_TO_FREQ_ORDER195self._table_size = JIS_TABLE_SIZE196self.typical_distribution_ratio = JIS_TYPICAL_DISTRIBUTION_RATIO197198def get_order(self, byte_str):199# for sjis encoding, we are interested200# first byte range: 0x81 -- 0x9f , 0xe0 -- 0xfe201# second byte range: 0x40 -- 0x7e, 0x81 -- oxfe202# no validation needed here. State machine has done that203first_char, second_char = byte_str[0], byte_str[1]204if (first_char >= 0x81) and (first_char <= 0x9F):205order = 188 * (first_char - 0x81)206elif (first_char >= 0xE0) and (first_char <= 0xEF):207order = 188 * (first_char - 0xE0 + 31)208else:209return -1210order = order + second_char - 0x40211if second_char > 0x7F:212order = -1213return order214215216class EUCJPDistributionAnalysis(CharDistributionAnalysis):217def __init__(self):218super(EUCJPDistributionAnalysis, self).__init__()219self._char_to_freq_order = JIS_CHAR_TO_FREQ_ORDER220self._table_size = JIS_TABLE_SIZE221self.typical_distribution_ratio = JIS_TYPICAL_DISTRIBUTION_RATIO222223def get_order(self, byte_str):224# for euc-JP encoding, we are interested225# first byte range: 0xa0 -- 0xfe226# second byte range: 0xa1 -- 0xfe227# no validation needed here. State machine has done that228char = byte_str[0]229if char >= 0xA0:230return 94 * (char - 0xA1) + byte_str[1] - 0xa1231else:232return -1233234235