-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdetector.go
319 lines (282 loc) · 8.98 KB
/
detector.go
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
package chardet
import (
"bytes"
"github.com/wlynxg/chardet/consts"
"github.com/wlynxg/chardet/probe"
)
// Result represents the character encoding detection result
type Result struct {
// Encoding is the detected character encoding name
Encoding string `json:"encoding,omitempty"`
// Confidence indicates how confident the detector is about the result (0.0-1.0)
Confidence float64 `json:"confidence,omitempty"`
// Language represents the detected language (if applicable)
Language string `json:"language,omitempty"`
}
// UniversalDetector implements universal character encoding detection
type UniversalDetector struct {
// MinimumThreshold is the minimum confidence threshold for detection
MinimumThreshold float64
// IsoWinMap maps ISO encodings to Windows encodings
IsoWinMap map[string]string
// done indicates if detection is complete
done bool
// gotData indicates if any data has been processed
gotData bool
// hasWinBytes indicates if Windows-specific bytes were detected
hasWinBytes bool
// lastChars stores the last processed characters
lastChars []byte
// inputState tracks the current input processing state
inputState consts.InputState
// filter specifies which languages to detect
filter consts.LangFilter
// Various probes used for detection
escCharsetProbe probe.Probe
utf1632Probe *probe.UTF1632Probe
charsetProbes []probe.Probe
// result stores the final detection result
result Result
}
// NewUniversalDetector creates a new UniversalDetector instance with the specified language filter
func NewUniversalDetector(filter consts.LangFilter) *UniversalDetector {
if filter == consts.UnknownLangFilter {
filter = consts.AllLangFilter
}
return &UniversalDetector{
MinimumThreshold: 0.20,
IsoWinMap: map[string]string{
consts.ISO88591: consts.Windows1252,
consts.ISO88592: consts.Windows1250,
consts.ISO88595: consts.Windows1251,
consts.ISO88596: consts.Windows1256,
consts.ISO88597: consts.Windows1253,
consts.ISO88598: consts.Windows1255,
consts.ISO88599: consts.Windows1254,
consts.ISO885913: consts.Windows1257,
},
inputState: consts.PureAsciiInputState,
lastChars: []byte{},
filter: filter,
}
}
// Reset resets the detector to its initial state
func (u *UniversalDetector) Reset() {
u.result = Result{}
u.done = false
u.gotData = false
u.hasWinBytes = false
u.inputState = consts.PureAsciiInputState
u.lastChars = []byte{}
if u.escCharsetProbe != nil {
u.escCharsetProbe.Reset()
}
if u.utf1632Probe != nil {
u.utf1632Probe.Reset()
}
for _, p := range u.charsetProbes {
if p != nil {
p.Reset()
}
}
}
// Feed processes a chunk of bytes for character encoding detection
// It analyzes the input data and updates the internal state accordingly
func (u *UniversalDetector) Feed(buf []byte) {
if u.done || len(buf) == 0 {
return
}
// First check for known BOMs, since these are guaranteed to be correct
if !u.gotData {
// If the buf starts with BOM, we know it is UTF
var encoding string
if bytes.HasPrefix(buf, []byte(consts.UTF8BOM)) {
// EF BB BF UTF-8 with BOM
encoding = consts.UTF8SIG
} else if bytes.HasPrefix(buf, []byte(consts.UTF32LEBOM)) || bytes.HasPrefix(buf, []byte(consts.UTF32BEBOM)) {
// FF FE 00 00 UTF-32, little-endian BOM
// 00 00 FE FF UTF-32, big-endian BOM
encoding = consts.UTF32
} else if bytes.HasPrefix(buf, []byte(consts.UCS43412BOM)) {
// FE FF 00 00 UCS-4, unusual octet order BOM (3412)
encoding = consts.UCS43412
} else if bytes.HasPrefix(buf, []byte(consts.UCS42143BOM)) {
// 00 00 FF FE UCS-4, unusual octet order BOM (2143)
encoding = consts.UCS42143
} else if bytes.HasPrefix(buf, []byte(consts.UTF16LEBOM)) || bytes.HasPrefix(buf, []byte(consts.UTF16BEBOM)) {
// FF FE UTF-16, little endian BOM
// FE FF UTF-16, big endian BOM
encoding = consts.UTF16
}
u.gotData = true
if encoding != "" {
u.result = Result{
Encoding: encoding,
Confidence: 1.0,
Language: "",
}
u.done = true
return
}
}
// If none of those matched, and we've only seen ASCII so far, check
// for high bytes and escape sequences.
if u.inputState == consts.PureAsciiInputState {
if HighByteDetector(buf) {
u.inputState = consts.HighByteInputState
} else if u.inputState == consts.PureAsciiInputState &&
EscDetector(bytes.Join([][]byte{u.lastChars, buf}, nil)) {
u.inputState = consts.EcsAsciiInputState
}
}
u.lastChars = append(u.lastChars, buf[len(buf)-1])
// next we will look to see if it is appears to be either a UTF-16 or UTF-32 encoding
if u.utf1632Probe == nil {
u.utf1632Probe = probe.NewUTF1632Probe()
}
if u.utf1632Probe.State() == consts.DetectingProbingState {
if u.utf1632Probe.Feed(buf) == consts.FoundItProbingState {
u.result = Result{
Encoding: u.utf1632Probe.CharSetName(),
Confidence: u.utf1632Probe.GetConfidence(),
Language: "",
}
u.done = true
return
}
}
switch u.inputState {
case consts.EcsAsciiInputState:
// If we've seen escape sequences, use the EscCharSetProbe, which
// uses a simple state machine to check for known escape sequences in
// HZ and ISO-2022 encodings, since those are the only encodings that
// use such sequences.
if u.escCharsetProbe == nil {
u.escCharsetProbe = probe.NewEscCharSetProbe(u.filter)
}
if u.escCharsetProbe.Feed(buf) == consts.FoundItProbingState {
u.result = Result{
Encoding: u.escCharsetProbe.CharSetName(),
Confidence: u.escCharsetProbe.GetConfidence(),
Language: u.escCharsetProbe.Language(),
}
u.done = true
}
case consts.HighByteInputState:
// If we've seen high bytes (i.e., those with values greater than 127),
// we need to do more complicated checks using all our multi-byte and
// single-byte probes that are left. The single-byte probes
// use character bigram distributions to determine the encoding, whereas
// the multi-byte probes use a combination of character unigram and
// bigram distributions.
if len(u.charsetProbes) == 0 {
u.charsetProbes = []probe.Probe{probe.MBCGroupProbe(u.filter)}
// If we're checking non-CJK encodings, use single-byte probe
if u.filter&consts.NonCjkLangFilter != 0 {
u.charsetProbes = append(u.charsetProbes, probe.NewSBCSGroupProbe())
}
u.charsetProbes = append(u.charsetProbes, probe.NewLatin1Probe(), probe.NewMacRomanProbe())
}
for _, charsetProbe := range u.charsetProbes {
if charsetProbe == nil {
continue
}
if charsetProbe.Feed(buf) == consts.FoundItProbingState {
u.result = Result{
Encoding: charsetProbe.CharSetName(),
Confidence: charsetProbe.GetConfidence(),
Language: charsetProbe.Language(),
}
u.done = true
break
}
}
if WinByteDetector(buf) {
u.hasWinBytes = true
}
default:
}
}
// GetResult returns the final character encoding detection result
// If detection is not complete, it will finalize the detection process
func (u *UniversalDetector) GetResult() Result {
if u.done {
return u.result
}
u.done = true
switch {
case !u.gotData:
case u.inputState == consts.PureAsciiInputState:
u.result = Result{
Encoding: consts.Ascii,
Confidence: 1.0,
Language: "",
}
case u.inputState == consts.HighByteInputState:
var (
confidence, maxProbeConfidence float64
maxConfidenceProbe probe.Probe
)
for _, charsetProbe := range u.charsetProbes {
if charsetProbe == nil {
continue
}
confidence = charsetProbe.GetConfidence()
if confidence > maxProbeConfidence {
maxProbeConfidence = confidence
maxConfidenceProbe = charsetProbe
}
}
if maxConfidenceProbe != nil && maxProbeConfidence > u.MinimumThreshold {
charsetName := maxConfidenceProbe.CharSetName()
confidence := maxConfidenceProbe.GetConfidence()
if u.hasWinBytes {
// Use Windows encoding name instead of ISO-8859 if we saw any
// extra Windows-specific bytes
if n, ok := u.IsoWinMap[maxConfidenceProbe.CharSetName()]; ok {
charsetName = n
}
}
u.result = Result{
Encoding: charsetName,
Confidence: confidence,
Language: maxConfidenceProbe.Language(),
}
}
}
return u.result
}
// HighByteDetector checks if the buffer contains any bytes with values >= 0x80
func HighByteDetector(buf []byte) bool {
for _, b := range buf {
if b >= 0x80 { // Check if the byte is in the international range
return true
}
}
return false
}
// EscDetector checks if the buffer contains escape sequences
// commonly used in certain character encodings
func EscDetector(buf []byte) bool {
for i := 0; i < len(buf); i++ {
// Check for ESC character (0x1B)
if buf[i] == 0x1B {
return true
}
// Check for ~ character (0x7E) followed by {
if buf[i] == 0x7E && i+1 < len(buf) && buf[i+1] == '{' {
return true
}
}
return false
}
// WinByteDetector checks if the buffer contains Windows-specific byte values
// in the range 0x80-0x9F
func WinByteDetector(buf []byte) bool {
for _, b := range buf {
if b >= 0x80 && b <= 0x9F { // Check if the byte is in the specified range
return true
}
}
return false
}