forked from jthlim/jeff-numbers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjeff-numbers.py
331 lines (280 loc) · 10.4 KB
/
jeff-numbers.py
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
# Jeff's Numbers dictionary for Plover.
#
# See README.md for usage details.
import re
LONGEST_KEY = 20
DIGITS = '0123456789'
ENDING_DIGITS_MATCHER = re.compile(r'\d+$')
ENDING_NUMBER_MATCHER = re.compile(r'.?\d[\d,.]*$')
AM_SUFFIX = ' a.m.'
PM_SUFFIX = ' p.m.'
def lookup(key):
result = ''
needs_space = False
next_error = False
use_glue = True
for stroke in key:
if next_error:
raise KeyError
if not any(c in stroke for c in DIGITS) and not '#' in stroke:
raise KeyError
if needs_space:
result += ' '
needs_space = False
stroke_digits = digits(stroke)
result += stroke_digits
control = ''.join(c for c in stroke if c not in '0123456789#-EU')
if stroke_digits.endswith(','):
control = control.replace('*S', '')
if 'KWR' in control:
control = control.replace('KWR', '')
match = ENDING_NUMBER_MATCHER.search(result)
if not match:
raise KeyError
value = int(''.join(c for c in match.group(0) if c in DIGITS))
if value < 0 or value > 3999:
raise KeyError
result = '%d{}' % (value + 1900)
use_glue = False
next_error = True
elif 'RBG' in control:
control = control.replace('RBG', '')
match = ENDING_NUMBER_MATCHER.search(result)
if not match:
raise KeyError
value = int(''.join(c for c in match.group(0) if c in DIGITS))
if value < 0 or value > 3999:
raise KeyError
result = '%d{}' % (value + 2000)
use_glue = False
next_error = True
elif 'RB' in control:
control = control.replace('RB', '')
if len(result) >= 1 and result[-1] == '.':
result = result + r'0 {*($c)}'
else:
result = result + r' {*($c)}'
use_glue = False
needs_space = True
next_error = True
elif 'WR' in control:
control = control.replace('WR', '')
if len(result) >= 1 and result[-1] == '.':
result = result + r'0 {*($c)}'
else:
result = result + r' {*($c)}'
use_glue = False
needs_space = True
next_error = True
elif 'KR' in control:
control = control.replace('KR', '')
result = ENDING_NUMBER_MATCHER.sub(r'\g<0>%', result)
needs_space = True
next_error = True
elif 'RG' in control:
control = control.replace('RG', '')
result = ENDING_NUMBER_MATCHER.sub(r'\g<0>%', result)
needs_space = True
next_error = True
elif 'DZ' in control:
result = result + r'00 {*($c)}'
needs_space = True
next_error = True
use_glue = False
elif 'K' in control or 'BG' in control:
if 'K' not in control:
result += ':00'
elif 'BG' in control:
result += ':45'
elif 'G' in control:
result += ':15'
elif 'B' in control:
result += ':30'
else:
result += ':00'
if 'S' in control:
result += PM_SUFFIX if '*' in control else AM_SUFFIX
needs_space = True
next_error = True
use_glue = False
control = ''.join(c for c in control if c not in 'KBGS')
elif 'G' in control:
match = ENDING_NUMBER_MATCHER.search(result)
if not match:
raise KeyError
words = toWords(''.join(c for c in match.group(0) if c in DIGITS))
if 'W' in control:
if words.endswith('ty'):
words = words[:-1] + 'ieth'
elif words.endswith('one'):
words = words[:-3] + 'first'
elif words.endswith('two'):
words = words[:-3] + 'second'
elif words.endswith('three'):
words = words[:-5] + 'third'
elif words.endswith('ve'): # fifth and twelfth
words = words[:-2] + 'fth'
elif words.endswith('eight'):
words += 'h'
elif words.endswith('nine'):
words = words[:-1] + 'th'
else:
words += 'th'
result = match.expand(words)
needs_space = True
next_error = True
use_glue = False
control = ''.join(c for c in control if c not in 'WG')
elif 'W' in control or 'B' in control:
match = ENDING_DIGITS_MATCHER.search(result)
if not match:
raise KeyError
needs_space = True
next_error = True
use_glue = False
control = ''.join(c for c in control if c not in 'WB')
if len(result) >= 1 and result[-1] == '1':
result += 'th' if len(
result) >= 2 and result[-2] == '1' else 'st'
elif len(result) >= 1 and result[-1] == '2':
result += 'th' if len(
result) >= 2 and result[-2] == '1' else 'nd'
elif len(result) >= 1 and result[-1] == '3':
result += 'th' if len(
result) >= 2 and result[-2] == '1' else 'rd'
else:
result += 'th'
elif 'R' in control:
match = ENDING_NUMBER_MATCHER.search(result)
if not match:
raise KeyError
value = int(''.join(c for c in match.group(0) if c in DIGITS))
if value < 0 or value > 3999:
raise KeyError
roman = toRoman(value)
if '*' in control:
roman = roman.lower()
result = match.expand(roman)
control = control.replace('R', '')
needs_space = True
next_error = True
use_glue = False
control = ''.join(c for c in control if c not in 'DZ*')
if control != '':
raise KeyError
if use_glue:
result = "{&%s}" % result
return result
def digits(val):
result = ''.join(c for c in val if c in DIGITS)
control = ''.join(c for c in val if c not in DIGITS)
if 'E' in control or 'U' in control:
control = ''.join(c for c in control if c not in 'EU')
result = result[::-1]
if not 'DZ' in control:
if 'Z' in control:
result += ',000' if '*' in control else '00'
control = ''.join(c for c in control if c not in '*Z')
if 'D' in control:
if len(result) > 0:
result += result[-1]
control = control.replace('D', '')
if control == '*S' or control == '#*S':
result += ','
elif '*' in control and (not any(c in 'RSZ' for c in control) or control == "*RB" or control == "WR*"):
result += '.'
return result
ROMAN_VALUES = [
1000, 900, 500, 400,
100, 90, 50, 40,
10, 9, 5, 4,
1
]
ROMAN_SYMBOLS = [
'M', 'CM', 'D', 'CD',
'C', 'XC', 'L', 'XL',
'X', 'IX', 'V', 'IV',
'I'
]
def toRoman(num):
result = ''
i = 0
while num > 0:
for _ in range(num // ROMAN_VALUES[i]):
result += ROMAN_SYMBOLS[i]
num -= ROMAN_VALUES[i]
i += 1
return result
# ---- Adapted from https://programsolve.com/python-to-convert-numbers-to-words-with-source-code/
ONE_DIGIT_WORDS = {
'0': ["zero"],
'1': ["one"],
'2': ["two", "twen"],
'3': ["three", "thir"],
'4': ["four", "for"],
'5': ["five", "fif"],
'6': ["six"],
'7': ["seven"],
'8': ["eight"],
'9': ["nine"],
}
TWO_DIGIT_WORDS = ["ten", "eleven", "twelve"]
HUNDRED = "hundred"
LARGE_SUM_WORDS = ["thousand", "million", "billion", "trillion", "quadrillion",
"quintillion", "sextillion", "septillion", "octillion", "nonillion"]
def toWords(n):
if all(c == '0' for c in n):
return ONE_DIGIT_WORDS['0'][0]
word = []
if len(n) % 3 != 0 and len(n) > 3:
n = n.zfill(3 * (((len(n)-1) // 3) + 1))
sum_list = [n[i:i + 3] for i in range(0, len(n), 3)]
skip = False
for i, num in enumerate(sum_list):
if num != '000':
skip = False
for _ in range(len(num)):
num = num.lstrip('0')
if len(num) == 1:
if len(word) > 0:
if HUNDRED in word[-1] or i == len(sum_list) - 1:
word.append("and")
elif word[-1] in LARGE_SUM_WORDS:
word[-1] = word[-1] + ','
word.append(ONE_DIGIT_WORDS[num][0])
num = num[1:]
break
if len(num) == 2:
if num[0] != '0':
if len(word) > 0:
if HUNDRED in word[-1] or i == len(sum_list) - 1:
word.append("and")
elif word[-1] in LARGE_SUM_WORDS:
word[-1] = word[-1] + ','
if num.startswith('1'):
if int(num[1]) in range(3):
word.append(TWO_DIGIT_WORDS[int(num[1])])
else:
number = ONE_DIGIT_WORDS[num[1]][1 if int(
num[1]) in range(3, 6, 2) else 0]
word.append(
number + ("teen" if not number[-1] == 't' else "een"))
else:
word.append(ONE_DIGIT_WORDS[num[0]][1 if int(num[0]) in range(2, 6) else 0] + (
"ty" if num[0] != '8' else 'y') + ('-' + ONE_DIGIT_WORDS[num[1]][0] if num[1] != '0' else ""))
break
else:
num = num[1:]
continue
if len(num) == 3:
if num[0] != '0':
if len(word) > 0:
word[-1] = word[-1] + ','
word.append(ONE_DIGIT_WORDS[num[0]][0] + " " + HUNDRED)
if num[1:] == '00':
break
num = num[1:]
if len(sum_list[i:]) > 1 and not skip:
word.append(LARGE_SUM_WORDS[len(sum_list[i:]) - 2])
skip = True
return " ".join(map(str.strip, word))