| 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: nsLatin1Prober.pas,v 1.4 2013/04/23 19:47:10 ya_nick Exp $ |
| 20 |
|
| 21 |
unit nsLatin1Prober; |
| 22 |
|
| 23 |
interface |
| 24 |
|
| 25 |
uses |
| 26 |
nsCore, |
| 27 |
CustomDetector; |
| 28 |
|
| 29 |
type |
| 30 |
TnsLatin1Prober = class(TCustomDetector) |
| 31 |
private |
| 32 |
mLastCharClass: AnsiChar; |
| 33 |
mFreqCounter: array of uInt32; |
| 34 |
public |
| 35 |
constructor Create; override; |
| 36 |
destructor Destroy; override; |
| 37 |
|
| 38 |
function HandleData(aBuf: pAnsiChar; aLen: integer): eProbingState; override; |
| 39 |
function GetDetectedCharset: eInternalCharsetID; override; |
| 40 |
procedure Reset; override; |
| 41 |
function GetConfidence: float; override; |
| 42 |
{$ifdef DEBUG_chardet} |
| 43 |
procedure DumpStatus; override; |
| 44 |
{$endif} |
| 45 |
end; |
| 46 |
|
| 47 |
implementation |
| 48 |
|
| 49 |
uses |
| 50 |
SysUtils; |
| 51 |
|
| 52 |
const |
| 53 |
FREQ_CAT_NUM = 4; |
| 54 |
|
| 55 |
UDF = 0; (* undefined*) |
| 56 |
OTH = 1; (*other*) |
| 57 |
ASC = 2; (* ascii capital letter*) |
| 58 |
ASS = 3; (* ascii small letter*) |
| 59 |
ACV = 4; (* accent capital vowel*) |
| 60 |
ACO = 5; (* accent capital other*) |
| 61 |
ASV = 6; (* accent small vowel*) |
| 62 |
ASO = 7; (* accent small other*) |
| 63 |
CLASS_NUM = 8; (* total classes*) |
| 64 |
|
| 65 |
Latin1_CharToClass: array [0..255] of byte = |
| 66 |
( |
| 67 |
OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, // 00 - 07 |
| 68 |
OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, // 08 - 0F |
| 69 |
OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, // 10 - 17 |
| 70 |
OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, // 18 - 1F |
| 71 |
OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, // 20 - 27 |
| 72 |
OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, // 28 - 2F |
| 73 |
OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, // 30 - 37 |
| 74 |
OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, // 38 - 3F |
| 75 |
OTH, ASC, ASC, ASC, ASC, ASC, ASC, ASC, // 40 - 47 |
| 76 |
ASC, ASC, ASC, ASC, ASC, ASC, ASC, ASC, // 48 - 4F |
| 77 |
ASC, ASC, ASC, ASC, ASC, ASC, ASC, ASC, // 50 - 57 |
| 78 |
ASC, ASC, ASC, OTH, OTH, OTH, OTH, OTH, // 58 - 5F |
| 79 |
OTH, ASS, ASS, ASS, ASS, ASS, ASS, ASS, // 60 - 67 |
| 80 |
ASS, ASS, ASS, ASS, ASS, ASS, ASS, ASS, // 68 - 6F |
| 81 |
ASS, ASS, ASS, ASS, ASS, ASS, ASS, ASS, // 70 - 77 |
| 82 |
ASS, ASS, ASS, OTH, OTH, OTH, OTH, OTH, // 78 - 7F |
| 83 |
OTH, UDF, OTH, ASO, OTH, OTH, OTH, OTH, // 80 - 87 |
| 84 |
OTH, OTH, ACO, OTH, ACO, UDF, ACO, UDF, // 88 - 8F |
| 85 |
UDF, OTH, OTH, OTH, OTH, OTH, OTH, OTH, // 90 - 97 |
| 86 |
OTH, OTH, ASO, OTH, ASO, UDF, ASO, ACO, // 98 - 9F |
| 87 |
OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, // A0 - A7 |
| 88 |
OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, // A8 - AF |
| 89 |
OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, // B0 - B7 |
| 90 |
OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, // B8 - BF |
| 91 |
ACV, ACV, ACV, ACV, ACV, ACV, ACO, ACO, // C0 - C7 |
| 92 |
ACV, ACV, ACV, ACV, ACV, ACV, ACV, ACV, // C8 - CF |
| 93 |
ACO, ACO, ACV, ACV, ACV, ACV, ACV, OTH, // D0 - D7 |
| 94 |
ACV, ACV, ACV, ACV, ACV, ACO, ACO, ACO, // D8 - DF |
| 95 |
ASV, ASV, ASV, ASV, ASV, ASV, ASO, ASO, // E0 - E7 |
| 96 |
ASV, ASV, ASV, ASV, ASV, ASV, ASV, ASV, // E8 - EF |
| 97 |
ASO, ASO, ASV, ASV, ASV, ASV, ASV, OTH, // F0 - F7 |
| 98 |
ASV, ASV, ASV, ASV, ASV, ASO, ASO, ASO // F8 - FF |
| 99 |
); |
| 100 |
|
| 101 |
|
| 102 |
(* |
| 103 |
0 : illegal |
| 104 |
1 : very unlikely |
| 105 |
2 : normal |
| 106 |
3 : very likely |
| 107 |
*) |
| 108 |
Latin1ClassModel: array [0..63] of byte = |
| 109 |
( |
| 110 |
(* UDF OTH ASC ASS ACV ACO ASV ASO *) |
| 111 |
(*UDF*) 0, 0, 0, 0, 0, 0, 0, 0, |
| 112 |
(*OTH*) 0, 3, 3, 3, 3, 3, 3, 3, |
| 113 |
(*ASC*) 0, 3, 3, 3, 3, 3, 3, 3, |
| 114 |
(*ASS*) 0, 3, 3, 3, 1, 1, 3, 3, |
| 115 |
(*ACV*) 0, 3, 3, 3, 1, 2, 1, 2, |
| 116 |
(*ACO*) 0, 3, 3, 3, 3, 3, 3, 3, |
| 117 |
(*ASV*) 0, 3, 1, 3, 1, 1, 1, 3, |
| 118 |
(*ASO*) 0, 3, 1, 3, 1, 1, 3, 3 |
| 119 |
); |
| 120 |
|
| 121 |
{ TnsLatin1Prober } |
| 122 |
|
| 123 |
constructor TnsLatin1Prober.Create; |
| 124 |
begin |
| 125 |
inherited Create; |
| 126 |
SetLength(mFreqCounter, FREQ_CAT_NUM); |
| 127 |
Reset; |
| 128 |
end; |
| 129 |
|
| 130 |
destructor TnsLatin1Prober.Destroy; |
| 131 |
begin |
| 132 |
SetLength(mFreqCounter, 0); |
| 133 |
|
| 134 |
inherited; |
| 135 |
end; |
| 136 |
|
| 137 |
{$ifdef DEBUG_chardet} |
| 138 |
procedure TnsLatin1Prober.DumpStatus; |
| 139 |
begin |
| 140 |
printf(' Latin1Prober: %1.3f [%s]r'#13#10'',GetConfidence,GetCharSetName); |
| 141 |
end; |
| 142 |
{$endif} |
| 143 |
|
| 144 |
function TnsLatin1Prober.GetDetectedCharset: eInternalCharsetID; |
| 145 |
begin |
| 146 |
Result := WINDOWS_1252_CHARSET; |
| 147 |
end; |
| 148 |
|
| 149 |
function TnsLatin1Prober.GetConfidence: float; |
| 150 |
var |
| 151 |
confidence: float; |
| 152 |
total: cardinal; |
| 153 |
i: integer; |
| 154 |
begin |
| 155 |
if mState = psNotMe then |
| 156 |
begin |
| 157 |
Result := SURE_NO; |
| 158 |
exit; |
| 159 |
end; |
| 160 |
|
| 161 |
total := 0; |
| 162 |
for i := 0 to Pred(FREQ_CAT_NUM) do |
| 163 |
total := total + mFreqCounter[i]; |
| 164 |
|
| 165 |
if total = 0 then |
| 166 |
confidence := 0.0 |
| 167 |
else |
| 168 |
begin |
| 169 |
confidence := mFreqCounter[3] * 1.0 / total; |
| 170 |
confidence := confidence - (mFreqCounter[1] * 20.0 /total); |
| 171 |
end; |
| 172 |
if confidence < 0.0 then |
| 173 |
confidence := 0.0; |
| 174 |
|
| 175 |
confidence := confidence * (0.50); |
| 176 |
(* lower the confidence of latin1 so that other more accurate detector *) |
| 177 |
(* can take priority.*) |
| 178 |
Result := confidence; |
| 179 |
end; |
| 180 |
|
| 181 |
function TnsLatin1Prober.HandleData(aBuf: pAnsiChar; aLen: integer): eProbingState; |
| 182 |
var |
| 183 |
newBuf1: pAnsiChar; |
| 184 |
newLen1: integer; |
| 185 |
charClass: AnsiChar; |
| 186 |
freq: byte; |
| 187 |
i: integer; |
| 188 |
begin |
| 189 |
Result := inherited HandleData(aBuf, aLen); |
| 190 |
if Result = psNotMe then |
| 191 |
exit; |
| 192 |
|
| 193 |
newBuf1 := nil; |
| 194 |
newLen1 := 0; |
| 195 |
newBuf1 := AllocMem(aLen); |
| 196 |
try |
| 197 |
if not FilterWithEnglishLetters(aBuf,aLen,newBuf1,newLen1) then |
| 198 |
begin |
| 199 |
newBuf1 := aBuf; |
| 200 |
newLen1 := aLen; |
| 201 |
end; |
| 202 |
for i := 0 to Pred(newLen1) do |
| 203 |
begin |
| 204 |
charClass := AnsiChar(Latin1_CharToClass[integer(newBuf1[i])]); |
| 205 |
freq := Latin1ClassModel[byte(mLastCharClass) * CLASS_NUM + byte(charClass)]; |
| 206 |
if freq = 0 then |
| 207 |
begin |
| 208 |
mState:= psNotMe; |
| 209 |
break; |
| 210 |
end; |
| 211 |
inc(mFreqCounter[freq]); |
| 212 |
mLastCharClass := charClass; |
| 213 |
end; |
| 214 |
finally |
| 215 |
FreeMem(newBuf1, aLen); |
| 216 |
end; |
| 217 |
Result := mState; |
| 218 |
end; |
| 219 |
|
| 220 |
procedure TnsLatin1Prober.Reset; |
| 221 |
var |
| 222 |
i: integer; |
| 223 |
begin |
| 224 |
mState := psDetecting; |
| 225 |
mLastCharClass := AnsiChar(OTH); |
| 226 |
for i := 0 to Pred(FREQ_CAT_NUM) do |
| 227 |
mFreqCounter[i] := 0; |
| 228 |
end; |
| 229 |
|
| 230 |
end. |