@datawrapper/jschardet 3.0.1-0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CONTRIBUTORS +4 -0
- package/LICENSE +504 -0
- package/README.md +101 -0
- package/dist/jschardet.js +7859 -0
- package/dist/jschardet.min.js +669 -0
- package/index.d.ts +13 -0
- package/index.js +1 -0
- package/package.json +33 -0
- package/src/big5freq.js +925 -0
- package/src/big5prober.js +54 -0
- package/src/chardistribution.js +301 -0
- package/src/charsetgroupprober.js +120 -0
- package/src/charsetprober.js +104 -0
- package/src/codingstatemachine.js +71 -0
- package/src/constants.js +40 -0
- package/src/escprober.js +109 -0
- package/src/escsm.js +250 -0
- package/src/eucjpprober.js +107 -0
- package/src/euckrfreq.js +597 -0
- package/src/euckrprober.js +54 -0
- package/src/euctwfreq.js +429 -0
- package/src/euctwprober.js +54 -0
- package/src/gb2312freq.js +473 -0
- package/src/gb2312prober.js +54 -0
- package/src/hebrewprober.js +323 -0
- package/src/index.js +56 -0
- package/src/jisfreq.js +569 -0
- package/src/jpcntx.js +242 -0
- package/src/langbulgarianmodel.js +228 -0
- package/src/langcyrillicmodel.js +329 -0
- package/src/langgreekmodel.js +225 -0
- package/src/langhebrewmodel.js +199 -0
- package/src/langhungarianmodel.js +225 -0
- package/src/langthaimodel.js +200 -0
- package/src/latin1prober.js +168 -0
- package/src/logger.js +7 -0
- package/src/mbcharsetprober.js +99 -0
- package/src/mbcsgroupprober.js +64 -0
- package/src/mbcssm/big5.js +52 -0
- package/src/mbcssm/eucjp.js +54 -0
- package/src/mbcssm/euckr.js +51 -0
- package/src/mbcssm/euctw.js +55 -0
- package/src/mbcssm/gb2312.js +60 -0
- package/src/mbcssm/sjis.js +54 -0
- package/src/mbcssm/ucs2be.js +56 -0
- package/src/mbcssm/ucs2le.js +56 -0
- package/src/mbcssm/utf8.js +75 -0
- package/src/sbcharsetprober.js +137 -0
- package/src/sbcsgroupprober.js +83 -0
- package/src/sjisprober.js +105 -0
- package/src/universaldetector.js +262 -0
- package/src/utf8prober.js +108 -0
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The Original Code is Mozilla Universal charset detector code.
|
|
3
|
+
*
|
|
4
|
+
* The Initial Developer of the Original Code is
|
|
5
|
+
* Netscape Communications Corporation.
|
|
6
|
+
* Portions created by the Initial Developer are Copyright (C) 2001
|
|
7
|
+
* the Initial Developer. All Rights Reserved.
|
|
8
|
+
*
|
|
9
|
+
* Contributor(s):
|
|
10
|
+
* António Afonso (antonio.afonso gmail.com) - port to JavaScript
|
|
11
|
+
* Mark Pilgrim - port to Python
|
|
12
|
+
* Shy Shalom - original C code
|
|
13
|
+
*
|
|
14
|
+
* This library is free software; you can redistribute it and/or
|
|
15
|
+
* modify it under the terms of the GNU Lesser General Public
|
|
16
|
+
* License as published by the Free Software Foundation; either
|
|
17
|
+
* version 2.1 of the License, or (at your option) any later version.
|
|
18
|
+
*
|
|
19
|
+
* This library is distributed in the hope that it will be useful,
|
|
20
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
21
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
22
|
+
* Lesser General Public License for more details.
|
|
23
|
+
*
|
|
24
|
+
* You should have received a copy of the GNU Lesser General Public
|
|
25
|
+
* License along with this library; if not, write to the Free Software
|
|
26
|
+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
|
|
27
|
+
* 02110-1301 USA
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* This is a port from the python port, version "2.0.1"
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
var constants = require('./constants');
|
|
35
|
+
var MBCSGroupProber = require('./mbcsgroupprober');
|
|
36
|
+
var SBCSGroupProber = require('./sbcsgroupprober');
|
|
37
|
+
var Latin1Prober = require('./latin1prober');
|
|
38
|
+
var EscCharSetProber = require('./escprober');
|
|
39
|
+
var logger = require('./logger');
|
|
40
|
+
|
|
41
|
+
const supportedEncodings = (function() {
|
|
42
|
+
const BOM_UTF = [
|
|
43
|
+
"UTF-8", "UTF-32LE", "UTF-32BE", "UTF-32BE", "UTF-16LE", "UTF-16BE",
|
|
44
|
+
"X-ISO-10646-UCS-4-3412", "X-ISO-10646-UCS-4-2143"
|
|
45
|
+
]
|
|
46
|
+
const probers = [
|
|
47
|
+
new EscCharSetProber(),
|
|
48
|
+
new MBCSGroupProber(),
|
|
49
|
+
new SBCSGroupProber(),
|
|
50
|
+
new Latin1Prober()
|
|
51
|
+
];
|
|
52
|
+
const encodings = BOM_UTF.slice(0);
|
|
53
|
+
for (const prober of probers) {
|
|
54
|
+
[].push.apply(encodings, prober.getSupportedCharsetNames());
|
|
55
|
+
}
|
|
56
|
+
return encodings;
|
|
57
|
+
})();
|
|
58
|
+
|
|
59
|
+
const supportedEncodingsDenormalized = (function() {
|
|
60
|
+
denormalizedEncodings = [];
|
|
61
|
+
for (const encoding of supportedEncodings) {
|
|
62
|
+
denormalizedEncodings.push(
|
|
63
|
+
encoding.toLocaleLowerCase(),
|
|
64
|
+
encoding.toLocaleLowerCase().replace(/-/g, "")
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
return denormalizedEncodings;
|
|
68
|
+
})();
|
|
69
|
+
|
|
70
|
+
function UniversalDetector(options) {
|
|
71
|
+
if (!options) options = {};
|
|
72
|
+
if (!options.minimumThreshold) options.minimumThreshold = 0.20;
|
|
73
|
+
|
|
74
|
+
if (options.detectEncodings) {
|
|
75
|
+
for (const encoding of options.detectEncodings) {
|
|
76
|
+
if (!supportedEncodingsDenormalized.includes(encoding.toLowerCase())) {
|
|
77
|
+
throw new Error(`Encoding ${encoding} is not supported. Supported encodings: ${supportedEncodings}.`);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
var _state = {
|
|
83
|
+
pureAscii : 0,
|
|
84
|
+
escAscii : 1,
|
|
85
|
+
highbyte : 2
|
|
86
|
+
};
|
|
87
|
+
var self = this;
|
|
88
|
+
|
|
89
|
+
function init() {
|
|
90
|
+
self._highBitDetector = /[\x80-\xFF]/;
|
|
91
|
+
self._escDetector = /(\x1B|~\{)/;
|
|
92
|
+
self._mEscCharsetProber = null;
|
|
93
|
+
self._mCharsetProbers = [];
|
|
94
|
+
self.reset();
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function canDetectEncoding(encoding) {
|
|
98
|
+
if (!options.detectEncodings) {
|
|
99
|
+
return true;
|
|
100
|
+
}
|
|
101
|
+
return options.detectEncodings.includes(encoding.toLowerCase());
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
this.reset = function() {
|
|
105
|
+
this.result = {"encoding": null, "confidence": 0.0};
|
|
106
|
+
this.results = []
|
|
107
|
+
this.done = false;
|
|
108
|
+
this._mStart = true;
|
|
109
|
+
this._mGotData = false;
|
|
110
|
+
this._mInputState = _state.pureAscii;
|
|
111
|
+
this._mLastChar = [];
|
|
112
|
+
this._mBOM = "";
|
|
113
|
+
if( this._mEscCharsetProber ) {
|
|
114
|
+
this._mEscCharsetProber.reset();
|
|
115
|
+
}
|
|
116
|
+
for( var i = 0, prober; prober = this._mCharsetProbers[i]; i++ ) {
|
|
117
|
+
prober.reset();
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
this.feed = function(aBuf) {
|
|
122
|
+
if( this.done ) return;
|
|
123
|
+
|
|
124
|
+
var aLen = aBuf.length;
|
|
125
|
+
if( !aLen ) return;
|
|
126
|
+
|
|
127
|
+
if( !this._mGotData ) {
|
|
128
|
+
this._mBOM += aBuf;
|
|
129
|
+
// If the data starts with BOM, we know it is UTF
|
|
130
|
+
if( this._mBOM.slice(0,3) == "\xEF\xBB\xBF" && canDetectEncoding("UTF-8")) {
|
|
131
|
+
// EF BB BF UTF-8 with BOM
|
|
132
|
+
this.result = {"encoding": "UTF-8", "confidence": 1.0};
|
|
133
|
+
} else if( this._mBOM.slice(0,4) == "\xFF\xFE\x00\x00" && canDetectEncoding("UTF-32LE") ) {
|
|
134
|
+
// FF FE 00 00 UTF-32, little-endian BOM
|
|
135
|
+
this.result = {"encoding": "UTF-32LE", "confidence": 1.0};
|
|
136
|
+
} else if( this._mBOM.slice(0,4) == "\x00\x00\xFE\xFF" && canDetectEncoding("UTF-32BE")) {
|
|
137
|
+
// 00 00 FE FF UTF-32, big-endian BOM
|
|
138
|
+
this.result = {"encoding": "UTF-32BE", "confidence": 1.0};
|
|
139
|
+
} else if( this._mBOM.slice(0,4) == "\xFE\xFF\x00\x00" && canDetectEncoding("X-ISO-10646-UCS-4-3412")) {
|
|
140
|
+
// FE FF 00 00 UCS-4, unusual octet order BOM (3412)
|
|
141
|
+
this.result = {"encoding": "X-ISO-10646-UCS-4-3412", "confidence": 1.0};
|
|
142
|
+
} else if( this._mBOM.slice(0,4) == "\x00\x00\xFF\xFE" && canDetectEncoding("X-ISO-10646-UCS-4-2143")) {
|
|
143
|
+
// 00 00 FF FE UCS-4, unusual octet order BOM (2143)
|
|
144
|
+
this.result = {"encoding": "X-ISO-10646-UCS-4-2143", "confidence": 1.0};
|
|
145
|
+
} else if( this._mBOM.slice(0,2) == "\xFF\xFE" && canDetectEncoding("UTF-16LE")) {
|
|
146
|
+
// FF FE UTF-16, little endian BOM
|
|
147
|
+
this.result = {"encoding": "UTF-16LE", "confidence": 1.0};
|
|
148
|
+
} else if( this._mBOM.slice(0,2) == "\xFE\xFF" && canDetectEncoding("UTF-16BE")) {
|
|
149
|
+
// FE FF UTF-16, big endian BOM
|
|
150
|
+
this.result = {"encoding": "UTF-16BE", "confidence": 1.0};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (this.result.confidence > 0) {
|
|
154
|
+
this.results = [this.result];
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// If we got to 4 chars without being able to detect a BOM we
|
|
158
|
+
// stop trying.
|
|
159
|
+
if( this._mBOM.length > 3 ) {
|
|
160
|
+
this._mGotData = true;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if( this.result.encoding && (this.result.confidence > 0.0) ) {
|
|
165
|
+
this.done = true;
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if( this._mInputState == _state.pureAscii ) {
|
|
170
|
+
if( this._highBitDetector.test(aBuf) ) {
|
|
171
|
+
this._mInputState = _state.highbyte;
|
|
172
|
+
} else if( this._escDetector.test(this._mLastChar.join('') + aBuf) ) {
|
|
173
|
+
this._mInputState = _state.escAscii;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
this._mLastChar = aBuf.slice(-1).split('');
|
|
178
|
+
|
|
179
|
+
if( this._mInputState == _state.escAscii ) {
|
|
180
|
+
if( !this._mEscCharsetProber ) {
|
|
181
|
+
this._mEscCharsetProber = new EscCharSetProber();
|
|
182
|
+
}
|
|
183
|
+
if( this._mEscCharsetProber.feed(aBuf) == constants.foundIt && canDetectEncoding(this._mEscCharsetProber.getCharsetName()) ) {
|
|
184
|
+
this.result = {
|
|
185
|
+
"encoding": this._mEscCharsetProber.getCharsetName(),
|
|
186
|
+
"confidence": this._mEscCharsetProber.getConfidence()
|
|
187
|
+
};
|
|
188
|
+
this.results = [this.result];
|
|
189
|
+
this.done = true;
|
|
190
|
+
}
|
|
191
|
+
} else if( this._mInputState == _state.highbyte ) {
|
|
192
|
+
if( this._mCharsetProbers.length == 0 ) {
|
|
193
|
+
this._mCharsetProbers = [
|
|
194
|
+
new MBCSGroupProber(),
|
|
195
|
+
new SBCSGroupProber(),
|
|
196
|
+
new Latin1Prober()
|
|
197
|
+
];
|
|
198
|
+
}
|
|
199
|
+
for( var i = 0, prober; prober = this._mCharsetProbers[i]; i++ ) {
|
|
200
|
+
if( prober.feed(aBuf) == constants.foundIt && canDetectEncoding(prober.getCharsetName()) ) {
|
|
201
|
+
this.result = {
|
|
202
|
+
"encoding": prober.getCharsetName(),
|
|
203
|
+
"confidence": prober.getConfidence()
|
|
204
|
+
};
|
|
205
|
+
this.results = [this.result];
|
|
206
|
+
this.done = true;
|
|
207
|
+
break;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
this.close = function() {
|
|
214
|
+
if( this.done ) return;
|
|
215
|
+
if( this._mBOM.length === 0 ) {
|
|
216
|
+
logger.log("no data received!\n");
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
this.done = true;
|
|
220
|
+
|
|
221
|
+
if( this._mInputState == _state.pureAscii && canDetectEncoding("ascii") ) {
|
|
222
|
+
logger.log("pure ascii")
|
|
223
|
+
this.result = {"encoding": "ascii", "confidence": 1.0};
|
|
224
|
+
this.results.push(this.result);
|
|
225
|
+
return this.result;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
if( this._mInputState == _state.highbyte ) {
|
|
229
|
+
for( var i = 0, prober; prober = this._mCharsetProbers[i]; i++ ) {
|
|
230
|
+
if( !prober || !prober.getCharsetName() || !canDetectEncoding(prober.getCharsetName()) ) continue;
|
|
231
|
+
this.results.push({
|
|
232
|
+
"encoding": prober.getCharsetName(),
|
|
233
|
+
"confidence": prober.getConfidence()
|
|
234
|
+
});
|
|
235
|
+
logger.log(prober.getCharsetName() + " confidence " + prober.getConfidence());
|
|
236
|
+
}
|
|
237
|
+
this.results.sort(function(a, b) {
|
|
238
|
+
return b.confidence - a.confidence;
|
|
239
|
+
});
|
|
240
|
+
if (this.results.length > 0) {
|
|
241
|
+
var topResult = this.results[0];
|
|
242
|
+
if (topResult.confidence >= options.minimumThreshold) {
|
|
243
|
+
this.result = topResult;
|
|
244
|
+
return topResult;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
if( logger.enabled ) {
|
|
250
|
+
logger.log("no probers hit minimum threshhold\n");
|
|
251
|
+
for( var i = 0, prober; prober = this._mCharsetProbers[i]; i++ ) {
|
|
252
|
+
if( !prober || !canDetectEncoding(prober.getCharsetName()) ) continue;
|
|
253
|
+
logger.log(prober.getCharsetName() + " confidence = " +
|
|
254
|
+
prober.getConfidence() + "\n");
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
init();
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
module.exports = UniversalDetector;
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The Original Code is Mozilla Universal charset detector code.
|
|
3
|
+
*
|
|
4
|
+
* The Initial Developer of the Original Code is
|
|
5
|
+
* Netscape Communications Corporation.
|
|
6
|
+
* Portions created by the Initial Developer are Copyright (C) 2001
|
|
7
|
+
* the Initial Developer. All Rights Reserved.
|
|
8
|
+
*
|
|
9
|
+
* Contributor(s):
|
|
10
|
+
* António Afonso (antonio.afonso gmail.com) - port to JavaScript
|
|
11
|
+
* Mark Pilgrim - port to Python
|
|
12
|
+
* Shy Shalom - original C code
|
|
13
|
+
*
|
|
14
|
+
* This library is free software; you can redistribute it and/or
|
|
15
|
+
* modify it under the terms of the GNU Lesser General Public
|
|
16
|
+
* License as published by the Free Software Foundation; either
|
|
17
|
+
* version 2.1 of the License, or (at your option) any later version.
|
|
18
|
+
*
|
|
19
|
+
* This library is distributed in the hope that it will be useful,
|
|
20
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
21
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
22
|
+
* Lesser General Public License for more details.
|
|
23
|
+
*
|
|
24
|
+
* You should have received a copy of the GNU Lesser General Public
|
|
25
|
+
* License along with this library; if not, write to the Free Software
|
|
26
|
+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
|
|
27
|
+
* 02110-1301 USA
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
var CodingStateMachine = require('./codingstatemachine');
|
|
31
|
+
var CharSetProber = require('./charsetprober');
|
|
32
|
+
var constants = require('./constants');
|
|
33
|
+
var UTF8SMModel = require('./mbcssm/utf8');
|
|
34
|
+
|
|
35
|
+
function UTF8Prober() {
|
|
36
|
+
CharSetProber.apply(this);
|
|
37
|
+
|
|
38
|
+
var ONE_CHAR_PROB = 0.5;
|
|
39
|
+
var self = this;
|
|
40
|
+
|
|
41
|
+
function init() {
|
|
42
|
+
self._mCodingSM = new CodingStateMachine(UTF8SMModel);
|
|
43
|
+
self.reset();
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
this.reset = function() {
|
|
47
|
+
UTF8Prober.prototype.reset.apply(this);
|
|
48
|
+
this._mCodingSM.reset();
|
|
49
|
+
this._mNumOfMBChar = 0;
|
|
50
|
+
this._mMBCharLen = 0;
|
|
51
|
+
this._mFullLen = 0;
|
|
52
|
+
this._mBasicAsciiLen = 0;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
this.getCharsetName = function() {
|
|
56
|
+
return "UTF-8";
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
this.feed = function(aBuf) {
|
|
60
|
+
this._mFullLen += aBuf.length;
|
|
61
|
+
for( var i = 0, c; i < aBuf.length; i++ ) {
|
|
62
|
+
c = aBuf[i];
|
|
63
|
+
var codingState = this._mCodingSM.nextState(c);
|
|
64
|
+
if( codingState == constants.error ) {
|
|
65
|
+
this._mState = constants.notMe;
|
|
66
|
+
break;
|
|
67
|
+
} else if( codingState == constants.itsMe ) {
|
|
68
|
+
this._mState = constants.foundIt;
|
|
69
|
+
break;
|
|
70
|
+
} else if( codingState == constants.start ) {
|
|
71
|
+
if( this._mCodingSM.getCurrentCharLen() >= 2 ) {
|
|
72
|
+
this._mNumOfMBChar++;
|
|
73
|
+
this._mMBCharLen += this._mCodingSM.getCurrentCharLen();
|
|
74
|
+
} else if( c.charCodeAt(0) < 128 ) { // codes higher than 127 are extended ASCII
|
|
75
|
+
this._mBasicAsciiLen++;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if( this.getState() == constants.detecting ) {
|
|
81
|
+
if( this.getConfidence() > constants.SHORTCUT_THRESHOLD ) {
|
|
82
|
+
this._mState = constants.foundIt;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return this.getState();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
this.getConfidence = function() {
|
|
90
|
+
var unlike = 0.99;
|
|
91
|
+
var mbCharRatio = 0;
|
|
92
|
+
var nonBasciAsciiLen = (this._mFullLen - this._mBasicAsciiLen);
|
|
93
|
+
if( nonBasciAsciiLen > 0 ) {
|
|
94
|
+
mbCharRatio = this._mMBCharLen / nonBasciAsciiLen;
|
|
95
|
+
}
|
|
96
|
+
if( this._mNumOfMBChar < 6 && mbCharRatio <= 0.6 ) {
|
|
97
|
+
unlike *= Math.pow(ONE_CHAR_PROB, this._mNumOfMBChar);
|
|
98
|
+
return 1 - unlike;
|
|
99
|
+
} else {
|
|
100
|
+
return unlike;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
init();
|
|
105
|
+
}
|
|
106
|
+
UTF8Prober.prototype = new CharSetProber();
|
|
107
|
+
|
|
108
|
+
module.exports = UTF8Prober;
|