| 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: nsSBCharSetProber.pas,v 1.4 2013/04/23 19:47:10 ya_nick Exp $ |
| 20 |
|
| 21 |
unit nsSBCharSetProber; |
| 22 |
|
| 23 |
interface |
| 24 |
|
| 25 |
uses |
| 26 |
{$I dbg.inc} |
| 27 |
nsCore, |
| 28 |
CustomDetector; |
| 29 |
|
| 30 |
|
| 31 |
const |
| 32 |
NUMBER_OF_SEQ_CAT = 4; |
| 33 |
|
| 34 |
type |
| 35 |
SequenceModel = record |
| 36 |
charToOrderMap: pAnsiChar; (* [256] table use to find a char's order*) |
| 37 |
precedenceMatrix: pAnsiChar; (* [SAMPLE_SIZE][SAMPLE_SIZE]; table to find a 2-char sequence's frequency*) |
| 38 |
mTypicalPositiveRatio: float; (* = freqSeqs / totalSeqs *) |
| 39 |
keepEnglishLetter: Boolean; (* says if this script contains English characters (not implemented)*) |
| 40 |
CharsetID: eInternalCharsetID; |
| 41 |
end; |
| 42 |
|
| 43 |
TnsSingleByteCharSetProber = class(TCustomDetector) |
| 44 |
protected |
| 45 |
mModel: SequenceModel; |
| 46 |
mReversed: Boolean; (* TRUE if we need to reverse every pair in the model lookup*) |
| 47 |
mLastOrder: byte; (*char order of last character*) |
| 48 |
mTotalSeqs: uInt32; |
| 49 |
mSeqCounters: array [0..Pred(NUMBER_OF_SEQ_CAT)] of uInt32; |
| 50 |
mTotalChar: uInt32; (*characters that fall in our sampling range*) |
| 51 |
mFreqChar: uInt32; (* Optional auxiliary prober for name decision. created and destroyed by the GroupProber*) |
| 52 |
mNameProber: TCustomDetector; |
| 53 |
|
| 54 |
public |
| 55 |
constructor Create(model: SequenceModel; reversed: Boolean = FALSE; nameProber: TCustomDetector = nil); reintroduce; |
| 56 |
destructor Destroy; override; |
| 57 |
function GetDetectedCharset: eInternalCharsetID; override; |
| 58 |
function HandleData(aBuf: pAnsiChar; aLen: integer): eProbingState; override; |
| 59 |
procedure Reset; override; |
| 60 |
|
| 61 |
function GetConfidence: float; override; |
| 62 |
(* This feature is not implemented yet. any current language model*) |
| 63 |
(* contain this parameter as PR_FALSE. No one is looking at this*) |
| 64 |
(* parameter or calling this method.*) |
| 65 |
(* Moreover, the nsSBCSGroupProber which calls the HandleData of this*) |
| 66 |
(* prober has a hard-coded call to FilterWithoutEnglishLetters which gets rid*) |
| 67 |
(* of the English letters.*) |
| 68 |
|
| 69 |
function KeepEnglishLetters: Boolean; virtual; |
| 70 |
(* (not implemented)*) |
| 71 |
end; |
| 72 |
|
| 73 |
implementation |
| 74 |
|
| 75 |
{$ifdef DEBUG_chardet} |
| 76 |
uses |
| 77 |
TypInfo, |
| 78 |
SysUtils; |
| 79 |
{$endif} |
| 80 |
|
| 81 |
const |
| 82 |
SAMPLE_SIZE: byte = 64; |
| 83 |
SB_ENOUGH_REL_THRESHOLD = 1024; |
| 84 |
POSITIVE_SHORTCUT_THRESHOLD = SHORTCUT_THRESHOLD; |
| 85 |
NEGATIVE_SHORTCUT_THRESHOLD = 1 - POSITIVE_SHORTCUT_THRESHOLD; |
| 86 |
SYMBOL_CAT_ORDER: byte = 250; |
| 87 |
POSITIVE_CAT = (NUMBER_OF_SEQ_CAT-1); |
| 88 |
(*#define NEGATIVE_APPROACH 1*) |
| 89 |
{$ifdef NEGATIVE_APPROACH} |
| 90 |
NEGATIVE_CAT = 0; |
| 91 |
{$endif} |
| 92 |
|
| 93 |
{ TnsSingleByteCharSetProber } |
| 94 |
|
| 95 |
constructor TnsSingleByteCharSetProber.Create(model: SequenceModel; reversed: Boolean = FALSE; nameProber: TCustomDetector = nil); |
| 96 |
begin |
| 97 |
inherited Create; |
| 98 |
mModel := model; |
| 99 |
mReversed := reversed; |
| 100 |
mNameProber := nameProber; |
| 101 |
Reset; |
| 102 |
end; |
| 103 |
|
| 104 |
destructor TnsSingleByteCharSetProber.Destroy; |
| 105 |
begin |
| 106 |
|
| 107 |
inherited; |
| 108 |
end; |
| 109 |
|
| 110 |
function TnsSingleByteCharSetProber.GetDetectedCharset: eInternalCharsetID; |
| 111 |
begin |
| 112 |
if mNameProber = nil then |
| 113 |
begin |
| 114 |
Result := mModel.CharsetID; |
| 115 |
exit; |
| 116 |
end; |
| 117 |
Result := mNameProber.GetDetectedCharset; |
| 118 |
end; |
| 119 |
|
| 120 |
(*#define NEGATIVE_APPROACH 1*) |
| 121 |
function TnsSingleByteCharSetProber.GetConfidence: float; |
| 122 |
var |
| 123 |
r: float; |
| 124 |
begin |
| 125 |
{$ifdef NEGATIVE_APPROACH} |
| 126 |
if mTotalSeqs > 0 then |
| 127 |
if mTotalSeqs > mSeqCounters[NEGATIVE_CAT] * 10 then |
| 128 |
begin |
| 129 |
Result := (mTotalSeqs-mSeqCounters[NEGATIVE_CAT] * 10) / mTotalSeqs * mFreqChar / mTotalChar; |
| 130 |
exit; |
| 131 |
end; |
| 132 |
Result := SURE_NO; |
| 133 |
{$else} |
| 134 |
(*POSITIVE_APPROACH*) |
| 135 |
if mTotalSeqs > 0 then |
| 136 |
begin |
| 137 |
r := (1.0) * mSeqCounters[POSITIVE_CAT] / mTotalSeqs / mModel.mTypicalPositiveRatio; |
| 138 |
r := r * mFreqChar / mTotalChar; |
| 139 |
if r >= 1.0 then |
| 140 |
r := SURE_YES; |
| 141 |
|
| 142 |
Result := r; |
| 143 |
exit; |
| 144 |
end; |
| 145 |
|
| 146 |
Result := SURE_NO; |
| 147 |
{$endif} |
| 148 |
end; |
| 149 |
|
| 150 |
function TnsSingleByteCharSetProber.HandleData(aBuf: pAnsiChar; aLen: integer): eProbingState; |
| 151 |
var |
| 152 |
order: byte; |
| 153 |
i: integer; |
| 154 |
cf: float; |
| 155 |
begin |
| 156 |
Result := inherited HandleData(aBuf, aLen); |
| 157 |
if Result = psNotMe then |
| 158 |
exit; |
| 159 |
// TODO - move here call to FilterWithoutEnglishLetters from nsSBCSGroupProber.pas |
| 160 |
// if not mModel.keepEnglishLetter then ... |
| 161 |
|
| 162 |
for i := 0 to Pred(aLen) do |
| 163 |
begin |
| 164 |
order := byte(mModel.charToOrderMap[byte(aBuf[i])]); |
| 165 |
if order < SYMBOL_CAT_ORDER then |
| 166 |
inc(mTotalChar); |
| 167 |
if order < SAMPLE_SIZE then |
| 168 |
begin |
| 169 |
inc(mFreqChar); |
| 170 |
if mLastOrder < SAMPLE_SIZE then |
| 171 |
begin |
| 172 |
inc(mTotalSeqs); |
| 173 |
{$ifdef DEBUG_chardet} |
| 174 |
//if ((mLastOrder * SAMPLE_SIZE + order) >= 4096) then |
| 175 |
AddDump(Format('oredr %4d for byte %4d last order %4d'+#10#13+ |
| 176 |
'array index %4d array high %8d'+#10#13+ |
| 177 |
'LangModel %s', |
| 178 |
[order,byte(aBuf[i]),mLastOrder, |
| 179 |
(mLastOrder * SAMPLE_SIZE + order),4096, |
| 180 |
getEnumName(TypeInfo(eInternalCharsetID), integer(mModel.CharsetID))])); |
| 181 |
{$endif} |
| 182 |
if not mReversed then |
| 183 |
inc(mSeqCounters[cardinal(mModel.precedenceMatrix[mLastOrder * SAMPLE_SIZE + order])]) |
| 184 |
else |
| 185 |
inc(mSeqCounters[cardinal(mModel.precedenceMatrix[order * SAMPLE_SIZE + mLastOrder])]); |
| 186 |
(* reverse the order of the letters in the lookup*) |
| 187 |
end; |
| 188 |
end; |
| 189 |
mLastOrder:= order; |
| 190 |
end; |
| 191 |
if mState = psDetecting then |
| 192 |
if mTotalSeqs > SB_ENOUGH_REL_THRESHOLD then |
| 193 |
begin |
| 194 |
cf := GetConfidence; |
| 195 |
if cf > POSITIVE_SHORTCUT_THRESHOLD then |
| 196 |
mState:= psFoundIt |
| 197 |
else |
| 198 |
if cf < NEGATIVE_SHORTCUT_THRESHOLD then |
| 199 |
mState:= psNotMe; |
| 200 |
end; |
| 201 |
|
| 202 |
Result := mState; |
| 203 |
end; |
| 204 |
|
| 205 |
function TnsSingleByteCharSetProber.KeepEnglishLetters: Boolean; |
| 206 |
begin |
| 207 |
Result := mModel.keepEnglishLetter; |
| 208 |
end; |
| 209 |
|
| 210 |
procedure TnsSingleByteCharSetProber.Reset; |
| 211 |
var |
| 212 |
i: integer; |
| 213 |
begin |
| 214 |
if mEnabled then |
| 215 |
mState := psDetecting |
| 216 |
else |
| 217 |
mState := psNotMe; |
| 218 |
mLastOrder := 255; |
| 219 |
for i := 0 to Pred(NUMBER_OF_SEQ_CAT) do |
| 220 |
mSeqCounters[i] := 0; |
| 221 |
mTotalSeqs := 0; |
| 222 |
mTotalChar := 0; |
| 223 |
mFreqChar := 0; |
| 224 |
end; |
| 225 |
|
| 226 |
end. |