Path: blob/master/tensorflow_tts/utils/number_norm.py
1558 views
# -*- coding: utf-8 -*-1# Copyright (c) 2017 Keith Ito2#3# Permission is hereby granted, free of charge, to any person obtaining a copy4# of this software and associated documentation files (the "Software"), to deal5# in the Software without restriction, including without limitation the rights6# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell7# copies of the Software, and to permit persons to whom the Software is8# furnished to do so, subject to the following conditions:910# The above copyright notice and this permission notice shall be included in11# all copies or substantial portions of the Software.1213# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR14# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE16# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER17# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,18# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN19# THE SOFTWARE.20"""Number norm module."""212223import re2425import inflect2627_inflect = inflect.engine()28_comma_number_re = re.compile(r"([0-9][0-9\,]+[0-9])")29_decimal_number_re = re.compile(r"([0-9]+\.[0-9]+)")30_pounds_re = re.compile(r"£([0-9\,]*[0-9]+)")31_dollars_re = re.compile(r"\$([0-9\.\,]*[0-9]+)")32_ordinal_re = re.compile(r"[0-9]+(st|nd|rd|th)")33_number_re = re.compile(r"[0-9]+")343536def _remove_commas(m):37return m.group(1).replace(",", "")383940def _expand_decimal_point(m):41return m.group(1).replace(".", " point ")424344def _expand_dollars(m):45match = m.group(1)46parts = match.split(".")47if len(parts) > 2:48return match + " dollars" # Unexpected format49dollars = int(parts[0]) if parts[0] else 050cents = int(parts[1]) if len(parts) > 1 and parts[1] else 051if dollars and cents:52dollar_unit = "dollar" if dollars == 1 else "dollars"53cent_unit = "cent" if cents == 1 else "cents"54return "%s %s, %s %s" % (dollars, dollar_unit, cents, cent_unit)55elif dollars:56dollar_unit = "dollar" if dollars == 1 else "dollars"57return "%s %s" % (dollars, dollar_unit)58elif cents:59cent_unit = "cent" if cents == 1 else "cents"60return "%s %s" % (cents, cent_unit)61else:62return "zero dollars"636465def _expand_ordinal(m):66return _inflect.number_to_words(m.group(0))676869def _expand_number(m):70num = int(m.group(0))71if num > 1000 and num < 3000:72if num == 2000:73return "two thousand"74elif num > 2000 and num < 2010:75return "two thousand " + _inflect.number_to_words(num % 100)76elif num % 100 == 0:77return _inflect.number_to_words(num // 100) + " hundred"78else:79return _inflect.number_to_words(80num, andword="", zero="oh", group=281).replace(", ", " ")82else:83return _inflect.number_to_words(num, andword="")848586def normalize_numbers(text):87text = re.sub(_comma_number_re, _remove_commas, text)88text = re.sub(_pounds_re, r"\1 pounds", text)89text = re.sub(_dollars_re, _expand_dollars, text)90text = re.sub(_decimal_number_re, _expand_decimal_point, text)91text = re.sub(_ordinal_re, _expand_ordinal, text)92text = re.sub(_number_re, _expand_number, text)93return text949596