numbers.py 4.12 KB
Newer Older
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
# *****************************************************************************
# Copyright (c) 2017 Keith Ito
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# *****************************************************************************
"""
Modified from https://github.com/keithito/tacotron
"""

import re

29
30
import inflect

31
32

_inflect = inflect.engine()
33
34
35
36
37
38
_comma_number_re = re.compile(r"([0-9][0-9\,]+[0-9])")
_pounds_re = re.compile(r"£([0-9\,]*[0-9]+)")
_dollars_re = re.compile(r"\$([0-9\.\,]*[0-9]+)")
_decimal_number_re = re.compile(r"([0-9]+\.[0-9]+)")
_ordinal_re = re.compile(r"[0-9]+(st|nd|rd|th)")
_number_re = re.compile(r"[0-9]+")
39
40


41
def _remove_commas(text: str) -> str:
42
    return re.sub(_comma_number_re, lambda m: m.group(1).replace(",", ""), text)
43
44


45
def _expand_pounds(text: str) -> str:
46
    return re.sub(_pounds_re, r"\1 pounds", text)
47
48


49
50
def _expand_dollars_repl_fn(m):
    """The replacement function for expanding dollars."""
51
    match = m.group(1)
52
    parts = match.split(".")
53
    if len(parts) > 2:
54
        return match + " dollars"  # Unexpected format
55
    dollars = int(parts[0]) if parts[0] else 0
56
57
58
59
60
61
62
63
    if len(parts) > 1 and parts[1]:
        if len(parts[1]) == 1:
            # handle the case where we have one digit after the decimal point
            cents = int(parts[1]) * 10
        else:
            cents = int(parts[1])
    else:
        cents = 0
64
    if dollars and cents:
65
66
67
        dollar_unit = "dollar" if dollars == 1 else "dollars"
        cent_unit = "cent" if cents == 1 else "cents"
        return "%s %s, %s %s" % (dollars, dollar_unit, cents, cent_unit)
68
    elif dollars:
69
70
        dollar_unit = "dollar" if dollars == 1 else "dollars"
        return "%s %s" % (dollars, dollar_unit)
71
    elif cents:
72
73
        cent_unit = "cent" if cents == 1 else "cents"
        return "%s %s" % (cents, cent_unit)
74
    else:
75
        return "zero dollars"
76
77


78
79
80
def _expand_dollars(text: str) -> str:
    return re.sub(_dollars_re, _expand_dollars_repl_fn, text)

81

82
def _expand_decimal_point(text: str) -> str:
83
    return re.sub(_decimal_number_re, lambda m: m.group(1).replace(".", " point "), text)
84

85
86
87
88
89
90
91

def _expand_ordinal(text: str) -> str:
    return re.sub(_ordinal_re, lambda m: _inflect.number_to_words(m.group(0)), text)


def _expand_number_repl_fn(m):
    """The replacement function for expanding number."""
92
93
94
    num = int(m.group(0))
    if num > 1000 and num < 3000:
        if num == 2000:
95
            return "two thousand"
96
        elif num > 2000 and num < 2010:
97
            return "two thousand " + _inflect.number_to_words(num % 100)
98
        elif num % 100 == 0:
99
            return _inflect.number_to_words(num // 100) + " hundred"
100
        else:
101
            return _inflect.number_to_words(num, andword="", zero="oh", group=2).replace(", ", " ")
102
    else:
103
        return _inflect.number_to_words(num, andword="")
104
105


106
107
108
109
def _expand_number(text: str) -> str:
    return re.sub(_number_re, _expand_number_repl_fn, text)


110
def normalize_numbers(text: str) -> str:
111
112
113
114
115
116
    text = _remove_commas(text)
    text = _expand_pounds(text)
    text = _expand_dollars(text)
    text = _expand_decimal_point(text)
    text = _expand_ordinal(text)
    text = _expand_number(text)
117
    return text