Spaces:
Running
Running
File size: 18,524 Bytes
79d5798 8283b61 79d5798 8283b61 79d5798 8283b61 79d5798 8283b61 79d5798 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 |
import re
class VietnameseTTSNormalizer:
"""
A text normalizer for Vietnamese Text-to-Speech systems.
Converts numbers, dates, units, and special characters into readable Vietnamese text.
"""
def __init__(self):
self.units = {
'km': 'ki lô mét', 'dm': 'đê xi mét', 'cm': 'xen ti mét',
'mm': 'mi li mét', 'nm': 'na nô mét', 'µm': 'mic rô mét',
'μm': 'mic rô mét', 'm': 'mét',
'kg': 'ki lô gam', 'g': 'gam', 'mg': 'mi li gam',
'km²': 'ki lô mét vuông', 'km2': 'ki lô mét vuông',
'm²': 'mét vuông', 'm2': 'mét vuông',
'cm²': 'xen ti mét vuông', 'cm2': 'xen ti mét vuông',
'mm²': 'mi li mét vuông', 'mm2': 'mi li mét vuông',
'ha': 'héc ta',
'km³': 'ki lô mét khối', 'km3': 'ki lô mét khối',
'm³': 'mét khối', 'm3': 'mét khối',
'cm³': 'xen ti mét khối', 'cm3': 'xen ti mét khối',
'mm³': 'mi li mét khối', 'mm3': 'mi li mét khối',
'l': 'lít', 'dl': 'đê xi lít', 'ml': 'mi li lít', 'hl': 'héc tô lít',
'v': 'vôn', 'kv': 'ki lô vôn', 'mv': 'mi li vôn',
'a': 'am pe', 'ma': 'mi li am pe', 'ka': 'ki lô am pe',
'w': 'oát', 'kw': 'ki lô oát', 'mw': 'mê ga oát', 'gw': 'gi ga oát',
'kwh': 'ki lô oát giờ', 'mwh': 'mê ga oát giờ', 'wh': 'oát giờ',
'ω': 'ôm', 'ohm': 'ôm', 'kω': 'ki lô ôm', 'mω': 'mê ga ôm',
'hz': 'héc', 'khz': 'ki lô héc', 'mhz': 'mê ga héc', 'ghz': 'gi ga héc',
'pa': 'pát cal', 'kpa': 'ki lô pát cal', 'mpa': 'mê ga pát cal',
'bar': 'ba', 'mbar': 'mi li ba', 'atm': 'át mốt phia', 'psi': 'pi ét xai',
'j': 'giun', 'kj': 'ki lô giun',
'cal': 'ca lo', 'kcal': 'ki lô ca lo',
}
self.digits = ['không', 'một', 'hai', 'ba', 'bốn',
'năm', 'sáu', 'bảy', 'tám', 'chín']
def normalize(self, text):
"""Main normalization pipeline with EN tag protection."""
# Step 1: Extract and protect EN tags
en_contents = []
placeholder_pattern = "___EN_PLACEHOLDER_{}___ "
def extract_en(match):
en_contents.append(match.group(0))
return placeholder_pattern.format(len(en_contents) - 1)
text = re.sub(r'<en>.*?</en>', extract_en, text, flags=re.IGNORECASE)
# Step 2: Normal normalization pipeline
text = text.lower()
text = self._normalize_temperature(text)
text = self._normalize_currency(text)
text = self._normalize_percentage(text)
text = self._normalize_units(text)
text = self._normalize_time(text)
text = self._normalize_date(text)
text = self._normalize_phone(text)
text = self._normalize_numbers(text)
text = self._number_to_words(text)
text = self._normalize_special_chars(text)
text = self._normalize_whitespace(text)
# Step 3: Restore EN tags
for idx, en_content in enumerate(en_contents):
text = text.replace(placeholder_pattern.format(idx).lower(), en_content + ' ')
# Final whitespace cleanup
text = self._normalize_whitespace(text)
return text
def _normalize_temperature(self, text):
"""Convert temperature notation to words."""
text = re.sub(r'-(\d+(?:[.,]\d+)?)\s*°\s*c\b', r'âm \1 độ xê', text, flags=re.IGNORECASE)
text = re.sub(r'-(\d+(?:[.,]\d+)?)\s*°\s*f\b', r'âm \1 độ ép', text, flags=re.IGNORECASE)
text = re.sub(r'(\d+(?:[.,]\d+)?)\s*°\s*c\b', r'\1 độ xê', text, flags=re.IGNORECASE)
text = re.sub(r'(\d+(?:[.,]\d+)?)\s*°\s*f\b', r'\1 độ ép', text, flags=re.IGNORECASE)
text = re.sub(r'°', ' độ ', text)
return text
def _normalize_currency(self, text):
"""Convert currency notation to words."""
def decimal_currency(match):
whole = match.group(1)
decimal = match.group(2)
unit = match.group(3)
decimal_words = ' '.join([self.digits[int(d)] for d in decimal])
unit_map = {'k': 'nghìn', 'm': 'triệu', 'b': 'tỷ'}
unit_word = unit_map.get(unit.lower(), unit)
return f"{whole} phẩy {decimal_words} {unit_word}"
text = re.sub(r'(\d+)[.,](\d+)\s*([kmb])\b', decimal_currency, text, flags=re.IGNORECASE)
text = re.sub(r'(\d+)\s*k\b', r'\1 nghìn', text, flags=re.IGNORECASE)
text = re.sub(r'(\d+)\s*m\b', r'\1 triệu', text, flags=re.IGNORECASE)
text = re.sub(r'(\d+)\s*b\b', r'\1 tỷ', text, flags=re.IGNORECASE)
text = re.sub(r'(\d+(?:[.,]\d+)?)\s*đ\b', r'\1 đồng', text)
text = re.sub(r'(\d+(?:[.,]\d+)?)\s*vnd\b', r'\1 đồng', text, flags=re.IGNORECASE)
text = re.sub(r'\$\s*(\d+(?:[.,]\d+)?)', r'\1 đô la', text)
text = re.sub(r'(\d+(?:[.,]\d+)?)\s*\$', r'\1 đô la', text)
return text
def _normalize_percentage(self, text):
"""Convert percentage to words."""
text = re.sub(r'(\d+(?:[.,]\d+)?)\s*%', r'\1 phần trăm', text)
return text
def _normalize_units(self, text):
"""Convert measurement units to words."""
def expand_compound_with_number(match):
number = match.group(1)
unit1 = match.group(2).lower()
unit2 = match.group(3).lower()
full_unit1 = self.units.get(unit1, unit1)
full_unit2 = self.units.get(unit2, unit2)
return f"{number} {full_unit1} trên {full_unit2}"
def expand_compound_without_number(match):
unit1 = match.group(1).lower()
unit2 = match.group(2).lower()
full_unit1 = self.units.get(unit1, unit1)
full_unit2 = self.units.get(unit2, unit2)
return f"{full_unit1} trên {full_unit2}"
text = re.sub(r'(\d+(?:[.,]\d+)?)\s*([a-zA-Zμµ²³°]+)/([a-zA-Zμµ²³°0-9]+)\b',
expand_compound_with_number, text)
text = re.sub(r'\b([a-zA-Zμµ²³°]+)/([a-zA-Zμµ²³°0-9]+)\b',
expand_compound_without_number, text)
sorted_units = sorted(self.units.items(), key=lambda x: len(x[0]), reverse=True)
for unit, full_name in sorted_units:
pattern = r'(\d+(?:[.,]\d+)?)\s*' + re.escape(unit) + r'\b'
text = re.sub(pattern, rf'\1 {full_name}', text, flags=re.IGNORECASE)
for unit, full_name in sorted_units:
if any(c in unit for c in '²³°'):
pattern = r'\b' + re.escape(unit) + r'\b'
text = re.sub(pattern, full_name, text, flags=re.IGNORECASE)
return text
def _normalize_time(self, text):
"""Convert time notation to words with validation."""
def validate_and_convert_time(match):
"""Validate time components before converting."""
groups = match.groups()
# HH:MM:SS format
if len(groups) == 3:
hour, minute, second = groups
hour_int, minute_int, second_int = int(hour), int(minute), int(second)
if not (0 <= hour_int <= 23):
return match.group(0)
if not (0 <= minute_int <= 59):
return match.group(0)
if not (0 <= second_int <= 59):
return match.group(0)
return f"{hour} giờ {minute} phút {second} giây"
# HH:MM or HHhMM format
elif len(groups) == 2:
hour, minute = groups
hour_int, minute_int = int(hour), int(minute)
if not (0 <= hour_int <= 23):
return match.group(0)
if not (0 <= minute_int <= 59):
return match.group(0)
return f"{hour} giờ {minute} phút"
# HHh format
else:
hour = groups[0]
hour_int = int(hour)
if not (0 <= hour_int <= 23):
return match.group(0)
return f"{hour} giờ"
text = re.sub(r'(\d{1,2}):(\d{2}):(\d{2})', validate_and_convert_time, text)
text = re.sub(r'(\d{1,2}):(\d{2})', validate_and_convert_time, text)
text = re.sub(r'(\d{1,2})h(\d{2})', validate_and_convert_time, text)
text = re.sub(r'(\d{1,2})h\b', validate_and_convert_time, text)
return text
def _normalize_date(self, text):
"""Convert date notation to words with validation."""
def is_valid_date(day, month, year):
"""Check if date components are valid."""
day, month, year = int(day), int(month), int(year)
if not (1 <= day <= 31):
return False
if not (1 <= month <= 12):
return False
return True
def date_to_text(match):
day, month, year = match.groups()
if is_valid_date(day, month, year):
return f"ngày {day} tháng {month} năm {year}"
return match.group(0)
def date_iso_to_text(match):
year, month, day = match.groups()
if is_valid_date(day, month, year):
return f"ngày {day} tháng {month} năm {year}"
return match.group(0)
def date_short_year(match):
day, month, year = match.groups()
full_year = f"20{year}" if int(year) < 50 else f"19{year}"
if is_valid_date(day, month, full_year):
return f"ngày {day} tháng {month} năm {full_year}"
return match.group(0)
text = re.sub(r'\bngày\s+(\d{1,2})[/\-](\d{1,2})[/\-](\d{4})\b',
lambda m: date_to_text(m).replace('ngày ngày', 'ngày'), text)
text = re.sub(r'\bngày\s+(\d{1,2})[/\-](\d{1,2})[/\-](\d{2})\b',
lambda m: date_short_year(m).replace('ngày ngày', 'ngày'), text)
text = re.sub(r'\b(\d{4})-(\d{1,2})-(\d{1,2})\b', date_iso_to_text, text)
text = re.sub(r'\b(\d{1,2})[/\-](\d{1,2})[/\-](\d{4})\b', date_to_text, text)
text = re.sub(r'\b(\d{1,2})[/\-](\d{1,2})[/\-](\d{2})\b', date_short_year, text)
return text
def _normalize_phone(self, text):
"""Convert phone numbers to digit-by-digit reading."""
def phone_to_text(match):
phone = match.group(0)
phone = re.sub(r'[^\d]', '', phone)
if phone.startswith('84') and len(phone) >= 10:
phone = '0' + phone[2:]
if 10 <= len(phone) <= 11:
words = [self.digits[int(d)] for d in phone]
return ' '.join(words) + ' '
return match.group(0)
text = re.sub(r'(\+84|84)[\s\-\.]?\d[\d\s\-\.]{7,}', phone_to_text, text)
text = re.sub(r'\b0\d[\d\s\-\.]{8,}', phone_to_text, text)
return text
def _normalize_numbers(self, text):
text = re.sub(r'(\d+(?:[,.]\d+)?)%', lambda m: f'{m.group(1)} phần trăm', text)
text = re.sub(r'(\d{1,3})(?:\.(\d{3}))+', lambda m: m.group(0).replace('.', ''), text)
def decimal_to_words(match):
whole = match.group(1)
decimal = match.group(2)
decimal_words = ' '.join([self.digits[int(d)] for d in decimal])
separator = 'phẩy' if ',' in match.group(0) else 'chấm'
return f"{whole} {separator} {decimal_words}"
text = re.sub(r'(\d+),(\d+)', decimal_to_words, text)
text = re.sub(r'(\d+)\.(\d{1,2})\b', decimal_to_words, text)
return text
def _read_two_digits(self, n):
"""Read two-digit numbers in Vietnamese."""
if n < 10:
return self.digits[n]
elif n == 10:
return "mười"
elif n < 20:
if n == 15:
return "mười lăm"
return f"mười {self.digits[n % 10]}"
else:
tens = n // 10
ones = n % 10
if ones == 0:
return f"{self.digits[tens]} mươi"
elif ones == 1:
return f"{self.digits[tens]} mươi mốt"
elif ones == 5:
return f"{self.digits[tens]} mươi lăm"
else:
return f"{self.digits[tens]} mươi {self.digits[ones]}"
def _read_three_digits(self, n):
"""Read three-digit numbers in Vietnamese."""
if n < 100:
return self._read_two_digits(n)
hundreds = n // 100
remainder = n % 100
result = f"{self.digits[hundreds]} trăm"
if remainder == 0:
return result
elif remainder < 10:
result += f" lẻ {self.digits[remainder]}"
else:
result += f" {self._read_two_digits(remainder)}"
return result
def _convert_number_to_words(self, num):
"""Convert a number to Vietnamese words."""
if num == 0:
return "không"
if num < 0:
return f"âm {self._convert_number_to_words(-num)}"
if num >= 1000000000:
billion = num // 1000000000
remainder = num % 1000000000
result = f"{self._read_three_digits(billion)} tỷ"
if remainder > 0:
result += f" {self._convert_number_to_words(remainder)}"
return result
elif num >= 1000000:
million = num // 1000000
remainder = num % 1000000
result = f"{self._read_three_digits(million)} triệu"
if remainder > 0:
result += f" {self._convert_number_to_words(remainder)}"
return result
elif num >= 1000:
thousand = num // 1000
remainder = num % 1000
result = f"{self._read_three_digits(thousand)} nghìn"
if remainder > 0:
if remainder < 10:
result += f" không trăm lẻ {self.digits[remainder]}"
elif remainder < 100:
result += f" không trăm {self._read_two_digits(remainder)}"
else:
result += f" {self._read_three_digits(remainder)}"
return result
else:
return self._read_three_digits(num)
def _number_to_words(self, text):
"""Convert all remaining numbers to words."""
def convert_number(match):
num = int(match.group(0))
return self._convert_number_to_words(num)
text = re.sub(r'\b\d+\b', convert_number, text)
return text
def _normalize_special_chars(self, text):
"""Handle special characters."""
# Remove quotes first to avoid creating spaces before commas
text = text.replace('"', '')
text = text.replace("'", '')
text = text.replace(''', '')
text = text.replace(''', '')
text = text.replace('"', '')
text = text.replace('"', '')
text = text.replace('&', ' và ')
text = text.replace('+', ' cộng ')
text = text.replace('=', ' bằng ')
text = text.replace('#', ' thăng ')
# Handle parentheses/brackets as natural pauses: (text) -> , text ,
text = re.sub(r'[\(\[\{]\s*(.*?)\s*[\)\]\}]', r', \1, ', text)
# Remaining individual brackets or parens
text = re.sub(r'[\[\]\(\)\{\}]', ' ', text)
# Paired dashes (like parentheses): - text - -> , text ,
text = re.sub(r'(?:\s+|^)[-–—]\s*(.*?)\s*[-–—](?:\s+|$)', r', \1 , ', text)
# Single dashes used as punctuation (with spaces) -> comma
text = re.sub(r'\s+[-–—]+\s+', ', ', text)
# Dashes at the start of a line (bullet points) -> comma
text = re.sub(r'^[-–—]+\s+', ', ', text)
# Collapse multiple commas and surrounding spaces (remove spaces before AND after commas)
text = re.sub(r'\s*,\s*', ', ', text)
text = re.sub(r',\s*,+', ',', text) # Remove duplicate commas
text = re.sub(r'\.{2,}', ' ', text)
text = re.sub(r'\s+\.\s+', ' ', text)
text = re.sub(r'[^\w\sàáảãạăắằẳẵặâấầẩẫậèéẻẽẹêếềểễệìíỉĩịòóỏõọôốồổỗộơớờởỡợùúủũụưứừửữựỳýỷỹỵđ.,!?;:@%_]', ' ', text)
return text
def _normalize_whitespace(self, text):
"""Normalize whitespace."""
text = re.sub(r'\s+', ' ', text)
text = text.strip()
return text
if __name__ == "__main__":
normalizer = VietnameseTTSNormalizer()
test_texts = [
"Chỉ cần thay đổi một dấu thanh, ý nghĩa của từ đã hoàn toàn khác biệt. Ví dụ như \"ma\", \"má\", \"mà\", \"mả\", \"mã\", \"mạ\" – đây chính là \"bài toán khó\" mà các kỹ sư công nghệ phải giải quyết để tạo ra một giọng đọc tự nhiên như người bản xứ."
]
print("=" * 80)
print("VIETNAMESE TTS NORMALIZATION TEST (WITH EN TAG)")
print("=" * 80)
for text in test_texts:
print(f"\n📝 Input: {text}")
normalized = normalizer.normalize(text)
print(f"🎵 Output: {normalized}")
print("-" * 80) |