@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.
Files changed (52) hide show
  1. package/CONTRIBUTORS +4 -0
  2. package/LICENSE +504 -0
  3. package/README.md +101 -0
  4. package/dist/jschardet.js +7859 -0
  5. package/dist/jschardet.min.js +669 -0
  6. package/index.d.ts +13 -0
  7. package/index.js +1 -0
  8. package/package.json +33 -0
  9. package/src/big5freq.js +925 -0
  10. package/src/big5prober.js +54 -0
  11. package/src/chardistribution.js +301 -0
  12. package/src/charsetgroupprober.js +120 -0
  13. package/src/charsetprober.js +104 -0
  14. package/src/codingstatemachine.js +71 -0
  15. package/src/constants.js +40 -0
  16. package/src/escprober.js +109 -0
  17. package/src/escsm.js +250 -0
  18. package/src/eucjpprober.js +107 -0
  19. package/src/euckrfreq.js +597 -0
  20. package/src/euckrprober.js +54 -0
  21. package/src/euctwfreq.js +429 -0
  22. package/src/euctwprober.js +54 -0
  23. package/src/gb2312freq.js +473 -0
  24. package/src/gb2312prober.js +54 -0
  25. package/src/hebrewprober.js +323 -0
  26. package/src/index.js +56 -0
  27. package/src/jisfreq.js +569 -0
  28. package/src/jpcntx.js +242 -0
  29. package/src/langbulgarianmodel.js +228 -0
  30. package/src/langcyrillicmodel.js +329 -0
  31. package/src/langgreekmodel.js +225 -0
  32. package/src/langhebrewmodel.js +199 -0
  33. package/src/langhungarianmodel.js +225 -0
  34. package/src/langthaimodel.js +200 -0
  35. package/src/latin1prober.js +168 -0
  36. package/src/logger.js +7 -0
  37. package/src/mbcharsetprober.js +99 -0
  38. package/src/mbcsgroupprober.js +64 -0
  39. package/src/mbcssm/big5.js +52 -0
  40. package/src/mbcssm/eucjp.js +54 -0
  41. package/src/mbcssm/euckr.js +51 -0
  42. package/src/mbcssm/euctw.js +55 -0
  43. package/src/mbcssm/gb2312.js +60 -0
  44. package/src/mbcssm/sjis.js +54 -0
  45. package/src/mbcssm/ucs2be.js +56 -0
  46. package/src/mbcssm/ucs2le.js +56 -0
  47. package/src/mbcssm/utf8.js +75 -0
  48. package/src/sbcharsetprober.js +137 -0
  49. package/src/sbcsgroupprober.js +83 -0
  50. package/src/sjisprober.js +105 -0
  51. package/src/universaldetector.js +262 -0
  52. package/src/utf8prober.js +108 -0
