Debian Codegraph @

components/chsdet/src/nsMBCSMultiProber.pas

raw
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: nsMBCSMultiProber.pas,v 1.3 2013/04/23 19:47:10 ya_nick Exp $
20
21 unit nsMBCSMultiProber;
22
23 interface
24
25 uses
26 {$I dbg.inc}
27 nsCore,
28 MultiModelProber,
29 JpCntx,
30 CharDistribution;
31
32 type
33 TnsMBCSMultiProber = class (TMultiModelProber)
34 private
35 mDistributionAnalysis: array of TCharDistributionAnalysis;
36 mContextAnalysis: array of TJapaneseContextAnalysis;
37 mBestGuess: integer;
38
39 function RunStatAnalyse(aBuf: pAnsiChar; aLen: integer): eProbingState;
40 function GetConfidenceFor(index: integer): double; reintroduce;
41 public
42 constructor Create; reintroduce;
43 destructor Destroy; override;
44 function HandleData(aBuf: pAnsiChar; aLen: integer): eProbingState; override;
45 function GetConfidence: double; override;
46 procedure Reset; override;
47 {$ifdef DEBUG_chardet}
48 procedure DumpStatus(Dump: string); override;
49 {$endif}
50 end;
51
52 implementation
53 uses
54 SysUtils,
55 nsCodingStateMachine
56 {$ifdef DEBUG_chardet}
57 ,TypInfo
58 {$endif}
59 ;
60
61 {$I '.\mbclass\SJISLangModel.inc'}
62 {$I '.\mbclass\EUCJPLangModel.inc'}
63 {$I '.\mbclass\GB18030LangModel.inc'}
64 {$I '.\mbclass\EUCKRLangModel.inc'}
65 {$I '.\mbclass\Big5LangModel.inc'}
66 {$I '.\mbclass\EUCTWLangModel.inc'}
67
68
69
70 { TnsMBCSMultiProber }
71 const
72 NUM_OF_PROBERS = 6;
73
74
75 constructor TnsMBCSMultiProber.Create;
76 begin
77 inherited Create;
78 SetLength(mDistributionAnalysis, NUM_OF_PROBERS);
79 SetLength(mContextAnalysis, NUM_OF_PROBERS);
80
81 AddCharsetModel(SJISLangModel);
82 mDistributionAnalysis[0] := TSJISDistributionAnalysis.Create;
83 mContextAnalysis[0] := TSJISContextAnalysis.Create;
84
85 AddCharsetModel(EUCJPLangModel);
86 mDistributionAnalysis[1] := TEUCKRDistributionAnalysis.Create;
87 mContextAnalysis[1] := nil;
88
89 AddCharsetModel(GB18030LangModel);
90 mDistributionAnalysis[2] := TGB2312DistributionAnalysis.Create;
91 mContextAnalysis[2] := nil;
92
93 AddCharsetModel(EUCKRLangModel);
94 mDistributionAnalysis[3] := TEUCKRDistributionAnalysis.Create;
95 mContextAnalysis[3] := nil;
96
97 AddCharsetModel(Big5LangModel);
98 mDistributionAnalysis[4] := TBig5DistributionAnalysis.Create;
99 mContextAnalysis[4] := nil;
100
101 AddCharsetModel(EUCTWLangModel);
102 mDistributionAnalysis[5] := TEUCTWDistributionAnalysis.Create;
103 mContextAnalysis[5] := nil;
104
105 end;
106
107 destructor TnsMBCSMultiProber.Destroy;
108 var
109 i: integer;
110 begin
111 inherited;
112 for i := 0 to Pred(mCharsetsCount) do
113 begin
114 if mDistributionAnalysis[i] <> nil then
115 mDistributionAnalysis[i].Free;
116 if mContextAnalysis[i] <> nil then
117 mContextAnalysis[i].Free;
118 end;
119
120 SetLength(mDistributionAnalysis, 0);
121 SetLength(mContextAnalysis, 0);
122
123 end;
124
125 {$ifdef DEBUG_chardet}
126 procedure TnsMBCSMultiProber.DumpStatus(Dump: string);
127 var
128 i: integer;
129 begin
130 AddDump(Dump + ' Current state ' + GetEnumName(TypeInfo(eProbingState), integer(mState)));
131 AddDump(Format('%30s - %10s - %5s',
132 ['Prober',
133 'State',
134 'Conf']));
135 for i := 0 to Pred(mCharsetsCount) do
136 AddDump(Format('%30s - %10s - %1.5f',
137 [GetEnumName(TypeInfo(eInternalCharsetID), integer(mCodingSM[i].GetCharsetID)),
138 GetEnumName(TypeInfo(eProbingState), integer(mSMState[i])),
139 GetConfidenceFor(i)
140 ]));
141 end;
142 {$endif}
143
144 function TnsMBCSMultiProber.HandleData(aBuf: pAnsiChar; aLen: integer): eProbingState;
145 var
146 i: integer; (*do filtering to reduce load to probers*)
147 highbyteBuf: pAnsiChar;
148 hptr: pAnsiChar;
149 keepNext: Boolean;
150 begin
151 keepNext := TRUE;
152 (*assume previous is not ascii, it will do no harm except add some noise*)
153 highbyteBuf := AllocMem(aLen);
154 try
155 hptr:= highbyteBuf;
156 if hptr = nil then
157 begin
158 Result := mState;
159 exit;
160 end;
161 for i:=0 to Pred(aLen) do
162 begin
163 if (Byte(aBuf[i]) > $80) then
164 begin
165 hptr^ := aBuf[i];
166 inc(hptr);
167 keepNext:= TRUE;
168 end
169 else
170 begin
171 (*if previous is highbyte, keep this even it is a ASCII*)
172 if keepNext = TRUE then
173 begin
174 hptr^ := aBuf[i];
175 inc(hptr);
176 keepNext:= FALSE;
177 end;
178 end;
179 end;
180 {$IFDEF DEBUG_chardet}
181 AddDump('MultiByte - HandleData - start');
182 {$endif}
183
184 inherited HandleData(highbyteBuf, hptr - highbyteBuf);
185 {$IFDEF DEBUG_chardet}
186 AddDump('MultiByte - HandleData - end');
187 {$endif}
188 finally
189 FreeMem(highbyteBuf, aLen);
190 end;
191
192 // if we have more when one candidat then
193 // try statisic analyse
194 if (mState <> psFoundIt) or
195 (mState <> psNotMe) then
196 RunStatAnalyse(aBuf, aLen);
197 Result := mState;
198 end;
199
200 function TnsMBCSMultiProber.RunStatAnalyse(aBuf: pAnsiChar; aLen: integer): eProbingState;
201 var
202 i, c: integer;
203 codingState: nsSMState;
204 charLen: byte;
205 mLastChar: array [0..1] of AnsiChar;
206 begin
207 {$IFDEF DEBUG_chardet}
208 AddDump('MultiByte - Stat Analyse - start');
209 {$endif}
210
211 for i := 0 to Pred(mCharsetsCount) do
212 begin
213 if (mSMState[i] = psFoundIt) or
214 (mSMState[i] = psNotMe) then
215 continue;
216 if mDistributionAnalysis[i] = nil then
217 continue;
218 for c := 0 to Pred(aLen) do
219 begin
220 codingState := mCodingSM[i].NextState(aBuf[c]);
221 if codingState = eStart then
222 begin
223 charLen := mCodingSM[i].GetCurrentCharLen;
224 if c = 0 then
225 begin
226 mLastChar[1] := aBuf[0];
227 if mContextAnalysis[i] <> nil then
228 mContextAnalysis[i].HandleOneChar(mLastChar,charLen);
229 mDistributionAnalysis[i].HandleOneChar(mLastChar,charLen);
230 end
231 else
232 begin
233 if mContextAnalysis[i] <> nil then
234 mContextAnalysis[i].HandleOneChar(aBuf+c-1,charLen);
235 mDistributionAnalysis[i].HandleOneChar(aBuf+c-1,charLen);
236 end;
237 end;
238 if (mContextAnalysis[i] <> nil) then
239 if mContextAnalysis[i].GotEnoughData and
240 (GetConfidenceFor(i) > SHORTCUT_THRESHOLD) then
241 begin
242 mState := psFoundIt;
243 mDetectedCharset := mCodingSM[i].GetCharsetID;
244 break;
245 end;
246 end;
247 end;
248
249 {$IFDEF DEBUG_chardet}
250 AddDump('MultiByte - Stat Analyse - EXIT');
251 {$endif}
252 Result := mState;
253 end;
254
255 function TnsMBCSMultiProber.GetConfidenceFor(index: integer): double;
256 var
257 contxtCf: double;
258 distribCf: double;
259 begin
260 if mContextAnalysis[index] <> nil then
261 contxtCf := mContextAnalysis[index].GetConfidence
262 else
263 contxtCf := -1;
264
265 distribCf := mDistributionAnalysis[index].GetConfidence;
266
267 if contxtCf > distribCf then
268 Result := contxtCf
269 else
270 Result := distribCf;
271 end;
272
273 function TnsMBCSMultiProber.GetConfidence: double;
274 var
275 i: integer;
276 conf,
277 bestConf: double;
278 begin
279 mBestGuess := -1;
280 bestConf := SURE_NO;
281 for i := 0 to Pred(mCharsetsCount) do
282 begin
283 if (mSMState[i] = psFoundIt) or
284 (mSMState[i] = psNotMe) then
285 continue;
286 if mDistributionAnalysis[i] = nil then
287 continue;
288 conf := GetConfidenceFor(i);
289 if conf > bestConf then
290 begin
291 mBestGuess := i;
292 bestConf := conf;
293 end;
294 end;
295 Result := bestConf;
296 if mBestGuess > -1 then
297 mDetectedCharset := mCodingSM[mBestGuess].GetCharsetID
298 else
299 mDetectedCharset := UNKNOWN_CHARSET;
300 end;
301
302 procedure TnsMBCSMultiProber.Reset;
303 var
304 i: integer;
305 begin
306 inherited Reset;
307 for i := 0 to Pred(mCharsetsCount) do
308 begin
309 if mDistributionAnalysis[i] <> nil then
310 mDistributionAnalysis[i].Reset;
311 if mContextAnalysis[i] <> nil then
312 mContextAnalysis[i].Reset;
313 end;
314 end;
315
316 end.