| 1 |
// +----------------------------------------------------------------------+ |
| 2 |
// | chsdet - Charset Detector Library | |
| 3 |
// +----------------------------------------------------------------------+ |
| 4 |
// | Copyright (C) 2006, Nick Yakowlew http://chsdet.sourceforge.net | |
| 5 |
// +----------------------------------------------------------------------+ |
| 6 |
// | Based on Mozilla sources http://www.mozilla.org/projects/intl/ | |
| 7 |
// +----------------------------------------------------------------------+ |
| 8 |
// | This library is free software; you can redistribute it and/or modify | |
| 9 |
// | it under the terms of the GNU General Public License as published by | |
| 10 |
// | the Free Software Foundation; either version 2 of the License, or | |
| 11 |
// | (at your option) any later version. | |
| 12 |
// | This library is distributed in the hope that it will be useful | |
| 13 |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 14 |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 15 |
// | See the GNU Lesser General Public License for more details. | |
| 16 |
// | http://www.opensource.org/licenses/lgpl-license.php | |
| 17 |
// +----------------------------------------------------------------------+ |
| 18 |
// |
| 19 |
// $Id: CharDistribution.pas,v 1.4 2013/04/23 19:47:10 ya_nick Exp $ |
| 20 |
|
| 21 |
unit CharDistribution; |
| 22 |
|
| 23 |
{$R-} |
| 24 |
|
| 25 |
interface |
| 26 |
|
| 27 |
uses |
| 28 |
nsCore; |
| 29 |
|
| 30 |
type |
| 31 |
TCharDistributionAnalysis = class (TObject) |
| 32 |
protected |
| 33 |
//mDone: PRBool; (*If this flag is set to PR_TRUE, detection is done and conclusion has been made*) |
| 34 |
// YaN: nice idea. Unfortunately is not implemented :(( |
| 35 |
mFreqChars: uInt32; (*The number of characters whose frequency order is less than 512*) |
| 36 |
mTotalChars: uInt32; (*Total character encounted.*) |
| 37 |
|
| 38 |
mCharToFreqOrder: pInt16; (*Mapping table to get frequency order from char order |
| 39 |
(get from GetOrder())*) |
| 40 |
|
| 41 |
mTableSize: uInt32; (*Size of above table*) |
| 42 |
mTypicalDistributionRatio: double;(*This is a constant value varies from language to language, |
| 43 |
it is used in calculating confidence. |
| 44 |
See my paper for further detail.*) |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
//we do not handle character base on its original encoding string, but |
| 49 |
//convert this encoding string to a number, here called order. |
| 50 |
//This allow multiple encoding of a language to share one frequency table |
| 51 |
function GetOrder(str: pAnsiChar): int32; virtual; abstract; |
| 52 |
(*feed a block of data and do distribution analysis*) |
| 53 |
// function HandleData(const aBuf: pAnsiChar; aLen: uInt32): eProbingState; virtual; abstract; |
| 54 |
public |
| 55 |
destructor Destroy; override; |
| 56 |
(*This function is for future extension. |
| 57 |
Caller can use this function to control analyser's behavior*) |
| 58 |
// procedure SetOpion(); virtual; abstract; |
| 59 |
(*return confidence base on existing data*) |
| 60 |
function GetConfidence: float; virtual; |
| 61 |
(*Reset analyser, clear any state *) |
| 62 |
procedure Reset; virtual; |
| 63 |
|
| 64 |
(*It is not necessary to receive all data to draw conclusion. |
| 65 |
For charset detection, certain amount of data is enough*) |
| 66 |
function GotEnoughData: Boolean; |
| 67 |
|
| 68 |
(*Feed a character with known length*) |
| 69 |
procedure HandleOneChar(aStr: pAnsiChar; aCharLen: uInt32); virtual; |
| 70 |
|
| 71 |
end; |
| 72 |
|
| 73 |
TEUCTWDistributionAnalysis = class (TCharDistributionAnalysis) |
| 74 |
(*for euc-TW encoding, we are interested *) |
| 75 |
(* first byte range: 0xc4 -- 0xfe*) |
| 76 |
(* second byte range: 0xa1 -- 0xfe*) |
| 77 |
(*no validation needed here. State machine has done that*) |
| 78 |
protected |
| 79 |
function GetOrder(str: pAnsiChar): int32; override; |
| 80 |
public |
| 81 |
constructor Create; reintroduce; |
| 82 |
end; |
| 83 |
|
| 84 |
TEUCKRDistributionAnalysis = class (TCharDistributionAnalysis) |
| 85 |
(*for euc-KR encoding, we are interested *) |
| 86 |
(* first byte range: 0xb0 -- 0xfe*) |
| 87 |
(* second byte range: 0xa1 -- 0xfe*) |
| 88 |
(*no validation needed here. State machine has done that*) |
| 89 |
protected |
| 90 |
function GetOrder(str: pAnsiChar): int32; override; |
| 91 |
public |
| 92 |
constructor Create; reintroduce; |
| 93 |
end; |
| 94 |
|
| 95 |
TGB2312DistributionAnalysis = class (TCharDistributionAnalysis) |
| 96 |
(*for GB2312 encoding, we are interested *) |
| 97 |
(* first byte range: 0xb0 -- 0xfe*) |
| 98 |
(* second byte range: 0xa1 -- 0xfe*) |
| 99 |
(*no validation needed here. State machine has done that*) |
| 100 |
protected |
| 101 |
function GetOrder(str: pAnsiChar): int32; override; |
| 102 |
public |
| 103 |
constructor Create; reintroduce; |
| 104 |
end; |
| 105 |
|
| 106 |
TBig5DistributionAnalysis = class (TCharDistributionAnalysis) |
| 107 |
(*for big5 encoding, we are interested *) |
| 108 |
(* first byte range: 0xa4 -- 0xfe*) |
| 109 |
(* second byte range: 0x40 -- 0x7e , 0xa1 -- 0xfe*) |
| 110 |
(*no validation needed here. State machine has done that*) |
| 111 |
protected |
| 112 |
function GetOrder(str: pAnsiChar): int32; override; |
| 113 |
public |
| 114 |
constructor Create; reintroduce; |
| 115 |
end; |
| 116 |
|
| 117 |
TSJISDistributionAnalysis = class (TCharDistributionAnalysis) |
| 118 |
(*for sjis encoding, we are interested *) |
| 119 |
(* first byte range: 0x81 -- 0x9f , 0xe0 -- 0xfe*) |
| 120 |
(* second byte range: 0x40 -- 0x7e, 0x81 -- oxfe*) |
| 121 |
(*no validation needed here. State machine has done that*) |
| 122 |
protected |
| 123 |
function GetOrder(str: pAnsiChar): int32; override; |
| 124 |
public |
| 125 |
constructor Create; reintroduce; |
| 126 |
end; |
| 127 |
|
| 128 |
TEUCJPDistributionAnalysis = class (TCharDistributionAnalysis) |
| 129 |
(*for euc-JP encoding, we are interested *) |
| 130 |
(* first byte range: 0xa0 -- 0xfe*) |
| 131 |
(* second byte range: 0xa1 -- 0xfe*) |
| 132 |
(*no validation needed here. State machine has done that*) |
| 133 |
protected |
| 134 |
function GetOrder(str: pAnsiChar): int32; override; |
| 135 |
public |
| 136 |
constructor Create; reintroduce; |
| 137 |
end; |
| 138 |
|
| 139 |
implementation |
| 140 |
uses |
| 141 |
JISFreq, |
| 142 |
Big5Freq, |
| 143 |
EUCKRFreq, |
| 144 |
EUCTWFreq, |
| 145 |
GB2312Freq; |
| 146 |
|
| 147 |
destructor TCharDistributionAnalysis.Destroy; |
| 148 |
begin |
| 149 |
mCharToFreqOrder := nil; |
| 150 |
|
| 151 |
inherited; |
| 152 |
end; |
| 153 |
|
| 154 |
procedure TCharDistributionAnalysis.HandleOneChar(aStr: pAnsiChar; aCharLen: uInt32); |
| 155 |
var |
| 156 |
order: integer; |
| 157 |
begin |
| 158 |
(*we only care about 2-bytes character in our distribution analysis*) |
| 159 |
if (aCharLen=2) then |
| 160 |
order := GetOrder(aStr) |
| 161 |
else |
| 162 |
order := -1; |
| 163 |
if order >= 0 then |
| 164 |
begin |
| 165 |
inc(mTotalChars); (*order is valid*) |
| 166 |
if order < integer(mTableSize) then |
| 167 |
begin |
| 168 |
if 512 > aInt16(mCharToFreqOrder)[order] then |
| 169 |
inc(mFreqChars); |
| 170 |
end; |
| 171 |
end; |
| 172 |
end; |
| 173 |
|
| 174 |
function TCharDistributionAnalysis.GetConfidence: float; |
| 175 |
var |
| 176 |
r: float; |
| 177 |
begin |
| 178 |
(*if we didn't receive any character in our consideration range, return negative answer*) |
| 179 |
if mTotalChars <= 0 then |
| 180 |
begin |
| 181 |
Result := SURE_NO; |
| 182 |
exit; |
| 183 |
end; |
| 184 |
if mTotalChars <> mFreqChars then |
| 185 |
begin |
| 186 |
r := mFreqChars / ((mTotalChars - mFreqChars) * mTypicalDistributionRatio); |
| 187 |
if r < SURE_YES then |
| 188 |
begin |
| 189 |
Result := r; |
| 190 |
exit; |
| 191 |
end; |
| 192 |
end; |
| 193 |
(*normalize confidence, (we don't want to be 100% sure)*) |
| 194 |
Result := SURE_YES; |
| 195 |
end; |
| 196 |
|
| 197 |
procedure TCharDistributionAnalysis.Reset; |
| 198 |
begin |
| 199 |
mTotalChars := 0; |
| 200 |
mFreqChars := 0; |
| 201 |
end; |
| 202 |
|
| 203 |
function TCharDistributionAnalysis.GotEnoughData: Boolean; |
| 204 |
begin |
| 205 |
Result := (mTotalChars > ENOUGH_DATA_THRESHOLD); |
| 206 |
end; |
| 207 |
|
| 208 |
constructor TEUCTWDistributionAnalysis.Create; |
| 209 |
begin |
| 210 |
inherited Create; |
| 211 |
mCharToFreqOrder := @EUCTWCharToFreqOrder; |
| 212 |
mTableSize := EUCTW_TABLE_SIZE; |
| 213 |
mTypicalDistributionRatio := EUCTW_TYPICAL_DISTRIBUTION_RATIO; |
| 214 |
end; |
| 215 |
|
| 216 |
function TEUCTWDistributionAnalysis.GetOrder(str: pAnsiChar): int32; |
| 217 |
begin |
| 218 |
if byte(str^) >= $c4 then |
| 219 |
Result := 94 * (byte(str[0]) - $c4) + byte(str[1]) - byte($a1) |
| 220 |
else |
| 221 |
Result := -1; |
| 222 |
end; |
| 223 |
|
| 224 |
constructor TEUCKRDistributionAnalysis.Create; |
| 225 |
begin |
| 226 |
inherited Create; |
| 227 |
mCharToFreqOrder := @EUCKRCharToFreqOrder; |
| 228 |
mTableSize := EUCKR_TABLE_SIZE; |
| 229 |
mTypicalDistributionRatio := EUCKR_TYPICAL_DISTRIBUTION_RATIO; |
| 230 |
end; |
| 231 |
|
| 232 |
function TEUCKRDistributionAnalysis.GetOrder(str: pAnsiChar): int32; |
| 233 |
begin |
| 234 |
if byte(str^) >= $b0 then |
| 235 |
Result := 94 * (byte(str[0]) - $b0) + byte(str[1]) - $a1 |
| 236 |
else |
| 237 |
Result := -1; |
| 238 |
end; |
| 239 |
|
| 240 |
constructor TGB2312DistributionAnalysis.Create; |
| 241 |
begin |
| 242 |
inherited; |
| 243 |
mCharToFreqOrder := @GB2312CharToFreqOrder; |
| 244 |
mTableSize := GB2312_TABLE_SIZE; |
| 245 |
mTypicalDistributionRatio := GB2312_TYPICAL_DISTRIBUTION_RATIO; |
| 246 |
end; |
| 247 |
|
| 248 |
function TGB2312DistributionAnalysis.GetOrder(str: pAnsiChar): int32; |
| 249 |
begin |
| 250 |
if (byte(str[0]) >= $b0) and |
| 251 |
(byte(str[1]) >= $a1) then |
| 252 |
Result := 94 * (byte(str[0]) - $b0) + byte(str[1]) - $a1 |
| 253 |
else |
| 254 |
Result := -1; |
| 255 |
end; |
| 256 |
|
| 257 |
constructor TBig5DistributionAnalysis.Create; |
| 258 |
begin |
| 259 |
inherited; |
| 260 |
mCharToFreqOrder := @Big5CharToFreqOrder; |
| 261 |
mTableSize := BIG5_TABLE_SIZE; |
| 262 |
mTypicalDistributionRatio := BIG5_TYPICAL_DISTRIBUTION_RATIO; |
| 263 |
end; |
| 264 |
|
| 265 |
function TBig5DistributionAnalysis.GetOrder(str: pAnsiChar): int32; |
| 266 |
begin |
| 267 |
if byte(str[0]) >= $a4 then |
| 268 |
begin |
| 269 |
if byte(str[1]) >= $a1 then |
| 270 |
Result := 157 * (byte(str[0]) - $a4) + byte(str[1]) - $a1 + 63 |
| 271 |
else |
| 272 |
Result := 157 * (byte(str[0]) - $a4) + byte(str[1]) - $40; |
| 273 |
end |
| 274 |
else |
| 275 |
Result:= -1; |
| 276 |
end; |
| 277 |
|
| 278 |
constructor TSJISDistributionAnalysis.Create; |
| 279 |
begin |
| 280 |
inherited Create; |
| 281 |
mCharToFreqOrder := @JISCharToFreqOrder; |
| 282 |
mTableSize := JIS_TABLE_SIZE; |
| 283 |
mTypicalDistributionRatio := JIS_TYPICAL_DISTRIBUTION_RATIO; |
| 284 |
end; |
| 285 |
|
| 286 |
function TSJISDistributionAnalysis.GetOrder(str: pAnsiChar): int32; |
| 287 |
var |
| 288 |
order: int32; |
| 289 |
begin |
| 290 |
if (byte(str[0]) >= $81) and |
| 291 |
(byte(str[0]) <= $9f) then |
| 292 |
order := 188 * (byte(str[0]) - $81) |
| 293 |
else |
| 294 |
if (byte(str[0]) >= $e0) and |
| 295 |
(byte(str[0]) <= $ef) then |
| 296 |
order := 188 * (byte(str[0]) - $e0 + 31) |
| 297 |
else |
| 298 |
begin |
| 299 |
Result:= -1; |
| 300 |
exit; |
| 301 |
end; |
| 302 |
order := order + (byte((str+1)^) - $40); |
| 303 |
if byte(str[1]) > $7f then |
| 304 |
dec(order); |
| 305 |
Result := order; |
| 306 |
end; |
| 307 |
|
| 308 |
constructor TEUCJPDistributionAnalysis.Create; |
| 309 |
begin |
| 310 |
inherited Create; |
| 311 |
mCharToFreqOrder := @JISCharToFreqOrder; |
| 312 |
mTableSize := JIS_TABLE_SIZE; |
| 313 |
mTypicalDistributionRatio := JIS_TYPICAL_DISTRIBUTION_RATIO; |
| 314 |
end; |
| 315 |
|
| 316 |
function TEUCJPDistributionAnalysis.GetOrder(str: pAnsiChar): int32; |
| 317 |
begin |
| 318 |
if byte(str[0]) >= $a0 then |
| 319 |
Result := 94 * (byte(str[0]) - $a1) + byte(str[1]) - $a1 |
| 320 |
else |
| 321 |
Result:= -1; |
| 322 |
end; |
| 323 |
|
| 324 |
end. |