| 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: nsGroupProber.pas,v 1.3 2013/04/23 19:47:10 ya_nick Exp $ |
| 20 |
|
| 21 |
unit nsGroupProber; |
| 22 |
|
| 23 |
interface |
| 24 |
uses |
| 25 |
{$I dbg.inc} |
| 26 |
nsCore, |
| 27 |
CustomDetector; |
| 28 |
|
| 29 |
|
| 30 |
type |
| 31 |
TnsGroupProber = class(TCustomDetector) |
| 32 |
protected |
| 33 |
mNumOfProbers: integer; |
| 34 |
mProbers: array of TCustomDetector; |
| 35 |
mIsActive: array of Boolean; |
| 36 |
mBestGuess: integer; |
| 37 |
mActiveNum: integer; |
| 38 |
mProberStates: array of eProbingState; |
| 39 |
|
| 40 |
public |
| 41 |
constructor Create; override; |
| 42 |
destructor Destroy; override; |
| 43 |
procedure Reset; override; |
| 44 |
function HandleData(aBuf: pAnsiChar; aLen: integer): eProbingState; override; |
| 45 |
function GetDetectedCharset: eInternalCharsetID; override; |
| 46 |
function GetConfidence: float; override; |
| 47 |
function EnableCharset(Charset: eInternalCharsetID; NewValue: Boolean): Boolean; |
| 48 |
{$ifdef DEBUG_chardet} |
| 49 |
procedure DumpStatus(Dump: string); override; |
| 50 |
{$endif} |
| 51 |
end; |
| 52 |
|
| 53 |
implementation |
| 54 |
uses |
| 55 |
SysUtils |
| 56 |
{$ifdef DEBUG_chardet} |
| 57 |
,TypInfo |
| 58 |
{$endif} |
| 59 |
; |
| 60 |
|
| 61 |
{ TnsGroupProber } |
| 62 |
|
| 63 |
constructor TnsGroupProber.Create; |
| 64 |
begin |
| 65 |
inherited Create; |
| 66 |
|
| 67 |
Self.Reset; |
| 68 |
end; |
| 69 |
|
| 70 |
destructor TnsGroupProber.Destroy; |
| 71 |
var |
| 72 |
i: integer; |
| 73 |
begin |
| 74 |
for i := 0 to Pred(mNumOfProbers) do |
| 75 |
if mProbers[i] <> nil then |
| 76 |
mProbers[i].Free; |
| 77 |
|
| 78 |
SetLength(mProbers, 0); |
| 79 |
SetLength(mProberStates, 0); |
| 80 |
SetLength(mIsActive, 0); |
| 81 |
inherited; |
| 82 |
end; |
| 83 |
|
| 84 |
function TnsGroupProber.GetDetectedCharset: eInternalCharsetID; |
| 85 |
begin |
| 86 |
(*if we have no answer yet*) |
| 87 |
if mBestGuess = -1 then |
| 88 |
begin |
| 89 |
GetConfidence; |
| 90 |
(*no charset seems positive*) |
| 91 |
if mBestGuess = -1 then |
| 92 |
mBestGuess:= 0; |
| 93 |
(*we will use default.*) |
| 94 |
end; |
| 95 |
|
| 96 |
Result := mProbers[mBestGuess].GetDetectedCharset; |
| 97 |
end; |
| 98 |
|
| 99 |
function TnsGroupProber.HandleData(aBuf: pAnsiChar; aLen: integer): eProbingState; |
| 100 |
var |
| 101 |
i: integer; |
| 102 |
begin |
| 103 |
{$IFDEF DEBUG_chardet} |
| 104 |
AddDump('Group Prober - HandleData - start'); |
| 105 |
{$endif} |
| 106 |
for i:=0 to Pred(mNumOfProbers) do |
| 107 |
begin |
| 108 |
if (mProberStates[i] = psNotMe) or |
| 109 |
(mProberStates[i] = psFoundIt) then |
| 110 |
continue; |
| 111 |
|
| 112 |
mProberStates[i] := mProbers[i].HandleData(aBuf, aLen); |
| 113 |
|
| 114 |
if mProberStates[i] = psFoundIt then |
| 115 |
begin |
| 116 |
mBestGuess := i; |
| 117 |
mState := psFoundIt; |
| 118 |
break; |
| 119 |
end |
| 120 |
else |
| 121 |
if mProberStates[i] = psNotMe then |
| 122 |
begin |
| 123 |
mIsActive[i] := FALSE; |
| 124 |
dec(mActiveNum); |
| 125 |
if mActiveNum <= 0 then |
| 126 |
begin |
| 127 |
mState := psNotMe; |
| 128 |
break; |
| 129 |
end; |
| 130 |
end; |
| 131 |
end; |
| 132 |
Result := mState; |
| 133 |
{$IFDEF DEBUG_chardet} |
| 134 |
DumpStatus('Group Prober - HandleData - exit'); |
| 135 |
{$endif} |
| 136 |
end; |
| 137 |
|
| 138 |
procedure TnsGroupProber.Reset; |
| 139 |
var |
| 140 |
i: integer; |
| 141 |
begin |
| 142 |
mActiveNum := 0; |
| 143 |
for i := 0 to Pred(mNumOfProbers) do |
| 144 |
begin |
| 145 |
if mProbers[i] <> nil then |
| 146 |
begin |
| 147 |
mProbers[i].Reset; |
| 148 |
mIsActive[i] := mProbers[i].Enabled; |
| 149 |
if mProbers[i].Enabled then |
| 150 |
begin |
| 151 |
mProberStates[i] := psDetecting; |
| 152 |
inc(mActiveNum); |
| 153 |
end |
| 154 |
else |
| 155 |
mProberStates[i] := psNotMe; |
| 156 |
end |
| 157 |
else |
| 158 |
mIsActive[i] := FALSE; |
| 159 |
end; |
| 160 |
mBestGuess := -1; |
| 161 |
mEnabled := (mActiveNum > 0); |
| 162 |
if mEnabled then |
| 163 |
mState := psDetecting |
| 164 |
else |
| 165 |
mState := psNotMe; |
| 166 |
end; |
| 167 |
|
| 168 |
function TnsGroupProber.GetConfidence: float; |
| 169 |
var |
| 170 |
i: integer; |
| 171 |
bestConf: float; |
| 172 |
cf: float; |
| 173 |
begin |
| 174 |
bestConf := 0.0; |
| 175 |
case mState of |
| 176 |
psFoundIt: |
| 177 |
begin |
| 178 |
Result := SURE_YES; (*sure yes*) |
| 179 |
exit; |
| 180 |
end; |
| 181 |
psNotMe: |
| 182 |
begin |
| 183 |
Result := SURE_NO; (*sure no*) |
| 184 |
exit; |
| 185 |
end; |
| 186 |
else |
| 187 |
for i := 0 to Pred(mNumOfProbers) do |
| 188 |
begin |
| 189 |
if not mIsActive[i] then |
| 190 |
continue; |
| 191 |
|
| 192 |
cf := mProbers[i].GetConfidence; |
| 193 |
if bestConf < cf then |
| 194 |
begin |
| 195 |
bestConf := cf; |
| 196 |
mBestGuess := i; |
| 197 |
end; |
| 198 |
end; |
| 199 |
end;{case} |
| 200 |
{$IFDEF DEBUG_chardet} |
| 201 |
DumpStatus('Group Prober - GetConfidience'); |
| 202 |
{$endif} |
| 203 |
|
| 204 |
Result := bestConf; |
| 205 |
end; |
| 206 |
|
| 207 |
function TnsGroupProber.EnableCharset(Charset: eInternalCharsetID; NewValue: Boolean): Boolean; |
| 208 |
var |
| 209 |
i: integer; |
| 210 |
begin |
| 211 |
for i := 0 to Pred(mNumOfProbers) do |
| 212 |
if mProbers[i].GetDetectedCharset = Charset then |
| 213 |
begin |
| 214 |
Result := true; |
| 215 |
mProbers[i].Enabled := NewValue; |
| 216 |
exit; |
| 217 |
end; |
| 218 |
Result := false; |
| 219 |
end; |
| 220 |
|
| 221 |
{$ifdef DEBUG_chardet} |
| 222 |
procedure TnsGroupProber.DumpStatus(Dump: string); |
| 223 |
var |
| 224 |
i: integer; |
| 225 |
begin |
| 226 |
inherited DumpStatus(Dump); |
| 227 |
inherited DumpStatus(Format('%30s - %5s - %5s', |
| 228 |
['Prober', |
| 229 |
'Active', |
| 230 |
'Conf'])); |
| 231 |
for i := 0 to Pred(mNumOfProbers) do |
| 232 |
inherited DumpStatus(Format('%30s - %5s - %1.5f', |
| 233 |
[GetEnumName(TypeInfo(eInternalCharsetID), integer(mProbers[i].GetDetectedCharset)), |
| 234 |
BoolToStr(mIsActive[i], True), |
| 235 |
mProbers[i].GetConfidence])); |
| 236 |
|
| 237 |
end; |
| 238 |
{$endif} |
| 239 |
|
| 240 |
end. |
| 241 |
|
| 242 |
|
| 243 |
|