-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5.js
More file actions
496 lines (423 loc) · 15.3 KB
/
Copy path5.js
File metadata and controls
496 lines (423 loc) · 15.3 KB
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
// wink-helpers
// Functions for cross validation, shuffle, cartesian product and more
//
// Copyright (C) 2017-18 GRAYPE Systems Private Limited
//
// This file is part of “wink-helpers”.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
var helpers = Object.create( null );
// ### Private Functions
// #### Product Reducer (Callback)
// Callback function used by `reduce` inside the `product()` function.
// Follows the standard guidelines of `reduce()` callback function.
var productReducer = function ( prev, curr ) {
var c,
cmax = curr.length;
var p,
pmax = prev.length;
var result = [];
for ( p = 0; p < pmax; p += 1 ) {
for ( c = 0; c < cmax; c += 1 ) {
result.push( prev[ p ].concat( curr[ c ] ) );
}
}
return ( result );
}; // productReducer()
// ### Public Function
// ### Array Helpers
helpers.array = Object.create( null);
// #### is Array
// Tests if argument `v` is a JS array; returns `true` if it is, otherwise returns `false`.
helpers.array.isArray = function ( v ) {
return ( ( v !== undefined ) && ( v !== null ) && ( Object.prototype.toString.call( v ) === '[object Array]' ) );
}; // isArray()
// #### sorting helpers
// Set of helpers to sort either numbers or strings. For key/value pairs,
// the format for each element must be `[ key, value ]`.
// Sort helper to sort an array in ascending order.
helpers.array.ascending = function ( a, b ) {
return ( a > b ) ? 1 :
( a === b ) ? 0 : -1;
}; // ascending()
// Sort helper to sort an array in descending order.
helpers.array.descending = function ( a, b ) {
return ( b > a ) ? 1 :
( b === a ) ? 0 : -1;
}; // descending()
// Sort helper to sort an array of `[ key, value ]` in ascending order by **key**.
helpers.array.ascendingOnKey = function ( a, b ) {
return ( a[ 0 ] > b[ 0 ] ) ? 1 :
( a[ 0 ] === b[ 0 ] ) ? 0 : -1;
}; // ascendingOnKey()
// Sort helper to sort an array of `[ key, value ]` in descending order by **key**.
helpers.array.descendingOnKey = function ( a, b ) {
return ( b[ 0 ] > a[ 0 ] ) ? 1 :
( b[ 0 ] === a[ 0 ] ) ? 0 : -1;
}; // descendingOnKey()
// Sort helper to sort an array of `[ key, value ]` in ascending order by **value**.
helpers.array.ascendingOnValue = function ( a, b ) {
return ( a[ 1 ] > b[ 1 ] ) ? 1 :
( a[ 1 ] === b[ 1 ] ) ? 0 : -1;
}; // ascendingOnValue()
// Sort helper to sort an array of `[ key, value ]` in descending order by **value**.
helpers.array.descendingOnValue = function ( a, b ) {
return ( b[ 1 ] > a[ 1 ] ) ? 1 :
( b[ 1 ] === a[ 1 ] ) ? 0 : -1;
}; // descendingOnValue()
// The following two functions generate a suitable function for sorting on a single
// key or on a composite keys (max 2 only). Just a remider, the generated function
// does not sort on two keys; instead it will sort on a key composed of the two
// accessors.
// Sorts in ascending order on `accessor1` & `accessor2` (optional).
helpers.array.ascendingOn = function ( accessor1, accessor2 ) {
if ( accessor2 ) {
return ( function ( a, b ) {
return ( a[ accessor1 ][ accessor2 ] > b[ accessor1 ][ accessor2 ] ) ? 1 :
( a[ accessor1 ][ accessor2 ] === b[ accessor1 ][ accessor2 ] ) ? 0 : -1;
} );
}
return ( function ( a, b ) {
return ( a[ accessor1 ] > b[ accessor1 ] ) ? 1 :
( a[ accessor1 ] === b[ accessor1 ] ) ? 0 : -1;
} );
}; // ascendingOn()
// Sorts in descending order on `accessor1` & `accessor2` (optional).
helpers.array.descendingOn = function ( accessor1, accessor2 ) {
if ( accessor2 ) {
return ( function ( a, b ) {
return ( b[ accessor1 ][ accessor2 ] > a[ accessor1 ][ accessor2 ] ) ? 1 :
( b[ accessor1 ][ accessor2 ] === a[ accessor1 ][ accessor2 ] ) ? 0 : -1;
} );
}
return ( function ( a, b ) {
return ( b[ accessor1 ] > a[ accessor1 ] ) ? 1 :
( b[ accessor1 ] === a[ accessor1 ] ) ? 0 : -1;
} );
}; // descendingOn()
// #### pluck
// Plucks specified element from each element of an **array of array**, and
// returns the resultant array. The element is specified by `i` (default `0`) and
// number of elements to pluck are defined by `limit` (default `a.length`).
helpers.array.pluck = function ( a, key, limit ) {
var k, plucked;
k = a.length;
var i = key || 0;
var lim = limit || k;
if ( lim > k ) lim = k;
plucked = new Array( lim );
for ( k = 0; k < lim; k += 1 ) plucked[ k ] = a[ k ][ i ];
return plucked;
}; // pluck()
// #### product
// Finds the Cartesian Product of arrays present inside the array `a`. Therefore
// the array `a` must be an array of 1-dimensional arrays. For example,
// `product( [ [ 9, 8 ], [ 1, 2 ] ] )`
// will produce `[ [ 9, 1 ], [ 9, 2 ], [ 8, 1 ], [ 8, 2 ] ]`.
helpers.array.product = function ( a ) {
return (
a.reduce( productReducer, [ [] ] )
);
};
// #### shuffle
// Randomly shuffles the elements of an array and returns the same.
// Reference: Chapter on Random Numbers/Shuffling in Seminumerical algorithms.
// The Art of Computer Programming Volume II by Donald E Kunth
helpers.array.shuffle = function ( array ) {
var a = array;
var balance = a.length;
var candidate;
var temp;
while ( balance ) {
candidate = Math.floor( Math.random() * balance );
balance -= 1;
temp = a[ balance ];
a[ balance ] = a[ candidate ];
a[ candidate ] = temp;
}
return ( a );
};
// ### Object Helpers
var objectKeys = Object.keys;
var objectCreate = Object.create;
helpers.object = Object.create( null );
// #### is Object
// Tests if argument `v` is a JS object; returns `true` if it is, otherwise returns `false`.
helpers.object.isObject = function ( v ) {
return ( v && ( Object.prototype.toString.call( v ) === '[object Object]' ) ) ? true : false; // eslint-disable-line no-unneeded-ternary
}; // isObject()
// #### keys
// Returns keys of the `obj` in an array.
helpers.object.keys = function ( obj ) {
return ( objectKeys( obj ) );
}; // keys()
// #### size
// Returns the number of keys of the `obj`.
helpers.object.size = function ( obj ) {
return ( ( objectKeys( obj ) ).length );
}; // size()
// #### values
// Returns all values from each key/value pair of the `obj` in an array.
helpers.object.values = function ( obj ) {
var keys = helpers.object.keys( obj );
var length = keys.length;
var values = new Array( length );
for ( var i = 0; i < length; i += 1 ) {
values[ i ] = obj[ keys[ i ] ];
}
return values;
}; // values()
// #### value Freq
// Returns the frequency of each unique value present in the `obj`, where the
// **key** is the *value* and **value** is the *frequency*.
helpers.object.valueFreq = function ( obj ) {
var keys = helpers.object.keys( obj );
var length = keys.length;
var val;
var vf = objectCreate( null );
for ( var i = 0; i < length; i += 1 ) {
val = obj[ keys[ i ] ];
vf[ val ] = 1 + ( vf[ val ] || 0 );
}
return vf;
}; // valueFreq()
// #### table
// Converts the `obj` in to an array of `[ key, value ]` pairs in form of a table.
// Second argument - `f` is optional and it is a function, which is called with
// each `value`.
helpers.object.table = function ( obj, f ) {
var keys = helpers.object.keys( obj );
var length = keys.length;
var pairs = new Array( length );
var ak, av;
for ( var i = 0; i < length; i += 1 ) {
ak = keys[ i ];
av = obj[ ak ];
if ( typeof f === 'function' ) f( av );
pairs[ i ] = [ ak, av ];
}
return pairs;
}; // table()
// ### Validation Helpers
helpers.validate = Object.create( null );
// Create aliases for isObject and isArray.
helpers.validate.isObject = helpers.object.isObject;
helpers.validate.isArray = helpers.array.isArray;
// #### isFiniteInteger
// Validates if `n` is a finite integer.
helpers.validate.isFiniteInteger = function ( n ) {
return (
( typeof n === 'number' ) &&
!isNaN( n ) &&
isFinite( n ) &&
( n === Math.round( n ) )
);
}; // isFiniteInteger()
// #### isFiniteNumber
// Validates if `n` is a valid number.
helpers.validate.isFiniteNumber = function ( n ) {
return (
( typeof n === 'number' ) &&
!isNaN( n ) &&
isFinite( n )
);
}; // isFiniteNumber()
// ### cross validation
/**
*
* Creates an instance of cross validator useful for machine learning tasks.
*
* @param {string[]} classLabels - array containing all the class labels.
* @return {methods} object conatining set of API methods for tasks like evalutaion,
* reset and metrics generation.
*/
helpers.validate.cross = function ( classLabels ) {
// wink's const for unknown predictions!
const unknown = 'unknown';
// To ensure that metrics is not computed prior to evaluation.
var evaluated = false;
// The confusion matrix.
var cm;
var precision;
var recall;
var fmeasure;
// The class labels is assigned to this variable.
var labels;
// The length of `labels` array.
var labelCount;
var labelsObj = Object.create( null );
// Returned!
var methods = Object.create( null );
/**
*
* Resets the current instance for another round of evaluation; the class
* labels defined at instance creation time are not touched.
*
* @return {undefined} nothing!
*/
var reset = function ( ) {
evaluated = false;
cm = Object.create( null );
precision = Object.create( null );
recall = Object.create( null );
fmeasure = Object.create( null );
// Initialize confusion matrix and metrics.
for ( let i = 0; i < labelCount; i += 1 ) {
const row = labels[ i ];
labelsObj[ row ] = true;
cm[ row ] = Object.create( null );
precision[ row ] = 0;
recall[ row ] = 0;
fmeasure[ row ] = 0;
for ( let j = 0; j < labelCount; j += 1 ) {
const col = labels[ j ];
cm[ row ][ col ] = 0;
}
}
}; // reset()
/**
*
* Creates an instance of cross validator useful for machine learning tasks.
*
* @param {string} truth - the actual class label.
* @param {string} guess - the predicted class label.
* @return {boolean} returns true if the evaluation is successful. The evaluation
* may fail if `truth` or `guess` is not in the array `classLabels` provided at
* instance creation time; or if guess is equal to `unknown`.
*/
var evaluate = function ( truth, guess ) {
// If prediction failed then return false!
if ( guess === unknown || !labelsObj[ truth ] || !labelsObj[ guess ] ) return false;
// Update confusion matrix.
if ( guess === truth ) {
cm[ truth ][ guess ] += 1;
} else {
cm[ guess ][ truth ] += 1;
}
evaluated = true;
return true;
}; // evaluate()
/**
*
* It computes a detailed metrics consisting of macro-averaged precision,
* recall and f-measure along with their label-wise values and the confusion
* matrix.
*
* @return {object} object containing macro-averaged `avgPrecision`, `avgRecall`,
* `avgFMeasure` values along with other details such as label-wise values
* and the confusion matrix. A value of `null` is returned if no evaluate()
* has been called before.
*/
var metrics = function ( ) {
if ( !evaluated ) return null;
// Numerators for every label; they are same for precision & recall both.
var n = Object.create( null );
// Only denominators differs for precision & recall
var pd = Object.create( null );
var rd = Object.create( null );
// `row` and `col` of confusion matrix.
var col, row;
var i, j;
// Macro average values for metrics.
var avgPrecision = 0;
var avgRecall = 0;
var avgFMeasure = 0;
// Compute label-wise numerators & denominators!
for ( i = 0; i < labelCount; i += 1 ) {
row = labels[ i ];
for ( j = 0; j < labelCount; j += 1 ) {
col = labels[ j ];
if ( row === col ) {
n[ row ] = cm[ row ][ col ];
}
pd[ row ] = cm[ row ][ col ] + ( pd[ row ] || 0 );
rd[ row ] = cm[ col ][ row ] + ( rd[ row ] || 0 );
}
}
// Ready to compute metrics.
for ( i = 0; i < labelCount; i += 1 ) {
row = labels[ i ];
precision[ row ] = +( n[ row ] / pd[ row ] ).toFixed( 4 );
// NaN can occur if a label has not been encountered.
if ( isNaN( precision[ row ] ) ) precision[ row ] = 0;
recall[ row ] = +( n[ row ] / rd[ row ] ).toFixed( 4 );
if ( isNaN( recall[ row ] ) ) recall[ row ] = 0;
fmeasure[ row ] = +( 2 * precision[ row ] * recall[ row ] / ( precision[ row ] + recall[ row ] ) ).toFixed( 4 );
if ( isNaN( fmeasure[ row ] ) ) fmeasure[ row ] = 0;
}
// Compute thier averages, note they will be macro avegages.
for ( i = 0; i < labelCount; i += 1 ) {
avgPrecision += ( precision[ labels[ i ] ] / labelCount );
avgRecall += ( recall[ labels[ i ] ] / labelCount );
avgFMeasure += ( fmeasure[ labels[ i ] ] / labelCount );
}
// Return metrics.
return (
{
// Macro-averaged metrics.
avgPrecision: +avgPrecision.toFixed( 4 ),
avgRecall: +avgRecall.toFixed( 4 ),
avgFMeasure: +avgFMeasure.toFixed( 4 ),
details: {
// Confusion Matrix.
confusionMatrix: cm,
// Label wise metrics details, from those averages were computed.
precision: precision,
recall: recall,
fmeasure: fmeasure
}
}
);
}; // metrics()
if ( !helpers.validate.isArray( classLabels ) ) {
throw Error( 'cross validate: class labels must be an array.' );
}
if ( classLabels.length < 2 ) {
throw Error( 'cross validate: at least 2 class labels are required.' );
}
labels = classLabels;
labelCount = labels.length;
reset();
methods.reset = reset;
methods.evaluate = evaluate;
methods.metrics = metrics;
return methods;
}; // cross()
// ### Object Helpers
helpers.string = Object.create( null );
// Regex for [diacritical marks](https://en.wikipedia.org/wiki/Combining_Diacritical_Marks) removal.
var rgxDiacritical = /[\u0300-\u036f]/g;
/**
*
* Normalizes the token's value by converting it to lower case and stripping
* the diacritical marks (if any).
*
* @param {string} str — that needs to be normalized.
* @return {string} the normalized value.
* @example
* normalize( 'Nestlé' );
* // -> nestle
*/
helpers.string.normalize = function ( str ) {
return (
str.toLowerCase().normalize( 'NFD' ).replace( rgxDiacritical, '' )
);
}; // normalize()
module.exports = helpers;