-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtxt2num.py
More file actions
67 lines (53 loc) · 2.13 KB
/
txt2num.py
File metadata and controls
67 lines (53 loc) · 2.13 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
def txt2num(textnum, numwords={}):
if not numwords:
units = [
"zero", "one", "two", "three", "four", "five", "six", "seven", "eight",
"nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
"sixteen", "seventeen", "eighteen", "nineteen"
]
tens = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy",
"eighty", "ninety"]
scales = ["hundred", "thousand", "million", "billion", "trillion"]
for idx, word in enumerate(units): numwords[word] = (1, idx)
for idx, word in enumerate(tens): numwords[word] = (1, idx * 10)
for idx, word in enumerate(scales): numwords[word] = (10 ** (idx * 3 or 2), 0)
current = result = 0
for word in textnum.split():
if word not in numwords:
raise Exception("Illegal word: " + word)
scale, increment = numwords[word]
current = current * scale + increment
if scale > 100:
result += current
current = 0
return str(result + current)
def num_fix(str):
words= str.split()
output=""
lit_string = ""
A = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight",
"nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
"sixteen", "seventeen", "eighteen", "nineteen",]
B=["twenty", "thirty", "forty","fifty", "sixty", "seventy", "eighty", "ninety"]
C=["hundred", "thousand","million", "billion", "trillion"]
D = A + B + C
for w in range(len(words)):
if words[w] in D:
lit_string += words[w] + " "
if w == len(words) - 1:
output+= txt2num(lit_string) + " "
else:
continue
if words[w] not in D:
if lit_string != "":
if len(words) == w+1:
output += txt2num(lit_string) + " " + words[w]
else:
output += txt2num(lit_string) + " " + words[w] + " "
else:
if len(words) == w+1:
output += words[w]
else:
output += words[w] + " "
lit_string = ""
return output