-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnumbers.py
184 lines (153 loc) · 6.18 KB
/
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
# Provides parsing of numbers to text
"""
This module provides parsing of numeric types in English to text.
Modified from https://github.com/keithito/tacotron
"""
import inflect
import re
_magnitudes = ["trillion", "billion", "million", "thousand", "hundred", "m", "b", "t"]
_magnitudes_key = {"m": "million", "b": "billion", "t": "trillion"}
_measurements = "(f|c|k|d|m|km|ft)"
_measurements_key = {
"f": "fahrenheit",
"c": "celsius",
"k": "thousand",
"m": "meters",
"km": "kilometers",
"ft": "feet",
}
_currency_key = {"$": "dollar", "£": "pound", "€": "euro", "₩": "won"}
_inflect = inflect.engine()
_comma_number_re = re.compile(r"([0-9][0-9,]+[0-9])")
_decimal_number_re = re.compile(r"([0-9]+\.[0-9]+)")
_currency_re = re.compile(
r"([$€£₩])([0-9.,]*[0-9]+)(?:[ ]?({})(?=[^a-zA-Z]|$))?".format(
"|".join(_magnitudes)
),
re.IGNORECASE,
)
# _measurement_re = re.compile(r'([0-9.,]*[0-9]+(\s)?{}\b)'.format(_measurements), re.IGNORECASE)
_measurement_re = re.compile(
r"(?<!\{)" + r"([\d.,]*\d+(\s)?{}\b)".format(_measurements) + r"(?![\w\s]*[}])",
re.IGNORECASE,
)
_ordinal_re = re.compile(r"[0-9]+(st|nd|rd|th)")
_range_re = re.compile(r"(?<=[0-9])+(-)(?=[0-9])+.*?")
_roman_re = re.compile(
r"\b(?=[MDCLXVI]+\b)M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{2,3})\b"
) # avoid I
_multiply_re = re.compile(r"(\b[0-9]+)(x)([0-9]+)")
# _number_re = re.compile(r"[0-9]+'s|[0-9]+s|[0-9]+")
_number_re = re.compile(r"[0-9]+'s|[0-9]+s|(?<!\{)[0-9]+(?![\w\s]*[}])")
def _remove_commas(m):
return m.group(1).replace(",", "")
def _expand_decimal_point(m):
return m.group(1).replace(".", " point ")
def _expand_currency(m):
currency = _currency_key[m.group(1)]
quantity = m.group(2)
magnitude = m.group(3)
# remove commas from quantity to be able to convert to numerical
quantity = quantity.replace(",", "")
# check for million, billion, etc...
if magnitude is not None and magnitude.lower() in _magnitudes:
if len(magnitude) == 1:
magnitude = _magnitudes_key[magnitude.lower()]
return "{} {} {}".format(_expand_hundreds(quantity), magnitude, currency + "s")
parts = quantity.split(".")
if len(parts) > 2:
return quantity + " " + currency + "s" # Unexpected format
dollars = int(parts[0]) if parts[0] else 0
cents = int(parts[1]) if len(parts) > 1 and parts[1] else 0
if dollars and cents:
dollar_unit = currency if dollars == 1 else currency + "s"
cent_unit = "cent" if cents == 1 else "cents"
return "{} {}, {} {}".format(
_expand_hundreds(dollars),
dollar_unit,
_inflect.number_to_words(cents),
cent_unit,
)
elif dollars:
dollar_unit = currency if dollars == 1 else currency + "s"
return "{} {}".format(_expand_hundreds(dollars), dollar_unit)
elif cents:
cent_unit = "cent" if cents == 1 else "cents"
return "{} {}".format(_inflect.number_to_words(cents), cent_unit)
else:
return "zero" + " " + currency + "s"
def _expand_hundreds(text):
number = float(text)
if 1000 < number < 10000 and (number % 100 == 0) and (number % 1000 != 0):
return _inflect.number_to_words(int(number / 100)) + " hundred"
else:
return _inflect.number_to_words(text)
def _expand_ordinal(m):
return _inflect.number_to_words(m.group(0))
def _expand_measurement(m):
_, number, measurement = re.split(r"(\d+(?:\.\d+)?)", m.group(0))
number = _inflect.number_to_words(number)
measurement = "".join(measurement.split())
measurement = _measurements_key.get(measurement.lower(), measurement)
# if measurement is plural, and number is singular, remove the 's'
if number == "one" and str.endswith(measurement, "s"):
# Remove the 's' from the end of the measurement
measurement = measurement[:-1]
return "{} {}".format(number, measurement)
def _expand_range(m):
return " to "
def _expand_multiply(m):
left = m.group(1)
right = m.group(3)
return "{} by {}".format(left, right)
def _expand_roman(m):
# from https://stackoverflow.com/questions/19308177/converting-roman-numerals-to-integers-in-python
roman_numerals = {"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000}
result = 0
num = m.group(0)
for i, c in enumerate(num):
if (i + 1) == len(num) or roman_numerals[c] >= roman_numerals[num[i + 1]]:
result += roman_numerals[c]
else:
result -= roman_numerals[c]
return str(result)
def _expand_number(m):
_, number, suffix = re.split(r"(\d+(?:'?\d+)?)", m.group(0))
number = int(number)
if 1000 < number < 10000 and (number % 100 == 0) and (number % 1000 != 0):
text = _inflect.number_to_words(number // 100) + " hundred"
elif 1000 < number < 3000:
if number == 2000:
text = "two thousand"
elif 2000 < number < 2010:
text = "two thousand " + _inflect.number_to_words(number % 100)
elif number % 100 == 0:
text = _inflect.number_to_words(number // 100) + " hundred"
else:
number = _inflect.number_to_words(
number, andword="", zero="oh", group=2
).replace(", ", " ")
number = re.sub(r"-", " ", number)
text = number
else:
number = _inflect.number_to_words(number, andword="and")
number = re.sub(r"-", " ", number)
number = re.sub(r",", "", number)
text = number
if suffix in ("'s", "s"):
if text[-1] == "y":
text = text[:-1] + "ies"
else:
text = text + suffix
return text
def normalize_numbers(text: str) -> str:
text = re.sub(_comma_number_re, _remove_commas, text)
text = re.sub(_currency_re, _expand_currency, text)
text = re.sub(_decimal_number_re, _expand_decimal_point, text)
text = re.sub(_ordinal_re, _expand_ordinal, text)
# text = re.sub(_range_re, _expand_range, text)
text = re.sub(_measurement_re, _expand_measurement, text)
text = re.sub(_roman_re, _expand_roman, text)
text = re.sub(_multiply_re, _expand_multiply, text)
text = re.sub(_number_re, _expand_number, text)
return text