-
Notifications
You must be signed in to change notification settings - Fork 6
/
PSMDetector.cs
357 lines (323 loc) · 9.6 KB
/
PSMDetector.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
* Created by SharpDevelop.
* User: Administrator
* Date: 2006-12-16
* Time: 2:55
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
namespace NChardet
{
/// <summary>
/// Description of PSMDetector.
/// </summary>
public abstract class PSMDetector
{
public static readonly int ALL = 0 ;
public static readonly int JAPANESE = 1 ;
public static readonly int CHINESE = 2 ;
public static readonly int SIMPLIFIED_CHINESE = 3 ;
public static readonly int TRADITIONAL_CHINESE = 4 ;
public static readonly int KOREAN = 5 ;
public static readonly int NO_OF_LANGUAGES = 6 ;
public static readonly int MAX_VERIFIERS = 16 ;
private Verifier[] mVerifier ;
private EUCStatistics[] mStatisticsData ;
private EUCSampler mSampler = new EUCSampler() ;
private byte[] mState = new byte[MAX_VERIFIERS] ;
private int[] mItemIdx = new int[MAX_VERIFIERS] ;
private int mItems ;
private int mClassItems ;
public bool mDone ;
public bool mRunSampler ;
public bool mClassRunSampler ;
public PSMDetector()
{
initVerifiers( PSMDetector.ALL );
Reset() ;
}
public PSMDetector(int langFlag) {
initVerifiers(langFlag);
Reset() ;
}
public PSMDetector(int aItems, Verifier[] aVerifierSet,EUCStatistics[] aStatisticsSet) {
mClassRunSampler = ( aStatisticsSet != null ) ;
mStatisticsData = aStatisticsSet ;
mVerifier = aVerifierSet ;
mClassItems = aItems ;
Reset() ;
}
public void Reset() {
mRunSampler = mClassRunSampler ;
mDone = false ;
mItems = mClassItems ;
for(int i=0; i<mItems; i++) {
mState[i] = 0;
mItemIdx[i] = i;
}
mSampler.Reset() ;
}
protected void initVerifiers(int currVerSet) {
//int idx = 0 ;
int currVerifierSet ;
if (currVerSet >=0 && currVerSet < NO_OF_LANGUAGES ) {
currVerifierSet = currVerSet ;
}
else {
currVerifierSet = PSMDetector.ALL ;
}
mVerifier = null ;
mStatisticsData = null ;
if ( currVerifierSet == PSMDetector.TRADITIONAL_CHINESE ) {
mVerifier = new Verifier[] {
new UTF8Verifier(),
new BIG5Verifier(),
new ISO2022CNVerifier(),
new EUCTWVerifier(),
new CP1252Verifier(),
new UCS2BEVerifier(),
new UCS2LEVerifier()
};
mStatisticsData = new EUCStatistics[] {
null,
new Big5Statistics(),
null,
new EUCTWStatistics(),
null,
null,
null
};
}
//==========================================================
else if ( currVerifierSet == PSMDetector.KOREAN ) {
mVerifier = new Verifier[] {
new UTF8Verifier(),
new EUCKRVerifier(),
new ISO2022KRVerifier(),
new CP1252Verifier(),
new UCS2BEVerifier(),
new UCS2LEVerifier()
};
}
//==========================================================
else if ( currVerifierSet == PSMDetector.SIMPLIFIED_CHINESE ) {
mVerifier = new Verifier[] {
new UTF8Verifier(),
new GB2312Verifier(),
new GB18030Verifier(),
new ISO2022CNVerifier(),
new HZVerifier(),
new CP1252Verifier(),
new UCS2BEVerifier(),
new UCS2LEVerifier()
};
}
//==========================================================
else if ( currVerifierSet == PSMDetector.JAPANESE ) {
mVerifier = new Verifier[] {
new UTF8Verifier(),
new SJISVerifier(),
new EUCJPVerifier(),
new ISO2022JPVerifier(),
new CP1252Verifier(),
new UCS2BEVerifier(),
new UCS2LEVerifier()
};
}
//==========================================================
else if ( currVerifierSet == PSMDetector.CHINESE ) {
mVerifier = new Verifier[] {
new UTF8Verifier(),
new GB2312Verifier(),
new GB18030Verifier(),
new BIG5Verifier(),
new ISO2022CNVerifier(),
new HZVerifier(),
new EUCTWVerifier(),
new CP1252Verifier(),
new UCS2BEVerifier(),
new UCS2LEVerifier()
};
mStatisticsData = new EUCStatistics[] {
null,
new GB2312Statistics(),
null,
new Big5Statistics(),
null,
null,
new EUCTWStatistics(),
null,
null,
null
};
}
//==========================================================
else if ( currVerifierSet == PSMDetector.ALL ) {
mVerifier = new Verifier[] {
new UTF8Verifier(),
new SJISVerifier(),
new EUCJPVerifier(),
new ISO2022JPVerifier(),
new EUCKRVerifier(),
new ISO2022KRVerifier(),
new BIG5Verifier(),
new EUCTWVerifier(),
new GB2312Verifier(),
new GB18030Verifier(),
new ISO2022CNVerifier(),
new HZVerifier(),
new CP1252Verifier(),
new UCS2BEVerifier(),
new UCS2LEVerifier()
};
mStatisticsData = new EUCStatistics[] {
null,
null,
new EUCJPStatistics(),
null,
new EUCKRStatistics(),
null,
new Big5Statistics(),
new EUCTWStatistics(),
new GB2312Statistics(),
null,
null,
null,
null,
null,
null
};
}
mClassRunSampler = ( mStatisticsData != null ) ;
mClassItems = mVerifier.Length ;
}
public abstract void Report(String charset) ;
public bool HandleData(byte[] aBuf, int len) {
int i,j;
byte b, st;
for( i=0; i < len; i++) {
b = aBuf[i] ;
for (j=0; j < mItems; )
{
st = Verifier.getNextState( mVerifier[mItemIdx[j]],
b, mState[j]) ;
if (st == Verifier.eItsMe) {
Report( mVerifier[mItemIdx[j]].charset() );
mDone = true ;
return mDone ;
} else if (st == Verifier.eError ) {
mItems--;
if (j < mItems ) {
mItemIdx[j] = mItemIdx[mItems];
mState[j] = mState[mItems];
}
} else {
mState[j++] = st ;
}
}
if ( mItems <= 1 ) {
if( 1 == mItems) {
Report( mVerifier[mItemIdx[0]].charset() );
}
mDone = true ;
return mDone ;
}
else {
int nonUCS2Num=0;
int nonUCS2Idx=0;
for(j=0; j<mItems; j++) {
if ( (!(mVerifier[mItemIdx[j]].isUCS2())) &&
(!(mVerifier[mItemIdx[j]].isUCS2())) )
{
nonUCS2Num++ ;
nonUCS2Idx = j ;
}
}
if (1 == nonUCS2Num) {
Report( mVerifier[mItemIdx[nonUCS2Idx]].charset() );
mDone = true ;
return mDone ;
}
}
} // End of for( i=0; i < len ...
if (mRunSampler)
Sample(aBuf, len);
return mDone ;
}
public void DataEnd() {
if (mDone == true)
return ;
if (mItems == 2) {
if ((mVerifier[mItemIdx[0]].charset()).Equals("GB18030")) {
Report(mVerifier[mItemIdx[1]].charset()) ;
mDone = true ;
} else if ((mVerifier[mItemIdx[1]].charset()).Equals("GB18030")) {
Report(mVerifier[mItemIdx[0]].charset()) ;
mDone = true ;
}
}
if (mRunSampler)
Sample(null, 0, true);
}
public void Sample(byte[] aBuf, int aLen) {
Sample(aBuf, aLen, false) ;
}
public void Sample(byte[] aBuf, int aLen, bool aLastChance)
{
int possibleCandidateNum = 0;
int j;
int eucNum=0 ;
for (j=0; j< mItems; j++) {
if (null != mStatisticsData[mItemIdx[j]])
eucNum++ ;
if ((!mVerifier[mItemIdx[j]].isUCS2()) &&
(!(mVerifier[mItemIdx[j]].charset()).Equals("GB18030")))
possibleCandidateNum++ ;
}
mRunSampler = (eucNum > 1) ;
if (mRunSampler) {
mRunSampler = mSampler.Sample(aBuf, aLen);
if(((aLastChance && mSampler.GetSomeData()) ||
mSampler.EnoughData())
&& (eucNum == possibleCandidateNum)) {
mSampler.CalFreq();
int bestIdx = -1;
int eucCnt=0;
float bestScore = 0.0f;
for(j = 0; j < mItems; j++) {
if((null != mStatisticsData[mItemIdx[j]]) &&
(!(mVerifier[mItemIdx[j]].charset()).Equals("Big5")))
{
float score = mSampler.GetScore(
mStatisticsData[mItemIdx[j]].mFirstByteFreq(),
mStatisticsData[mItemIdx[j]].mFirstByteWeight(),
mStatisticsData[mItemIdx[j]].mSecondByteFreq(),
mStatisticsData[mItemIdx[j]].mSecondByteWeight() );
if(( 0 == eucCnt++) || (bestScore > score )) {
bestScore = score;
bestIdx = j;
} // if(( 0 == eucCnt++) || (bestScore > score ))
} // if(null != ...)
} // for
if (bestIdx >= 0)
{
Report( mVerifier[mItemIdx[bestIdx]].charset());
mDone = true;
}
} // if (eucNum == possibleCandidateNum)
} // if(mRunSampler)
}
public String[] getProbableCharsets() {
if (mItems <= 0) {
String[] nomatch = new String[1];
nomatch[0] = "nomatch" ;
return nomatch ;
}
string[] ret = new String[mItems] ;
for (int i=0; i<mItems; i++)
ret[i] = mVerifier[mItemIdx[i]].charset() ;
return ret ;
}
}
}