-
Notifications
You must be signed in to change notification settings - Fork 84
/
Parser.js
327 lines (272 loc) Β· 7.71 KB
/
Parser.js
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
var dbUnicodeBlocks = require('../data/unicode_blocks.json');
/**
* This class represents a text sample to be parsed.
*
* Largely inspired from the PHP Pear Package Text_LanguageDetect by Nicholas Pisarro
* Licence: http://www.debian.org/misc/bsd.license BSD
*
* @author Francois-Guillaume Ribreau - @FGRibreau
* @author Ruslan Zavackiy - @Chaoser
*
* @see https://github.com/FGRibreau/node-language-detect
*/
var Parser = module.exports = function (string) {
/**
* The size of the trigram data arrays
*
* @access private
* @var int
*/
this.threshold = 300;
/**
* stores the trigram ranks of the sample
*
* @access private
* @var array
*/
this.trigramRanks = {};
/**
* Whether the parser should compile trigrams
*
* @access private
* @var bool
*/
this.compileTrigram = true;
this.compileUnicode = true;
this.unicodeSkipAscii = true;
this.unicodeBlocks = {};
/**
* Whether the trigram parser should pad the beginning of the string
*
* @access private
* @var bool
*/
this.trigramPadStart = false;
this.trigram = {};
/**
* the piece of text being parsed
*
* @access private
* @var string
*/
/**
* Constructor
*
* @access private
* @param string string to be parsed
*/
this.string = string ? string.replace(/[~!@#$%^&*()_|+\-=?;:",.<>\{\}\[\]\\\/]/g, ' ') : '';
};
Parser.prototype = {
/**
* turn on/off padding the beginning of the sample string
*
* @access public
* @param bool true for on, false for off
*/
setPadStart: function (bool) {
this.trigramPadStart = bool || true;
},
/**
* Returns the trigram ranks for the text sample
*
* @access public
* @return array trigram ranks in the text sample
*/
getTrigramRanks: function () {
return this.trigramRanks;
},
getBlockCount: function () {
return dbUnicodeBlocks.length;
},
getUnicodeBlocks: function () {
return this.unicodeBlocks;
},
/**
* Executes the parsing operation
*
* Be sure to call the set*() functions to set options and the
* prepare*() functions first to tell it what kind of data to compute
*
* Afterwards the get*() functions can be used to access the compiled
* information.
*
* @access public
*/
analyze: function () {
var len = this.string.length
, byteCounter = 0
, a = ' ', b = ' '
, dropone, c;
if (this.compileUnicode) {
var blocksCount = dbUnicodeBlocks.length;
}
// trigram startup
if (this.compileTrigram) {
// initialize them as blank so the parser will skip the first two
// (since it skips trigrams with more than 2 contiguous spaces)
a = ' ';
b = ' ';
// kludge
// if it finds a valid trigram to start and the start pad option is
// off, then set a variable that will be used to reduce this
// trigram after parsing has finished
if (!this.trigramPadStart) {
a = this.string.charAt(byteCounter++).toLowerCase();
if (a != ' ') {
b = this.string.charAt(byteCounter).toLowerCase();
dropone = ' ' + a + b;
}
byteCounter = 0;
a = ' ';
b = ' ';
}
}
var skippedCount = 0;
var unicodeChars = {};
while (byteCounter < len) {
c = this.string.charAt(byteCounter++).toLowerCase();
// language trigram detection
if (this.compileTrigram) {
if (!(b == ' ' && (a == ' ' || c == ' '))) {
var abc = a + b + c;
this.trigram[abc] = this.trigram[abc] ? this.trigram[abc] += 1 : 1;
}
a = b;
b = c;
}
if (this.compileUnicode) {
var charCode = c.charCodeAt(0);
if (this.unicodeSkipAscii
&& c.match(/[a-z ]/i)
&& (charCode < 65 || charCode > 122 || (charCode > 90 && charCode < 97))
&& c != "'") {
skippedCount++;
continue;
}
unicodeChars[c] = unicodeChars[c] ? unicodeChars[c] += 1 : 1;
}
}
this.unicodeBlocks = {};
if (this.compileUnicode) {
var keys = Object.keys(unicodeChars)
, keysLength = keys.length;
for (var i = keysLength; i--;) {
var unicode = keys[i].charCodeAt(0)
, count = unicodeChars[keys[i]]
, search = this.unicodeBlockName(unicode, blocksCount)
, blockName = search != -1 ? search[2] : '[Malformatted]';
this.unicodeBlocks[blockName] = this.unicodeBlocks[blockName] ? this.unicodeBlocks[blockName] += count : count;
}
}
// trigram cleanup
if (this.compileTrigram) {
// pad the end
if (b != ' ') {
var ab = a + b + ' ';
this.trigram[ab] = this.trigram[ab] ? this.trigram[ab] += 1 : 1;
}
// perl compatibility; Language::Guess does not pad the beginning
// kludge
if (typeof dropone != 'undefined' && this.trigram[dropone] == 1) {
delete this.trigram[dropone];
}
if (this.trigram && Object.keys(this.trigram).length > 0) {
this.trigramRanks = this.arrRank(this.trigram);
} else {
this.trigramRanks = {};
}
}
},
/**
* Sorts an array by value breaking ties alphabetically
*
* @access private
* @param arr the array to sort
*/
bubleSort: function (arr) {
// should do the same as this perl statement:
// sort { $trigrams{$b} == $trigrams{$a} ? $a cmp $b : $trigrams{$b} <=> $trigrams{$a} }
// needs to sort by both key and value at once
// using the key to break ties for the value
// converts array into an array of arrays of each key and value
// may be a better way of doing this
var combined = [];
for (var key in arr) {
combined.push([key, arr[key]]);
}
combined = combined.sort(this.sortFunc);
var replacement = {};
var length = combined.length;
for (var i = 0; i < length; i++) {
replacement[combined[i][0]] = combined[i][1];
}
return replacement;
},
/**
* Converts a set of trigrams from frequencies to ranks
*
* Thresholds (cuts off) the list at $this->_threshold
*
* @access protected
* @param arr array of trgram
* @return object ranks of trigrams
*/
arrRank: function (arr) {
// sorts alphabetically first as a standard way of breaking rank ties
arr = this.bubleSort(arr);
var rank = {}, i = 0;
for (var key in arr) {
rank[key] = i++;
// cut off at a standard threshold
if (i >= this.threshold) {
break;
}
}
return rank;
},
/**
* Sort function used by bubble sort
*
* Callback function for usort().
*
* @access private
* @param a first param passed by usort()
* @param b second param passed by usort()
* @return int 1 if $a is greater, -1 if not
*
* @see bubleSort()
*/
sortFunc: function (a, b) {
// each is actually a key/value pair, so that it can compare using both
var aKey = a[0]
, aValue = a[1]
, bKey = b[0]
, bValue = b[1];
// if the values are the same, break ties using the key
if (aValue == bValue) {
return aKey.localeCompare(bKey);
} else {
return aValue > bValue ? -1 : 1;
}
},
unicodeBlockName: function (unicode, blockCount) {
if (unicode <= dbUnicodeBlocks[0][1]) {
return dbUnicodeBlocks[0];
}
var high = blockCount ? blockCount - 1 : dbUnicodeBlocks.length
, low = 1
, mid;
while (low <= high) {
mid = Math.floor((low + high) / 2);
if (unicode < dbUnicodeBlocks[mid][0]) {
high = mid - 1;
} else if (unicode > dbUnicodeBlocks[mid][1]) {
low = mid + 1;
} else {
return dbUnicodeBlocks[mid];
}
}
return -1;
}
};