forked from svlapin/js-verbal-number-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
226 lines (194 loc) · 4.36 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
'use strict';
module.exports = {
tokenize: tokenize,
convert: convert,
translate: translate
};
var classifiers = [
{
id: 'hundreds',
forms: ['hundreds', 'hundred'],
multiplier: 1e2
},
{
id: 'thousands',
forms: ['thousands', 'thousand', 'k', 'K'],
multiplier: 1e3
},
{
id: 'millions',
forms: ['millions', 'million', 'M', 'mln'],
multiplier: 1e6
},
{
id: 'billions',
forms: ['billions', 'billion', 'G', 'B', 'bln'],
multiplier: 1e9
}
];
/**
* Spltits the string by classifiers
* @param {str} str Input string
* @return {Object} object[classifier from classifiers]
* == number of such units
*/
function tokenize(str) {
var result = {};
var rem = str;
for (var k = classifiers.length - 1; k >= 0; k--) {
var cls = classifiers[k];
var split = _processClassifier(rem, cls);
if (split) {
rem = split[1];
result[cls.id] = split[0];
}
}
if (rem) {
result.units = rem;
}
return result;
}
/**
* Processed the string using one of the classifiers
* @param {String} str Input string
* @param {Object} cls One of classifiers
* @return {Array | Null} Null or split array
*/
function _processClassifier(str, cls) {
for (var i = 0, l = cls.forms.length; i < l; i++) {
var split = str.split(cls.forms[i]);
if (split.length > 1) {
return split.map(function(el) {
return _normalize(el);
});
}
}
return null;
}
/**
* Units anjusments
* @param {Str} str Input string
* @return {Str} Formatted units
*/
function _normalize(str) {
return str.replace(/\sand\s/, ' ').trim();
}
var unitsToNum = {
'one': 1,
'two': 2,
'three': 3,
'four': 4,
'five': 5,
'six': 6,
'seven': 7,
'eight': 8,
'nine': 9
};
/**
* Transpose an object, making its values a keys and vice versa
* @param {Object} obj Input hash
* @return {Object} Transposed object
*/
function invertHash(obj) {
var res = {};
for (var key in obj) {
res[obj[key]] = key;
}
return res;
}
var unitsToString = invertHash(unitsToNum);
var tensToNum = {
'ten': 10,
'eleven': 11,
'twelve': 12,
'thirteen': 13,
'fourteen': 14,
'fifteen': 15,
'sixteen': 16,
'seventeen': 17,
'eighteen': 18,
'nineteen': 19,
'twenty': 20,
'thirty': 30,
'fourty': 40,
'fifty': 50,
'sixty': 60,
'seventy': 70,
'eighty': 80,
'ninety': 90
};
var tensToString = invertHash(tensToNum);
/**
* Convert verbal numeric statement to a number
* @param {Object} obj object returned by 'tokenize'
* @return {Number} Number
*/
function convert(obj) {
var partial = {};
for (var cls in obj) {
var vAmount = obj[cls];
partial[cls] = 0;
// handle numeric values
if (vAmount.match(/^[0-9.]+$/)) {
partial[cls] += parseFloat(vAmount);
continue;
}
// handle hundrers in MSBs
var split = _processClassifier(vAmount, classifiers[0]);
if (split) {
partial[cls] += unitsToNum[split[0]] * classifiers[0].multiplier;
vAmount = split[1];
}
for (var t in tensToNum) {
if (vAmount.indexOf(t) !== -1) {
partial[cls] += tensToNum[t];
vAmount = vAmount.split(t)[1].trim();
break;
}
}
if (!vAmount) continue;
for (var u in unitsToNum) {
if (vAmount.indexOf(u) !== -1) {
partial[cls] += unitsToNum[u];
break;
}
}
}
var res = 0;
for (cls in partial) {
for (var i = classifiers.length - 1; i >= 0; i--) {
if (classifiers[i].id === cls) {
res += partial[cls] * classifiers[i].multiplier;
break;
}
}
}
return res + (partial.units || 0);
}
/**
* Two steps wrapper - tokenize + convert
* @return {String} Converted string
*/
function translate(str) {
return convert(tokenize(str));
}
/**
* Converts number to a string
* @param {Number} num Input number
* @return {String} Output string
*/
function stringify(num) {
console.log('stringifying %d', num)
var str = '';
var rem = num;
for (var i = classifiers.length - 1; i >= 0; i--) {
var cls = classifiers[i];
if (cls.multiplier > rem) continue;
var quotient = Math.floor(rem / cls.multiplier);
rem = rem % cls.multiplier;
str += [stringify(quotient), cls.id].join(' ');
}
str = [str, rem.toString()].join(' ');
console.log('Result: %s', str || num.toString())
return str || num.toString();
}