-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
748 lines (607 loc) · 17.1 KB
/
index.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
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
const BigNumber = require( "bignumber.js" );
// this needs to be larger than our largest exponent
// currently largest is hundred millinillion, at 3005
BigNumber.config({ EXPONENTIAL_AT: 3006 });
/*
wordToNumber Class
converts human readable word-numbers into (string) digits
*/
function WordToNumber() {
/*
(string) language
current language
*/
this.language = "english";
/*
(array) validate_whitelist
(array) validate_blacklist
regexes to determine whether a string should be parsed
*/
this.validate_whitelist = [];
this.validate_blacklist = [];
/*
(regex) side_char_regex
characters not allowed at the start of a word-number
useful for not matching word-numbers inside words like "attend",
which would otherwise match the ten
*/
this.side_char_regex = /[a-z]/i;
/*
(regex) before_parse_strip_regex
allowed delimiters for word-numbers, used to strip them
leaving only word-numbers and non-allowed characters
currently strips: spaces, periods, commas, dashes, and "and"s
*/
this.before_parse_strip_regex = /(\s+|\.|,|-|\s?and\s?)/ig;
/*
(int) max_delim_length
max characters allowed between words
if this is too high, we can match unrelated word-numbers
that just so happen to fit the normal pattern like
"one hundred pears, and six grean beans", would match as 106, not [ 100, 6 ]
*/
this.max_delim_length = 1;
/*o
(array) languages
array of necessary number [ name => value ] pairs
for single, tens, and place separators
in the "large" category, the value for each key is
the total number of digits, aka the number of zeros plus one
so for exponents, just subtract one
*/
this.languages = {
english: {
single: {
zero: 0,
one: 1,
two: 2,
three: 3,
four: 4,
five: 5,
six: 6,
seven: 7,
eight: 8,
nine: 9,
},
tens: {
ten: 10,
eleven: 11,
twelve: 12,
thirteen: 13,
fourteen: 14,
fifteen: 15,
sixteen: 16,
seventeen: 17,
eighteen: 18,
nineteen: 19,
twenty: 20,
thirty: 30,
forty: 40,
fourty: 40,
fifty: 50,
sixty: 60,
seventy: 70,
eighty: 80,
ninety: 90,
},
large: {
thousand: 4,
million: 7,
billion: 10,
trillion: 13,
quadrillion: 16,
quintillion: 19,
sextillion: 22,
septillion: 25,
octillion: 28,
nonillion: 31,
decillion: 34,
undecillion: 37,
duodecillion: 40,
tredecillion: 43,
quattuordecillion: 46,
quindecillion: 49,
sexdecillion: 52,
septendecillion: 55,
octodecillion: 58,
novemdecillion: 61,
vigintillion: 64,
unvigintillion: 67,
duovigintillion: 70,
tresvigintillion: 73,
quattuorvigintillion: 76,
quinquavigintillion: 79,
sesvigintillion: 82,
septemvigintillion: 85,
octovigintillion: 88,
novemvigintillion: 91,
trigintillion: 94,
untrigintillion: 97,
duotrigintillion: 100,
googol: 101,
trestrigintillion: 103,
quattuortrigintillion: 106,
quinquatrigintillion: 110,
sestrigintillion: 112,
septentrigintillion: 115,
octotrigintillion: 118,
noventrigintillion: 121,
quadragintillion: 124,
quinquagintillion: 154,
sexagintillion: 184,
septuagintillion: 214,
octogintillion: 244,
nonagintillion: 274,
centillion: 304,
uncentillion: 307,
duocentillion: 310,
trescentillion: 313,
decicentillion: 334,
undecicentillion: 337,
viginticentillion: 364,
unviginticentillion: 367,
trigintacentillion: 394,
quadragintacentillion: 424,
quinquagintacentillion: 454,
sexagintacentillion: 484,
septuagintacentillion: 514,
octogintacentillion: 544,
nonagintacentillion: 574,
ducentillion: 604,
trecentillion: 904,
quadringentillion: 1204,
quingentillion: 1504,
sescentillion: 1804,
septingentillion: 2104,
octingentillion: 2404,
nongentillion: 2704,
millinillion: 3004
}
}
};
this.all_word_numbers = Object.keys( this.languages[ this.language ].single ).concat( Object.keys( this.languages[ this.language ].tens ), Object.keys( this.languages[ this.language ].large ) );
}
/*
listLanguages
return array of available languages
*/
WordToNumber.prototype.listLanguages = function() {
return this.languages;
};
/*
setLanguage (string)
changes the default language
returns FALSE if it doesn't exist
*/
WordToNumber.prototype.setLanguage = function( language ) {
if ( ! this.languages[ language ] )
return false;
this.language = language;
return true;
};
/*
getLanguageData
return a language's data
*/
WordToNumber.prototype.getLanguageData = function( language ) {
return this.languages[ language ];
};
/*
updateLanguageData
create or replaces a language's data
*/
WordToNumber.prototype.updateLanguageData = function( language, data ) {
this.languages[ language ] = data;
};
/*
setValidatorWhitelist
Takes a single, or array of regex strings
to test numbers against before parsing.
Only matching values will be included.
This is run before setValidatorWhitelist.
Passing a falsey value will clear thie list.
*/
WordToNumber.prototype.setValidatorWhitelist = function( list ) {
if ( ! list.length )
this.validate_whitelist = [];
else if ( Array.isArray( list ) )
this.validate_whitelist = list;
else
this.validate_whitelist = [ list ];
};
/*
setValidatorBlacklist
Takes a single, or array of regex strings
to test numbers against before parsing.
Matching values will be exluded.
This is run before setValidatorWhitelist.
Passing a falsey value will clear thie list.
*/
WordToNumber.prototype.setValidatorBlacklist = function( list ) {
if ( ! list.length )
this.validate_blacklist = [];
else if ( Array.isArray( list ) )
this.validate_blacklist = list;
else
this.validate_blacklist = [ list ];
};
/*
validate
Validates a string against the setValidatorBlacklist
and then the setValidatorWhitelist to test numberstrings
against before parsing
*/
WordToNumber.prototype.validate = function( string ) {
if ( ! this.validate_blacklist.length )
for ( let i = 0; i < this.validate_blacklist.length; i++ )
if ( string.match( this.validate_blacklist[ i ] ) )
return false;
if ( ! this.validate_whitelist )
return true;
for ( let i = 0; i < this.validate_whitelist.length; i++ ) {
if ( string.match( this.validate_whitelist[ i ] ) )
return true;
}
return true;
};
/*
parseSingle
Attempts to parse a string as a single digit
*/
WordToNumber.prototype.parseSingle = function( text, is_invalid, pre_allow_invalid ) {
if ( pre_allow_invalid )
this.is_first = true;
let singles = this.languages[ this.language ].single;
let list = [];
for ( let word in singles ) {
if ( ! singles.hasOwnProperty( word ) )
continue;
let val = new BigNumber( singles[ word ] );
while ( ( pos = text.indexOf( word ) ) !== -1 ) {
if ( this.is_first ) {
if ( pos !== 0 && ! this.isValidSideChar( text.substring( 0, pos ), "start" ) )
is_invalid = true;
else if ( pos === 0 || pre_allow_invalid )
is_invalid = false;
pre_allow_invalid = true;
this.is_first = false;
}
let break_pre = [];
let parts = this.splitOne( text, word );
let post;
let pre_word;
let post_word;
if ( parts[0] && parts.length > 1 )
pre_word = parts[0];
else
post_word = parts[0];
if ( parts[1] )
post_word = parts[1];
// if we have a pre-word, and it parses, then it is a separate number
if ( pre_word ) {
break_pre = this.parseSingle( pre_word, is_invalid );
if ( break_pre.length )
list = list.concat( break_pre );
text = text.replace( pre_word, "" );
}
if ( post_word ) {
post = this.parseSingle( post_word, is_invalid );
text = text.replace( post_word, "" );
}
if ( ! is_invalid )
list.push( val.toString() );
if ( post && post.length )
list = list.concat( post );
text = text.replace( word, "" );
}
}
return list;
};
/*
splitOne
split the first occurrence of a string within a string
this is different from string.split( needle, 1 ),
because it returns the entire last half of the string,
not just up to the next match
*/
WordToNumber.prototype.splitOne = function( haystack, string ) {
let pos = haystack.indexOf( string );
if ( pos !== -1 ) {
let first_half = haystack.slice( 0, pos );
let last_half = haystack.slice( ( pos + string.length ) );
return [ first_half, last_half ];
}
return [];
};
/*
parseTens
Attempts to parse a string for the tens place
If a tens is found, it will continue and try to match the single place,
otherwise will return false;
*/
let level = 0;
WordToNumber.prototype.parseTens = function( text, is_invalid, pre_allow_invalid ) {
if ( pre_allow_invalid )
this.is_first = true;
let original = ( " " + text ).slice( 1 );
let tens = this.languages[ this.language ].tens;
let list = [];
for ( let word in tens ) {
if ( ! tens.hasOwnProperty( word ) )
continue;
let val = new BigNumber( tens[ word ] );
while ( ( pos = text.indexOf( word ) ) !== -1 ) {
if ( this.is_first ) {
if ( pos !== 0 && ! this.isValidSideChar( text.substring( 0, pos ), "start" ) )
is_invalid = true;
else if ( pos === 0 || pre_allow_invalid )
is_invalid = false;
pre_allow_invalid = true;
this.is_first = false;
}
let break_pre = [];
let parts = this.splitOne( text, word );
let post;
let pre_word;
let post_word;
if ( parts[0] && parts.length > 1 )
pre_word = parts[0];
else
post_word = parts[0];
if ( parts[1] )
post_word = parts[1];
text = text.replace( word, "" );
// if we have a pre-word, and it parses, then it is a separate number
if ( pre_word ) {
break_pre = this.parseTens( pre_word, is_invalid, pre_allow_invalid );
if ( break_pre.list.length )
list = list.concat( break_pre.list );
text = this.splitOne( text, pre_word ).join( "" );
}
let break_post = false
if ( post_word ) {
post = this.parseTens( post_word, is_invalid );
if ( ! this.isValidSideChar( post_word, "end" ) )
break_post = true;
text = this.splitOne( text, post_word ).join( "" );
}
let post_push;
if ( post && post.list && post.list.length && ! break_post ) {
if ( post.list[0] <= 9 && val >= 20 ) {
val = val.plus( post.list[0] );
if ( post.list.length > 1 )
post_push = post.list.splice( 1 );
}
else {
post_push = post.list;
}
}
if ( ! is_invalid )
list.push( val.toString() );
if ( post_push )
list = list.concat( post_push );
}
}
let singles = this.parseSingle( text, is_invalid, pre_allow_invalid );
list = list.concat( singles );
return {
list: list,
text: text,
original: original,
};
};
/*
parseHundreds
Attempts to parse a string for the hundreds
*/
WordToNumber.prototype.parseHundreds = function( text, hundred, is_invalid, pre_allow_invalid ) {
if ( pre_allow_invalid )
this.is_first = true;
let original = ( " " + text ).slice( 1 );
let list = [];
let word = hundred || "hundred";
while ( ( pos = text.indexOf( word ) ) !== -1 ) {
if ( this.is_first ) {
if ( pos !== 0 && ! this.isValidSideChar( text.substring( 0, pos ), "start" ) )
is_invalid = true;
else if ( pos === 0 || pre_allow_invalid )
is_invalid = false;
pre_allow_invalid = true;
this.is_first = false;
}
let val = new BigNumber( 100 );
let break_pre = [];
let parts = this.splitOne( text, word );
let post;
let pre_word;
let post_word;
if ( parts[0] && parts.length > 1 )
pre_word = parts[0];
else
post_word = parts[0];
if ( parts[1] )
post_word = parts[1];
if ( pre_word ) {
break_pre = this.parseHundreds( pre_word, null, is_invalid, pre_allow_invalid ).list;
if ( break_pre.length ) {
if ( pre_allow_invalid )
is_invalid = false;
if ( break_pre.length > 1 ) {
for ( let i = 0; i < ( break_pre.length - 1 ); i++ ) {
list = list.concat( break_pre[ i ] );
}
}
if ( break_pre[ break_pre.length - 1 ] > 9 ) {
list = list.concat( break_pre[ break_pre.length - 1 ] );
}
else {
val = val.times( break_pre[ break_pre.length - 1 ] );
}
}
text = text.replace( pre_word, "" );
}
let break_post = false
if ( post_word ) {
post = this.parseHundreds( post_word, null, is_invalid ).list;
text = text.replace( post_word, "" );
if ( ! this.isValidSideChar( post_word, "end" ) ) {
break_post = true;
}
}
if ( post && post.length && ! break_post ) {
if ( post[0] <= 99 ) {
val = val.plus( post[0] );
}
}
else if ( break_post ) {
post.unshift( "" );
}
if ( ! is_invalid )
list.push( val.toString() );
if ( post && post.length > 1 ) {
post.splice( 0, 1 );
list = list.concat( post );
}
text = text.replace( word, "" );
}
let tens = this.parseTens( text, is_invalid, pre_allow_invalid );
list = list.concat( tens.list );
return {
list: list,
text: text,
original: original,
};
};
/*
isValidSideChar
regex function for testing the characters surrounding the word-number
*/
WordToNumber.prototype.isValidSideChar = function( text, side ) {
let valid = false;
this.all_word_numbers.forEach( ( word ) => {
if ( valid )
return;
let regex = ( side == "start" ) ? new RegExp( word + "$" ) : new RegExp( "^" + word );
if ( regex.test( text ) )
valid = true;
});
if ( ! valid ) {
let char = ( side == "start" ) ? text[ text.length - 1 ] : text[0];
valid = ! this.side_char_regex.test( char );
}
return valid;
};
/*
setSideChars
takes a regex of allowed characters surrounding the word-number
this MUST include *at least* /a-z/i, so that you aren't matching words
like "attend", or "tone", that would otherwise match as 10, and 1
*/
WordToNumber.prototype.setSideChars = function( regex ) {
this.side_char_regex = regex;
};
/*
setExponent
takes a number that signifies the exponent at which scientific notation starts
*/
WordToNumber.prototype.setExponent = function( exponent ) {
BigNumber.config({ EXPONENTIAL_AT: exponent });
};
/*
beforeParseStrip
regex function to strip out allowed delimiters so we don't have to deal with them
* we have to use a hacky reverse here because javascript doesn't have negative lookbehinds
*/
WordToNumber.prototype.beforeParseStrip = function( str ) {
let placeholder = "{WORD_TO_NUMBER_PLACEHOLDER}";
str = str.replace( /thousand/g, placeholder );
str = str.replace( this.before_parse_strip_regex, "" );
str = str.replace( new RegExp( placeholder, "g" ), "thousand" );
return str;
};
WordToNumber.prototype.parseLarge = function( text, is_invalid, pre_allow_invalid ) {
if ( pre_allow_invalid )
this.is_first = true;
let list = [];
let large = this.languages[ this.language ].large;
let large_keys = Object.keys( large ).sort( ( a, b ) => {
return large[ b ] - large[ a ];
});
for ( let i = 0; i < large_keys.length; i++ ) {
let word = large_keys[ i ];
if ( ! large.hasOwnProperty( word ) )
continue;
while ( ( pos = text.indexOf( word ) ) !== -1 ) {
if ( this.is_first ) {
if ( pos !== 0 && ! this.isValidSideChar( text.substring( 0, pos ), "start" ) )
is_invalid = true;
else if ( pos === 0 || pre_allow_invalid )
is_invalid = false;
pre_allow_invalid = true;
this.is_first = false;
}
let val = new BigNumber( 10 ).toPower( ( large[ word ] - 1 ) );
let break_pre = [];
let parts = this.splitOne( text, word );
let post;
let pre_word;
let post_word;
if ( parts[0] && parts.length > 1 )
pre_word = parts[0];
else
post_word = parts[0];
if ( parts[1] )
post_word = parts[1];
if ( pre_word ) {
break_pre = this.parseLarge( pre_word, is_invalid, pre_allow_invalid );
if ( break_pre.length ) {
if ( pre_allow_invalid )
is_invalid = false;
if ( break_pre.length > 1 )
for ( let i = 0; i < ( break_pre.length - 1 ); i++ )
list = list.concat( break_pre[ i ] );
if ( break_pre[ break_pre.length - 1 ] > 999 )
list = list.concat( break_pre[ break_pre.length - 1 ] );
else
val = val.times( break_pre[ break_pre.length - 1 ] );
}
text = text.replace( pre_word, "" );
}
if ( post_word ) {
post = this.parseLarge( post_word, is_invalid );
text = text.replace( post_word, "" );
}
if ( post && post.length ) {
if ( post[0] <= ( val - 1 ) ) {
val = val.plus( post[0] );
}
}
if ( ! is_invalid )
list.push( val.toString() );
if ( post && post.length > 1 ) {
post.splice( 0, 1 );
list = list.concat( post );
}
text = text.replace( word, "" );
}
}
let hundreds = this.parseHundreds( text, null, is_invalid, pre_allow_invalid );
text = text.replace( hundreds.original, "" );
list = list.concat( hundreds.list );
return list;
};
/*
parse
Attempt to parse a string to an as many word-numbers as it can find
or false
*/
WordToNumber.prototype.parse = function( text ) {
if ( ! this.validate( text ) )
return false;
text = text.toLowerCase();
this.is_first = true;
let stripped_text = this.beforeParseStrip( text );
let result = this.parseLarge( stripped_text, false );
return ( ! result || result.length === 0 ) ? false : result;
};
module.exports = WordToNumber;