lenML/ChatTTS-Forge
301
1# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.14import re15from typing import List16 17from .char_convert import tranditional_to_simplified18from .chronology import (19 RE_DATE,20 RE_DATE2,21 RE_TIME,22 RE_TIME_RANGE,23 replace_date,24 replace_date2,25 replace_time,26)27from .constants import F2H_ASCII_LETTERS, F2H_DIGITS, F2H_SPACE28from .num import (29 RE_DECIMAL_NUM,30 RE_DEFAULT_NUM,31 RE_FRAC,32 RE_INTEGER,33 RE_NUMBER,34 RE_PERCENTAGE,35 RE_POSITIVE_QUANTIFIERS,36 RE_RANGE,37 replace_default_num,38 replace_frac,39 replace_negative_num,40 replace_number,41 replace_percentage,42 replace_positive_quantifier,43 replace_range,44)45from .phonecode import (46 RE_MOBILE_PHONE,47 RE_NATIONAL_UNIFORM_NUMBER,48 RE_TELEPHONE,49 replace_mobile,50 replace_phone,51)52from .quantifier import RE_TEMPERATURE, replace_measure, replace_temperature53 54 55class TextNormalizer:56 def __init__(self):57 self.SENTENCE_SPLITOR = re.compile(r"([:、,;。?!,;?!][”’]?)")58 59 def _split(self, text: str, lang="zh") -> List[str]:60 """Split long text into sentences with sentence-splitting punctuations.61 Args:62 text (str): The input text.63 Returns:64 List[str]: Sentences.65 """66 # Only for pure Chinese here67 if lang == "zh":68 text = text.replace(" ", "")69 # 过滤掉特殊字符70 text = re.sub(r"[——《》【】<=>{}()()#&@“”^_|…\\]", "", text)71 text = self.SENTENCE_SPLITOR.sub(r"\1\n", text)72 text = text.strip()73 sentences = [sentence.strip() for sentence in re.split(r"\n+", text)]74 return sentences75 76 def _post_replace(self, sentence: str) -> str:77 # sentence = sentence.replace('/', '每')78 # sentence = sentence.replace('~', '至')79 # sentence = sentence.replace('~', '至')80 sentence = sentence.replace("①", "一")81 sentence = sentence.replace("②", "二")82 sentence = sentence.replace("③", "三")83 sentence = sentence.replace("④", "四")84 sentence = sentence.replace("⑤", "五")85 sentence = sentence.replace("⑥", "六")86 sentence = sentence.replace("⑦", "七")87 sentence = sentence.replace("⑧", "八")88 sentence = sentence.replace("⑨", "九")89 sentence = sentence.replace("⑩", "十")90 sentence = sentence.replace("α", "阿尔法")91 sentence = sentence.replace("β", "贝塔")92 sentence = sentence.replace("γ", "伽玛").replace("Γ", "伽玛")93 sentence = sentence.replace("δ", "德尔塔").replace("Δ", "德尔塔")94 sentence = sentence.replace("ε", "艾普西龙")95 sentence = sentence.replace("ζ", "捷塔")96 sentence = sentence.replace("η", "依塔")97 sentence = sentence.replace("θ", "西塔").replace("Θ", "西塔")98 sentence = sentence.replace("ι", "艾欧塔")99 sentence = sentence.replace("κ", "喀帕")100 sentence = sentence.replace("λ", "拉姆达").replace("Λ", "拉姆达")101 sentence = sentence.replace("μ", "缪")102 sentence = sentence.replace("ν", "拗")103 sentence = sentence.replace("ξ", "克西").replace("Ξ", "克西")104 sentence = sentence.replace("ο", "欧米克伦")105 sentence = sentence.replace("π", "派").replace("Π", "派")106 sentence = sentence.replace("ρ", "肉")107 sentence = (108 sentence.replace("ς", "西格玛")109 .replace("Σ", "西格玛")110 .replace("σ", "西格玛")111 )112 sentence = sentence.replace("τ", "套")113 sentence = sentence.replace("υ", "宇普西龙")114 sentence = sentence.replace("φ", "服艾").replace("Φ", "服艾")115 sentence = sentence.replace("χ", "器")116 sentence = sentence.replace("ψ", "普赛").replace("Ψ", "普赛")117 sentence = sentence.replace("ω", "欧米伽").replace("Ω", "欧米伽")118 # re filter special characters, have one more character "-" than line 68119 # sentence = re.sub(r'[-——《》【】<=>{}()()#&@“”^_|…\\]', '', sentence)120 return sentence121 122 def normalize_sentence(self, sentence: str) -> str:123 # basic character conversions124 sentence = tranditional_to_simplified(sentence)125 sentence = (126 sentence.translate(F2H_ASCII_LETTERS)127 .translate(F2H_DIGITS)128 .translate(F2H_SPACE)129 )130 131 # number related NSW verbalization132 sentence = RE_DATE.sub(replace_date, sentence)133 sentence = RE_DATE2.sub(replace_date2, sentence)134 135 # range first136 sentence = RE_TIME_RANGE.sub(replace_time, sentence)137 sentence = RE_TIME.sub(replace_time, sentence)138 139 sentence = RE_TEMPERATURE.sub(replace_temperature, sentence)140 sentence = replace_measure(sentence)141 sentence = RE_FRAC.sub(replace_frac, sentence)142 sentence = RE_PERCENTAGE.sub(replace_percentage, sentence)143 sentence = RE_MOBILE_PHONE.sub(replace_mobile, sentence)144 145 sentence = RE_TELEPHONE.sub(replace_phone, sentence)146 sentence = RE_NATIONAL_UNIFORM_NUMBER.sub(replace_phone, sentence)147 148 sentence = RE_RANGE.sub(replace_range, sentence)149 sentence = RE_INTEGER.sub(replace_negative_num, sentence)150 sentence = RE_DECIMAL_NUM.sub(replace_number, sentence)151 sentence = RE_POSITIVE_QUANTIFIERS.sub(replace_positive_quantifier, sentence)152 sentence = RE_DEFAULT_NUM.sub(replace_default_num, sentence)153 sentence = RE_NUMBER.sub(replace_number, sentence)154 sentence = self._post_replace(sentence)155 156 return sentence157 158 def normalize(self, text: str, lang="") -> List[str]:159 sentences = self._split(text, lang)160 sentences = [self.normalize_sentence(sent) for sent in sentences]161 return sentences162 