1
0
mirror of https://github.com/janvarev/Irene-Voice-Assistant.git synced 2025-12-02 22:58:35 +02:00
Files
Irene-Voice-Assistant/utils/all_num_to_text.py

39 lines
1.3 KiB
Python
Raw Normal View History

# MIT License
# Janvarev Vladislav
#
# library for translate all digits in text to pronounce
import re
#from utils.num_to_text_ru import num2text
from lingua_franca.format import pronounce_number
def load_language(lang:str):
import lingua_franca
lingua_franca.load_language(lang)
def convert_one_num_float(match_obj):
if match_obj.group() is not None:
text = str(match_obj.group())
return pronounce_number(float(match_obj.group()))
def convert_diapazon(match_obj):
if match_obj.group() is not None:
text = str(match_obj.group())
text = text.replace("-"," тире ")
return all_num_to_text(text)
def all_num_to_text(text:str) -> str:
text = re.sub(r'[\d]*[.][\d]+-[\d]*[.][\d]+', convert_diapazon, text)
text = re.sub(r'-[\d]*[.][\d]+', convert_one_num_float, text)
text = re.sub(r'[\d]*[.][\d]+', convert_one_num_float, text)
text = re.sub(r'[\d]-[\d]+', convert_diapazon, text)
text = re.sub(r'-[\d]+', convert_one_num_float, text)
text = re.sub(r'[\d]+', convert_one_num_float, text)
text = text.replace("%", " процентов")
return text
if __name__ == "__main__":
load_language("ru")
print(all_num_to_text("Ба ва 120.1-120.8, Да -30.1, Ка 44.05, Га 225. Рынок -10%. Тест"))