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 re15 16from .num import num2str17 18# 温度表达式,温度会影响负号的读法19# -3°C 零下三度20RE_TEMPERATURE = re.compile(r"(-?)(\d+(\.\d+)?)(°C|℃|度|摄氏度)")21measure_dict = {22 "cm2": "平方厘米",23 "cm²": "平方厘米",24 "cm3": "立方厘米",25 "cm³": "立方厘米",26 "cm": "厘米",27 "db": "分贝",28 "ds": "毫秒",29 "kg": "千克",30 "km": "千米",31 "m2": "平方米",32 "m²": "平方米",33 "m³": "立方米",34 "m3": "立方米",35 "ml": "毫升",36 "m": "米",37 "mm": "毫米",38 "s": "秒",39}40 41 42def replace_temperature(match) -> str:43 """44 Args:45 match (re.Match)46 Returns:47 str48 """49 sign = match.group(1)50 temperature = match.group(2)51 unit = match.group(3)52 sign: str = "零下" if sign else ""53 temperature: str = num2str(temperature)54 unit: str = "摄氏度" if unit == "摄氏度" else "度"55 result = f"{sign}{temperature}{unit}"56 return result57 58 59def replace_measure(sentence) -> str:60 for q_notation in measure_dict:61 pattern = rf"(?<=\d){q_notation}"62 sentence = re.sub(pattern, measure_dict[q_notation], sentence)63 return sentence64 