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