Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
TensorSpeech
GitHub Repository: TensorSpeech/TensorFlowTTS
Path: blob/master/tensorflow_tts/utils/number_norm.py
1558 views
1
# -*- coding: utf-8 -*-
2
# Copyright (c) 2017 Keith Ito
3
#
4
# Permission is hereby granted, free of charge, to any person obtaining a copy
5
# of this software and associated documentation files (the "Software"), to deal
6
# in the Software without restriction, including without limitation the rights
7
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
# copies of the Software, and to permit persons to whom the Software is
9
# furnished to do so, subject to the following conditions:
10
11
# The above copyright notice and this permission notice shall be included in
12
# all copies or substantial portions of the Software.
13
14
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
# THE SOFTWARE.
21
"""Number norm module."""
22
23
24
import re
25
26
import inflect
27
28
_inflect = inflect.engine()
29
_comma_number_re = re.compile(r"([0-9][0-9\,]+[0-9])")
30
_decimal_number_re = re.compile(r"([0-9]+\.[0-9]+)")
31
_pounds_re = re.compile(r"£([0-9\,]*[0-9]+)")
32
_dollars_re = re.compile(r"\$([0-9\.\,]*[0-9]+)")
33
_ordinal_re = re.compile(r"[0-9]+(st|nd|rd|th)")
34
_number_re = re.compile(r"[0-9]+")
35
36
37
def _remove_commas(m):
38
return m.group(1).replace(",", "")
39
40
41
def _expand_decimal_point(m):
42
return m.group(1).replace(".", " point ")
43
44
45
def _expand_dollars(m):
46
match = m.group(1)
47
parts = match.split(".")
48
if len(parts) > 2:
49
return match + " dollars" # Unexpected format
50
dollars = int(parts[0]) if parts[0] else 0
51
cents = int(parts[1]) if len(parts) > 1 and parts[1] else 0
52
if dollars and cents:
53
dollar_unit = "dollar" if dollars == 1 else "dollars"
54
cent_unit = "cent" if cents == 1 else "cents"
55
return "%s %s, %s %s" % (dollars, dollar_unit, cents, cent_unit)
56
elif dollars:
57
dollar_unit = "dollar" if dollars == 1 else "dollars"
58
return "%s %s" % (dollars, dollar_unit)
59
elif cents:
60
cent_unit = "cent" if cents == 1 else "cents"
61
return "%s %s" % (cents, cent_unit)
62
else:
63
return "zero dollars"
64
65
66
def _expand_ordinal(m):
67
return _inflect.number_to_words(m.group(0))
68
69
70
def _expand_number(m):
71
num = int(m.group(0))
72
if num > 1000 and num < 3000:
73
if num == 2000:
74
return "two thousand"
75
elif num > 2000 and num < 2010:
76
return "two thousand " + _inflect.number_to_words(num % 100)
77
elif num % 100 == 0:
78
return _inflect.number_to_words(num // 100) + " hundred"
79
else:
80
return _inflect.number_to_words(
81
num, andword="", zero="oh", group=2
82
).replace(", ", " ")
83
else:
84
return _inflect.number_to_words(num, andword="")
85
86
87
def normalize_numbers(text):
88
text = re.sub(_comma_number_re, _remove_commas, text)
89
text = re.sub(_pounds_re, r"\1 pounds", text)
90
text = re.sub(_dollars_re, _expand_dollars, text)
91
text = re.sub(_decimal_number_re, _expand_decimal_point, text)
92
text = re.sub(_ordinal_re, _expand_ordinal, text)
93
text = re.sub(_number_re, _expand_number, text)
94
return text
95
96