@@ -0,0 +1,323 @@
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
+ // This prober doesn't actually recognize a language or a charset.
31
+ // It is a helper prober for the use of the Hebrew model probers
32
+
33
+ ////// General ideas of the Hebrew charset recognition //////
34
+ //
35
+ // Four main charsets exist in Hebrew:
36
+ // "ISO-8859-8" - Visual Hebrew
37
+ // "windows-1255" - Logical Hebrew
38
+ // "ISO-8859-8-I" - Logical Hebrew
39
+ // "x-mac-hebrew" - ?? Logical Hebrew ??
40
+ //
41
+ // Both "ISO" charsets use a completely identical set of code points, whereas
42
+ // "windows-1255" and "x-mac-hebrew" are two different proper supersets of
43
+ // these code points. windows-1255 defines additional characters in the range
44
+ // 0x80-0x9F as some misc punctuation marks as well as some Hebrew-specific
45
+ // diacritics and additional 'Yiddish' ligature letters in the range 0xc0-0xd6.
46
+ // x-mac-hebrew defines similar additional code points but with a different
47
+ // mapping.
48
+ //
49
+ // As far as an average Hebrew text with no diacritics is concerned, all four
50
+ // charsets are identical with respect to code points. Meaning that for the
51
+ // main Hebrew alphabet, all four map the same values to all 27 Hebrew letters
52
+ // (including final letters).
53
+ //
54
+ // The dominant difference between these charsets is their directionality.
55
+ // "Visual" directionality means that the text is ordered as if the renderer is
56
+ // not aware of a BIDI rendering algorithm. The renderer sees the text and
57
+ // draws it from left to right. The text itself when ordered naturally is read
58
+ // backwards. A buffer of Visual Hebrew generally looks like so:
59
+ // "[last word of first line spelled backwards] [whole line ordered backwards
60
+ // and spelled backwards] [first word of first line spelled backwards]
61
+ // [end of line] [last word of second line] ... etc' "
62
+ // adding punctuation marks, numbers and English text to visual text is
63
+ // naturally also "visual" and from left to right.
64
+ //
65
+ // "Logical" directionality means the text is ordered "naturally" according to
66
+ // the order it is read. It is the responsibility of the renderer to display
67
+ // the text from right to left. A BIDI algorithm is used to place general
68
+ // punctuation marks, numbers and English text in the text.
69
+ //
70
+ // Texts in x-mac-hebrew are almost impossible to find on the Internet. From
71
+ // what little evidence I could find, it seems that its general directionality
72
+ // is Logical.
73
+ //
74
+ // To sum up all of the above, the Hebrew probing mechanism knows about two
75
+ // charsets:
76
+ // Visual Hebrew - "ISO-8859-8" - backwards text - Words and sentences are
77
+ // backwards while line order is natural. For charset recognition purposes
78
+ // the line order is unimportant (In fact, for this implementation, even
79
+ // word order is unimportant).
80
+ // Logical Hebrew - "windows-1255" - normal, naturally ordered text.
81
+ //
82
+ // "ISO-8859-8-I" is a subset of windows-1255 and doesn't need to be
83
+ // specifically identified.
84
+ // "x-mac-hebrew" is also identified as windows-1255. A text in x-mac-hebrew
85
+ // that contain special punctuation marks or diacritics is displayed with
86
+ // some unconverted characters showing as question marks. This problem might
87
+ // be corrected using another model prober for x-mac-hebrew. Due to the fact
88
+ // that x-mac-hebrew texts are so rare, writing another model prober isn't
89
+ // worth the effort and performance hit.
90
+ //
91
+ //////// The Prober ////////
92
+ //
93
+ // The prober is divided between two SBCharSetProbers and a HebrewProber,
94
+ // all of which are managed, created, fed data, inquired and deleted by the
95
+ // SBCSGroupProber. The two SBCharSetProbers identify that the text is in
96
+ // fact some kind of Hebrew, Logical or Visual. The final decision about which
97
+ // one is it is made by the HebrewProber by combining final-letter scores
98
+ // with the scores of the two SBCharSetProbers to produce a final answer.
99
+ //
100
+ // The SBCSGroupProber is responsible for stripping the original text of HTML
101
+ // tags, English characters, numbers, low-ASCII punctuation characters, spaces
102
+ // and new lines. It reduces any sequence of such characters to a single space.
103
+ // The buffer fed to each prober in the SBCS group prober is pure text in
104
+ // high-ASCII.
105
+ // The two SBCharSetProbers (model probers) share the same language model:
106
+ // Win1255Model.
107
+ // The first SBCharSetProber uses the model normally as any other
108
+ // SBCharSetProber does, to recognize windows-1255, upon which this model was
109
+ // built. The second SBCharSetProber is told to make the pair-of-letter
110
+ // lookup in the language model backwards. This in practice exactly simulates
111
+ // a visual Hebrew model using the windows-1255 logical Hebrew model.
112
+ //
113
+ // The HebrewProber is not using any language model. All it does is look for
114
+ // final-letter evidence suggesting the text is either logical Hebrew or visual
115
+ // Hebrew. Disjointed from the model probers, the results of the HebrewProber
116
+ // alone are meaningless. HebrewProber always returns 0.00 as confidence
117
+ // since it never identifies a charset by itself. Instead, the pointer to the
118
+ // HebrewProber is passed to the model probers as a helper "Name Prober".
119
+ // When the Group prober receives a positive identification from any prober,
120
+ // it asks for the name of the charset identified. If the prober queried is a
121
+ // Hebrew model prober, the model prober forwards the call to the
122
+ // HebrewProber to make the final decision. In the HebrewProber, the
123
+ // decision is made according to the final-letters scores maintained and Both
124
+ // model probers scores. The answer is returned in the form of the name of the
125
+ // charset identified, either "windows-1255" or "ISO-8859-8".
126
+
127
+ var CharSetProber = require('./charsetprober');
128
+ var constants = require('./constants')
129
+
130
+ // https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/Array/IndexOf
131
+ if (!Array.prototype.indexOf)
132
+ {
133
+ Array.prototype.indexOf = function(elt /*, from*/)
134
+ {
135
+ var len = this.length >>> 0;
136
+
137
+ var from = Number(arguments[1]) || 0;
138
+ from = (from < 0)
139
+ ? Math.ceil(from)
140
+ : Math.floor(from);
141
+ if (from < 0)
142
+ from += len;
143
+
144
+ for (; from < len; from++)
145
+ {
146
+ if (from in this &&
147
+ this[from] === elt)
148
+ return from;
149
+ }
150
+ return -1;
151
+ };
152
+ }
153
+
154
+ function HebrewProber() {
155
+ CharSetProber.apply(this);
156
+
157
+ // windows-1255 / ISO-8859-8 code points of interest
158
+ var FINAL_KAF = '\xea'
159
+ var NORMAL_KAF = '\xeb'
160
+ var FINAL_MEM = '\xed'
161
+ var NORMAL_MEM = '\xee'
162
+ var FINAL_NUN = '\xef'
163
+ var NORMAL_NUN = '\xf0'
164
+ var FINAL_PE = '\xf3'
165
+ var NORMAL_PE = '\xf4'
166
+ var FINAL_TSADI = '\xf5'
167
+ var NORMAL_TSADI = '\xf6'
168
+
169
+ // Minimum Visual vs Logical final letter score difference.
170
+ // If the difference is below this, don't rely solely on the final letter score distance.
171
+ var MIN_FINAL_CHAR_DISTANCE = 5
172
+
173
+ // Minimum Visual vs Logical model score difference.
174
+ // If the difference is below this, don't rely at all on the model score distance.
175
+ var MIN_MODEL_DISTANCE = 0.01
176
+
177
+ var VISUAL_HEBREW_NAME = "ISO-8859-8"
178
+ var LOGICAL_HEBREW_NAME = "windows-1255"
179
+ var self = this;
180
+
181
+ function init() {
182
+ self._mLogicalProber = null;
183
+ self._mVisualProber = null;
184
+ self.reset();
185
+ }
186
+
187
+ this.reset = function() {
188
+ this._mFinalCharLogicalScore = 0;
189
+ this._mFinalCharVisualScore = 0;
190
+ // The two last characters seen in the previous buffer,
191
+ // mPrev and mBeforePrev are initialized to space in order to simulate a word
192
+ // delimiter at the beginning of the data
193
+ this._mPrev = " ";
194
+ this._mBeforePrev = " ";
195
+ // These probers are owned by the group prober.
196
+ }
197
+
198
+ this.setModelProbers = function(logicalProber, visualProber) {
199
+ this._mLogicalProber = logicalProber;
200
+ this._mVisualProber = visualProber;
201
+ }
202
+
203
+ this.isFinal = function(c) {
204
+ return [FINAL_KAF, FINAL_MEM, FINAL_NUN, FINAL_PE, FINAL_TSADI].indexOf(c) != -1;
205
+ }
206
+
207
+ this.isNonFinal = function(c) {
208
+ // The normal Tsadi is not a good Non-Final letter due to words like
209
+ // 'lechotet' (to chat) containing an apostrophe after the tsadi. This
210
+ // apostrophe is converted to a space in FilterWithoutEnglishLetters causing
211
+ // the Non-Final tsadi to appear at an end of a word even though this is not
212
+ // the case in the original text.
213
+ // The letters Pe and Kaf rarely display a related behavior of not being a
214
+ // good Non-Final letter. Words like 'Pop', 'Winamp' and 'Mubarak' for
215
+ // example legally end with a Non-Final Pe or Kaf. However, the benefit of
216
+ // these letters as Non-Final letters outweighs the damage since these words
217
+ // are quite rare.
218
+ return [NORMAL_KAF, NORMAL_MEM, NORMAL_NUN, NORMAL_PE].indexOf(c) != -1;
219
+ }
220
+
221
+ this.feed = function(aBuf) {
222
+ // Final letter analysis for logical-visual decision.
223
+ // Look for evidence that the received buffer is either logical Hebrew or
224
+ // visual Hebrew.
225
+ // The following cases are checked:
226
+ // 1) A word longer than 1 letter, ending with a final letter. This is an
227
+ // indication that the text is laid out "naturally" since the final letter
228
+ // really appears at the end. +1 for logical score.
229
+ // 2) A word longer than 1 letter, ending with a Non-Final letter. In normal
230
+ // Hebrew, words ending with Kaf, Mem, Nun, Pe or Tsadi, should not end with
231
+ // the Non-Final form of that letter. Exceptions to this rule are mentioned
232
+ // above in isNonFinal(). This is an indication that the text is laid out
233
+ // backwards. +1 for visual score
234
+ // 3) A word longer than 1 letter, starting with a final letter. Final letters
235
+ // should not appear at the beginning of a word. This is an indication that
236
+ // the text is laid out backwards. +1 for visual score.
237
+ //
238
+ // The visual score and logical score are accumulated throughout the text and
239
+ // are finally checked against each other in GetCharSetName().
240
+ // No checking for final letters in the middle of words is done since that case
241
+ // is not an indication for either Logical or Visual text.
242
+ //
243
+ // We automatically filter out all 7-bit characters (replace them with spaces)
244
+ // so the word boundary detection works properly. [MAP]
245
+
246
+ if( this.getState() == constants.notMe ) {
247
+ // Both model probers say it's not them. No reason to continue.
248
+ return constants.notMe;
249
+ }
250
+
251
+ aBuf = this.filterHighBitOnly(aBuf);
252
+
253
+ for( var i = 0, cur; i < aBuf.length; i++ ) {
254
+ cur = aBuf[i];
255
+ if( cur == " " ) {
256
+ // We stand on a space - a word just ended
257
+ if( this._mBeforePrev != " " ) {
258
+ // next-to-last char was not a space so self._mPrev is not a 1 letter word
259
+ if( this.isFinal(this._mPrev) ) {
260
+ // case (1) [-2:not space][-1:final letter][cur:space]
261
+ this._mFinalCharLogicalScore++;
262
+ } else if( this.isNonFinal(this._mPrev) ) {
263
+ // case (2) [-2:not space][-1:Non-Final letter][cur:space]
264
+ this._mFinalCharVisualScore++;
265
+ }
266
+ }
267
+ } else {
268
+ // Not standing on a space
269
+ if( this._mBeforePrev == " " && this.isFinal(this._mPrev) && cur != " " ) {
270
+ // case (3) [-2:space][-1:final letter][cur:not space]
271
+ this._mFinalCharVisualScore++;
272
+ }
273
+ }
274
+ this._mBeforePrev = this._mPrev;
275
+ this._mPrev = cur;
276
+ }
277
+ // Forever detecting, till the end or until both model probers return eNotMe (handled above)
278
+ return constants.detecting;
279
+ }
280
+
281
+ this.getCharsetName = function() {
282
+ // Make the decision: is it Logical or Visual?
283
+ // If the final letter score distance is dominant enough, rely on it.
284
+ var finalsub = this._mFinalCharLogicalScore - this._mFinalCharVisualScore;
285
+ if( finalsub >= MIN_FINAL_CHAR_DISTANCE ) {
286
+ return LOGICAL_HEBREW_NAME;
287
+ }
288
+ if( finalsub <= -MIN_FINAL_CHAR_DISTANCE ) {
289
+ return VISUAL_HEBREW_NAME;
290
+ }
291
+
292
+ // It's not dominant enough, try to rely on the model scores instead.
293
+ var modelsub = this._mLogicalProber.getConfidence() - this._mVisualProber.getConfidence();
294
+ if( modelsub > MIN_MODEL_DISTANCE ) {
295
+ return LOGICAL_HEBREW_NAME;
296
+ }
297
+ if( modelsub < -MIN_MODEL_DISTANCE ) {
298
+ return VISUAL_HEBREW_NAME;
299
+ }
300
+
301
+ // Still no good, back to final letter distance, maybe it'll save the day.
302
+ if( finalsub < 0 ) {
303
+ return VISUAL_HEBREW_NAME;
304
+ }
305
+
306
+ // (finalsub > 0 - Logical) or (don't know what to do) default to Logical.
307
+ return LOGICAL_HEBREW_NAME;
308
+ }
309
+
310
+ this.getState = function() {
311
+ // Remain active as long as any of the model probers are active.
312
+ if( this._mLogicalProber.getState() == constants.notMe &&
313
+ this._mVisualProber.getState() == constants.notMe ) {
314
+ return constants.notMe;
315
+ }
316
+ return constants.detecting;
317
+ }
318
+
319
+ init();
320
+ }
321
+ HebrewProber.prototype = new CharSetProber();
322
+
323
+ module.exports = HebrewProber
package/src/index.js ADDED
@@ -0,0 +1,56 @@
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 UniversalDetector = require('./universaldetector');
31
+ var setLogger = require('./logger').setLogger;
32
+
33
+ exports.detect = function(buffer, options) {
34
+ var u = runUniversalDetector(buffer, options);
35
+ return u.result;
36
+ }
37
+ exports.detectAll = function(buffer, options) {
38
+ var u = runUniversalDetector(buffer, options);
39
+ return u.results;
40
+ }
41
+ exports.UniversalDetector = UniversalDetector;
42
+ exports.enableDebug = function() {
43
+ setLogger(console.log.bind(console));
44
+ }
45
+
46
+ function runUniversalDetector(buffer, options) {
47
+ var u = new UniversalDetector(options);
48
+ u.reset();
49
+ if( typeof Buffer == 'function' && buffer instanceof Buffer ) {
50
+ u.feed(buffer.toString('binary'));
51
+ } else {
52
+ u.feed(buffer);
53
+ }
54
+ u.close();
55
+ return u;
56
+ }