Datasets:
File size: 40,979 Bytes
d49e060 | 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 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 | import pyphen
import re
import threading
import fugashi
from num2words import num2words
# ========== 工具模块:Pyphen 缓存 ==========
class PyphenCache:
_instance = None
_cache = {}
def __new__(cls):
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instance
def get_dictionary(self, lang_code):
if lang_code not in self._cache:
self._cache[lang_code] = pyphen.Pyphen(lang=lang_code)
return self._cache[lang_code]
_pyphen_cache = PyphenCache()
def _pyphen_syllable_count(word, pyphen_lang):
"""使用 pyphen 计算音节数(带缓存,规范化处理)"""
# 规范化:去除尾部标点和连字符
clean_word = word.rstrip(".,;:!?()[]{}\"\'-")
normalized = clean_word.replace("-", "") # 避免连字符被算作音节分隔
# 检查西语词典
if pyphen_lang == 'es_ES' and normalized.lower() in _SPANISH_SYLLABLES:
return _SPANISH_SYLLABLES[normalized.lower()]
try:
dic = _pyphen_cache.get_dictionary(pyphen_lang)
hyphenated = dic.inserted(normalized)
return hyphenated.count("-") + 1
except Exception:
return _fallback_syllable_count(normalized)
def _fallback_syllable_count(word):
word = word.lower()
if len(word) <= 3:
return 1
count = 0
vowels = "aeiouy"
if word[0] in vowels:
count += 1
for i in range(1, len(word)):
if word[i] in vowels and word[i - 1] not in vowels:
count += 1
if word.endswith('e'):
count -= 1
if word.endswith('le') and len(word) > 2 and word[-3] not in vowels:
count += 1
return max(1, count)
# ========== 混合内容解析器 ==========
_SCRIPT_RANGES = [
(re.compile(r'[一-鿿㐀-䶿]'), 'han'),
(re.compile(r'[-ゟ゠-ヿ]'), 'kana'),
(re.compile(r'[ء-يٱ-ۓە-ۿ'
r'ݐ-ݿࢠ-ࣿ'
r'ﭐ-﷿ﹰ-]'), 'arabic'),
(re.compile(r'[ً-ٰٟٓ]'), 'arabic_diacritic'),
(re.compile(r'[a-zA-ZÀ-ÿŒœ]'), 'latin'), # 包含扩展拉丁字母(包括 Œ/œ)
(re.compile(r'[0-9٠-٩0-9]'), 'number'), # ASCII、阿拉伯语、全角数字
]
def _detect_script(char):
for pattern, script in _SCRIPT_RANGES:
if pattern.match(char):
return script
return 'other'
def _parse_mixed_content(text):
if not text:
return []
segments = []
current_segment = ""
current_type = None
i = 0
while i < len(text):
char = text[i]
char_type = _detect_script(char)
# 处理数字相关的特殊格式
if char_type == 'number' or (char_type == 'other' and char in '.-/$'):
# 尝试匹配完整的数字格式(包括电话号码、小数、货币等)
number_match = re.match(r'[\d.,\-/$]+', text[i:])
if number_match:
number_str = number_match.group()
# 检查是否包含数字
if re.search(r'\d', number_str):
# 检查是否是 COVID-19 这类字母+连字符+数字
# 如果前面紧邻字母且以连字符开头,这是连字符词的一部分
if number_str.startswith('-') and i > 0 and text[i-1].isalpha():
# 这是连字符词的一部分,不单独处理
if current_type:
current_segment += char
else:
current_segment = char
current_type = 'other'
i += 1
continue
# 检查是否有序数后缀(st, nd, rd, th)
ordinal_suffix = ''
next_pos = i + len(number_str)
if next_pos + 2 <= len(text):
potential_suffix = text[next_pos:next_pos+2]
if potential_suffix.lower() in ('st', 'nd', 'rd', 'th'):
ordinal_suffix = potential_suffix
if current_segment and current_type:
segments.append((current_segment.strip(), current_type))
# 如果有序数后缀,合并到数字中
if ordinal_suffix:
segments.append((number_str + ordinal_suffix, 'number'))
i += len(number_str) + len(ordinal_suffix)
else:
segments.append((number_str, 'number'))
i += len(number_str)
current_segment = ""
current_type = None
continue
if char_type == 'other':
if char.isspace():
if current_type == 'latin':
current_segment += char
i += 1
continue
elif current_segment:
segments.append((current_segment.strip(), current_type))
current_segment = ""
current_type = None
i += 1
continue
if current_type:
current_segment += char
i += 1
continue
# arabic diacritics 归入 arabic
if char_type == 'arabic_diacritic':
char_type = 'arabic'
if char_type == current_type:
current_segment += char
else:
if current_segment:
segments.append((current_segment.strip(), current_type))
current_segment = char
current_type = char_type
i += 1
if current_segment and current_segment.strip():
segments.append((current_segment.strip(), current_type))
return segments
# ========== 数字展开模块 ==========
# num2words 语言代码映射
_NUM2WORDS_LANG_MAP = {
'en': 'en',
'zh': 'zh',
'ja': 'ja',
'de': 'de',
'fr': 'fr',
'es': 'es',
'ar': 'ar',
}
# 英语字母发音音节数 (A=1, B=1, C=1, D=1, E=1, F=1, G=1, H=1, I=1,
# J=1, K=1, L=1, M=1, N=1, O=1, P=1, Q=1, R=1, S=1, T=1, U=1,
# V=1, W=3, X=1, Y=1, Z=1)
_LETTER_SYLLABLES_EN = {
'A': 1, 'B': 1, 'C': 1, 'D': 1, 'E': 1, 'F': 1, 'G': 1, 'H': 1,
'I': 1, 'J': 1, 'K': 1, 'L': 1, 'M': 1, 'N': 1, 'O': 1, 'P': 1,
'Q': 1, 'R': 1, 'S': 1, 'T': 1, 'U': 1, 'V': 1, 'W': 3, 'X': 1,
'Y': 1, 'Z': 1,
}
def _is_year_like(num_str):
"""判断数字是否可能是年份(1000-2099)"""
# 如果包含逗号,不是年份(是带千分位的数字)
if ',' in num_str:
return False
try:
n = int(num_str)
return 1000 <= n <= 2099 and len(num_str) == 4
except ValueError:
return False
def _expand_year_en(year_str):
"""英语年份特殊读法:2024 → twenty twenty-four"""
n = int(year_str)
if 2000 <= n <= 2009:
return num2words(n, lang='en')
if 2010 <= n <= 2099:
first = n // 100
second = n % 100
first_word = num2words(first, lang='en')
second_word = num2words(second, lang='en')
return f"{first_word} {second_word}"
# 1900-1999: nineteen ninety-nine
if 1000 <= n <= 1999:
first = n // 100
second = n % 100
first_word = num2words(first, lang='en')
if second == 0:
return f"{first_word} hundred"
second_word = num2words(second, lang='en')
return f"{first_word} {second_word}"
return num2words(n, lang='en')
def _expand_number(num_str, lang):
"""将数字字符串展开为对应语言的文字"""
# 处理特殊格式
# 处理英文序数后缀
ordinal_suffix = ''
if lang == 'en' and len(num_str) > 2:
last_two = num_str[-2:].lower()
if last_two in ('st', 'nd', 'rd', 'th'):
ordinal_suffix = last_two
num_str = num_str[:-2]
# 去除货币符号
num_str = num_str.lstrip('$¥€£')
# 处理千位分隔符和小数点(根据语言)
if lang in ('es', 'fr', 'de'):
# 欧洲大陆:逗号是小数点,点是千位分隔符
# 先去除千位分隔符(点)
num_str_temp = num_str.replace('.', '')
# 将逗号替换为点(标准化为英语格式)
num_str_clean = num_str_temp.replace(',', '.')
else:
# 英语/中文/阿拉伯语:点是小数点,逗号是千位分隔符
# 去除千位分隔符(逗号)
num_str_clean = num_str.replace(',', '')
# 处理小数
if '.' in num_str_clean:
parts = num_str_clean.split('.')
if len(parts) == 2 and parts[0].isdigit() and parts[1].isdigit():
try:
n2w_lang = _NUM2WORDS_LANG_MAP.get(lang, 'en')
# 整数部分
result = num2words(int(parts[0]), lang=n2w_lang)
# 小数点的表达(根据语言)
if lang == 'zh':
result += '点'
elif lang == 'es':
result += ' coma'
elif lang == 'fr':
result += ' virgule'
elif lang == 'de':
result += ' Komma'
elif lang == 'ar':
result += ' فاصلة'
else:
result += ' point'
# 小数部分逐位读
for digit in parts[1]:
if lang == 'zh':
_ZH_DIGITS = '零一二三四五六七八九'
result += _ZH_DIGITS[int(digit)]
else:
result += ' ' + num2words(int(digit), lang=n2w_lang)
return result
except Exception:
pass
# 处理日期(三段斜杠数字)
if '/' in num_str_clean:
parts = num_str_clean.split('/')
# 检查是否是日期格式(三段数字)
if len(parts) == 3 and all(p.isdigit() for p in parts):
try:
n2w_lang = _NUM2WORDS_LANG_MAP.get(lang, 'en')
result_parts = []
for part in parts:
result_parts.append(num2words(int(part), lang=n2w_lang))
return ' '.join(result_parts)
except Exception:
pass
# 处理分数
if '/' in num_str_clean:
parts = num_str_clean.split('/')
if len(parts) == 2 and parts[0].isdigit() and parts[1].isdigit():
try:
n2w_lang = _NUM2WORDS_LANG_MAP.get(lang, 'en')
numerator_int = int(parts[0])
denominator_int = int(parts[1])
# 特殊处理常见分数
if lang == 'en':
if numerator_int == 1 and denominator_int == 2:
return "one half"
elif numerator_int == 1 and denominator_int == 4:
return "one quarter"
elif numerator_int == 3 and denominator_int == 4:
return "three quarters"
# 通用处理
numerator = num2words(numerator_int, lang=n2w_lang)
# 分母用序数
denominator = num2words(denominator_int, lang=n2w_lang, to='ordinal')
return f"{numerator} {denominator}"
except Exception:
pass
# 处理电话号码(连字符分隔的数字)
if '-' in num_str_clean and all(p.isdigit() for p in num_str_clean.split('-')):
# 电话号码逐位读
digits = num_str_clean.replace('-', '')
try:
n2w_lang = _NUM2WORDS_LANG_MAP.get(lang, 'en')
if lang == 'zh':
_ZH_DIGITS = '零一二三四五六七八九'
return ''.join(_ZH_DIGITS[int(d)] for d in digits)
else:
result = []
for digit in digits:
result.append(num2words(int(digit), lang=n2w_lang))
return ' '.join(result)
except Exception:
pass
# 处理纯整数
try:
n = int(num_str_clean)
except ValueError:
return num_str
# 检查是否是年份(使用原始字符串,包含逗号信息)
if lang == 'en' and _is_year_like(num_str):
return _expand_year_en(num_str_clean)
n2w_lang = _NUM2WORDS_LANG_MAP.get(lang, 'en')
# 中文:逐位读数字(如电话号码、年份等场景更常见)
if lang == 'zh':
_ZH_DIGITS = '零一二三四五六七八九'
return ''.join(_ZH_DIGITS[int(d)] for d in num_str_clean)
try:
return num2words(n, lang=n2w_lang)
except Exception:
return num2words(n, lang='en')
def _expand_decimal(text, lang):
"""处理小数"""
n2w_lang = _NUM2WORDS_LANG_MAP.get(lang, 'en')
try:
n = float(text)
return num2words(n, lang=n2w_lang)
except Exception:
return text
# ========== 缩写识别模块 ==========
# 作为完整单词发音的缩写(不逐字母读)及其音节数
_WORD_ACRONYMS = {
'NASA': 2, 'NATO': 2, 'ASAP': 4, 'IKEA': 3, 'OPEC': 2,
'FIFA': 2, 'UNESCO': 3, 'UNICEF': 3, 'NAFTA': 2, 'SARS': 1,
'AIDS': 1, 'RADAR': 2, 'LASER': 2, 'SCUBA': 2,
'PIN': 1, 'SIM': 1, 'RAM': 1, 'ROM': 1,
'LAN': 1, 'WAN': 1, 'JPEG': 2, 'GIF': 1,
'COVID': 2, 'COV': 1, # COVID-19, SARS-CoV-2
}
# 已知缩写/品牌名的音节数
_KNOWN_ABBREVIATIONS = {
# 品牌名
'iPhone': 2, 'iPad': 2, 'iPod': 2, 'iMac': 2,
'macOS': 3, 'iOS': 3, 'YouTube': 2, 'WiFi': 2,
'WhatsApp': 2, 'LinkedIn': 2, 'GitHub': 2, 'GitLab': 2,
'JavaScript': 3, 'TypeScript': 2, 'PowerPoint': 3,
'eBay': 2, 'PayPal': 2, 'FedEx': 2,
# 学位/职称缩写
'PhD': 3, 'Ph.D.': 3, 'Ph.D': 3,
'Dr': 2, 'Dr.': 2, # Doctor
'Mr': 2, 'Mr.': 2, # Mister
'Mrs': 2, 'Mrs.': 2, # Missus
'Ms': 2, 'Ms.': 2,
'Prof': 2, 'Prof.': 2, # Professor
# 技术缩写
'LaTeX': 2, 'MySQL': 3, 'PostgreSQL': 4,
}
def _is_spelled_out_acronym(word):
"""判断是否是逐字母拼读的缩写"""
# 检查是否在作为单词发音的缩写列表中
if word.upper() in _WORD_ACRONYMS:
return False
clean = word.replace('.', '')
if len(clean) < 2:
return False
# 全大写缩写:USA, FBI, MIT
if clean.isupper() and 2 <= len(clean) <= 6:
return True
# 带点的缩写:U.S.A., Ph.D., Dr.
if '.' in word and all(c.isupper() or c == '.' for c in word):
return True
# Mixed-case 缩写识别
# 规则:至少2个大写字母,且大写字母占比 >= 50%
upper_count = sum(1 for c in clean if c.isupper())
alpha_count = sum(1 for c in clean if c.isalpha())
if alpha_count >= 2 and upper_count >= 2:
# PhD, eBay, iOS, macOS 等
upper_ratio = upper_count / alpha_count
if upper_ratio >= 0.5:
return True
return False
def _abbreviation_syllable_count(word, lang='en'):
"""计算缩写/品牌名的音节数"""
# 规范化:去除尾部标点
clean = word.rstrip('.,;:!?()[]{}"\'-')
# 先检查已知缩写词典(使用规范化后的 token)
if clean in _KNOWN_ABBREVIATIONS:
return _KNOWN_ABBREVIATIONS[clean]
# 作为单词发音的缩写(使用规范化后的 token)
upper = clean.upper()
if upper in _WORD_ACRONYMS:
return _WORD_ACRONYMS[upper]
# 逐字母拼读的缩写(使用规范化后的 token)
if _is_spelled_out_acronym(clean):
letters = [c for c in clean if c.isalpha()]
if lang == 'en':
return sum(_LETTER_SYLLABLES_EN.get(c.upper(), 1) for c in letters)
return len(letters)
return None
# ========== 阿拉伯语音节计数(从原版保留并改进) ==========
_AR_FATHA = 'َ'
_AR_DAMMA = 'ُ'
_AR_KASRA = 'ِ'
_AR_SHORT_VOWELS = {_AR_FATHA, _AR_DAMMA, _AR_KASRA}
_AR_FATHATAN = 'ً'
_AR_DAMMATAN = 'ٌ'
_AR_KASRATAN = 'ٍ'
_AR_TANWEEN = {_AR_FATHATAN, _AR_DAMMATAN, _AR_KASRATAN}
_AR_SUKUN = 'ْ'
_AR_SHADDA = 'ّ'
_AR_SUPERSCRIPT_ALEF = 'ٰ'
_AR_DIACRITICS_RE = re.compile(r'[ً-ٰٟٓ]')
_AR_ALEF = 'ا'
_AR_WAW = 'و'
_AR_YAA = 'ي'
_AR_ALEF_MAQSURA = 'ى'
_AR_ALEF_MADDA = 'آ'
_AR_TAA_MARBUTA = 'ة'
_AR_TATWEEL = 'ـ'
_AR_LETTER_RE = re.compile(
r'[ء-غف-ي'
r'ً-ٰٟ'
r'ٱ-ۓە-ۿ'
r'ݐ-ݿࢠ-ࣿ'
r'ﭐ-﷿ﹰ-]+'
)
def _ar_is_letter(ch):
cp = ord(ch)
return ((0x0621 <= cp <= 0x063A)
or (0x0641 <= cp <= 0x064A)
or (0x0671 <= cp <= 0x06D3)
or (0x06D5 <= cp <= 0x06FF))
def _ar_is_fully_vocalized(word):
consonant_count = 0
vocalized_count = 0
chars = list(word)
n = len(chars)
for i, ch in enumerate(chars):
if (_ar_is_letter(ch)
and ch not in (_AR_ALEF, _AR_WAW, _AR_YAA, _AR_ALEF_MAQSURA,
_AR_ALEF_MADDA, _AR_TAA_MARBUTA)):
consonant_count += 1
if i + 1 < n and _AR_DIACRITICS_RE.match(chars[i + 1]):
vocalized_count += 1
if consonant_count == 0:
return False
return vocalized_count / consonant_count > 0.5
def _ar_vocalized_syllables(word):
syllables = 0
covered = False
for i, ch in enumerate(word):
if ch in _AR_SHORT_VOWELS:
syllables += 1
covered = True
elif ch in _AR_TANWEEN:
syllables += 1
covered = True
elif ch == _AR_SUPERSCRIPT_ALEF:
if not covered:
syllables += 1
covered = False
elif ch == _AR_ALEF_MADDA:
syllables += 1
covered = False
elif ch in (_AR_ALEF, _AR_ALEF_MAQSURA):
if i > 0 and not covered:
syllables += 1
covered = False
elif _ar_is_letter(ch):
covered = False
return max(1, syllables)
def _ar_unvocalized_syllables(word):
clean = _AR_DIACRITICS_RE.sub('', word)
clean = clean.replace(_AR_TATWEEL, '')
if not clean:
return 0
letters = list(clean)
n = len(letters)
if n == 0:
return 0
if n <= 2:
return 1
skeleton = []
for i, ch in enumerate(letters):
is_first = (i == 0)
if ch == _AR_ALEF_MAQSURA:
skeleton.append('V')
elif ch == _AR_ALEF_MADDA:
skeleton.append('V')
elif ch == _AR_ALEF:
skeleton.append('C' if is_first else 'V')
elif ch == _AR_TAA_MARBUTA:
skeleton.append('V')
elif ch in (_AR_WAW, _AR_YAA):
if (ch == _AR_YAA
and i == n - 2
and i + 1 < n
and letters[i + 1] == _AR_TAA_MARBUTA):
skeleton.append('C')
elif (ch == _AR_WAW
and i == n - 2
and i + 1 < n
and letters[i + 1] == _AR_TAA_MARBUTA):
skeleton.append('C')
elif is_first:
skeleton.append('C')
elif skeleton and skeleton[-1] == 'C':
skeleton.append('V')
else:
skeleton.append('C')
else:
skeleton.append('C')
v_positions = [i for i, x in enumerate(skeleton) if x == 'V']
if not v_positions:
return max(1, (len(skeleton) + 1) // 2)
syllables = len(v_positions)
syllables += v_positions[0] // 2
for k in range(1, len(v_positions)):
gap = v_positions[k] - v_positions[k - 1] - 1
syllables += gap // 2
post_c = len(skeleton) - v_positions[-1] - 1
if (post_c == 1
and len(v_positions) == 1
and v_positions[0] == 1
and len(skeleton) == 3):
syllables += 1
else:
syllables += post_c // 2
return max(1, syllables)
def _arabic_word_syllables(word):
if not word:
return 0
if _AR_DIACRITICS_RE.search(word):
if _ar_is_fully_vocalized(word):
return _ar_vocalized_syllables(word)
return _ar_unvocalized_syllables(word)
# ========== 日语音节计数(从原版保留并改进) ==========
_DIGIT_TO_KANA = {
'0': 'ゼロ', '1': 'いち', '2': 'に', '3': 'さん', '4': 'よん',
'5': 'ご', '6': 'ろく', '7': 'なな', '8': 'はち', '9': 'きゅう'
}
# One Tagger per thread. MeCab keeps parse state on the Tagger, so sharing a
# single instance across the evaluator's thread pool corrupts Japanese mora
# counts nondeterministically -- only syllable_order reads them, so the symptom
# was zh->ja instances flipping between runs at the default concurrency.
_thread_state = threading.local()
def _get_tagger():
tagger = getattr(_thread_state, "tagger", None)
if tagger is None:
tagger = fugashi.Tagger()
_thread_state.tagger = tagger
return tagger
def _count_japanese_mora(token):
has_kana = any('' <= c <= 'ゟ' or '゠' <= c <= 'ヿ' for c in token)
if not has_kana:
return len(token)
mora_count = 0
i = 0
length = len(token)
while i < length:
char = token[i]
if i + 1 < length and token[i + 1] in 'ゃゅょャュョ':
mora_count += 1
i += 2
elif char in 'っッんンー':
mora_count += 1
i += 1
elif '' <= char <= 'ゟ' or '゠' <= char <= 'ヿ':
mora_count += 1
i += 1
else:
i += 1
return mora_count
def _japanese_syllable_count(text):
total_mora = 0
parsed_nodes = _get_tagger()(text)
for word in parsed_nodes:
reading = getattr(word.feature, 'kana', None)
if reading is None:
reading = getattr(word.feature, 'pronBase', None)
if reading is None:
reading = word.surface
word_mora = _count_japanese_mora(reading)
total_mora += word_mora
return total_mora
# ========== 各语言计算器 ==========
# 西语常见词音节词典(pyphen 不准确的词)
_SPANISH_SYLLABLES = {
'país': 2, # pa-ís
'río': 2, # rí-o
'pingüino': 3, # pin-güi-no
'día': 2, # dí-a
'María': 3, # Ma-rí-a
'había': 3, # ha-bí-a
'tenía': 3, # te-ní-a
'podía': 3, # po-dí-a
'decía': 3, # de-cí-a
'hacía': 3, # ha-cí-a
'raíz': 2, # ra-íz
'maíz': 2, # ma-íz
'baúl': 2, # ba-úl
'Raúl': 2, # Ra-úl
}
_PYPHEN_LANG_MAP = {
'en': 'en_US',
'de': 'de_DE',
'fr': 'fr_FR',
'es': 'es_ES',
}
def _count_european(text, lang):
"""英、德、法、西等欧洲语言的音节计数"""
pyphen_lang = _PYPHEN_LANG_MAP.get(lang, 'en_US')
segments = _parse_mixed_content(text)
total = 0
for segment, script_type in segments:
if script_type == 'number':
expanded = _expand_number(segment, lang)
# 按空格和连字符分割
words = re.split(r'[\s\-]+', expanded)
for w in words:
clean_w = w.strip(',-')
if clean_w and clean_w.isalpha():
total += _pyphen_syllable_count(clean_w, pyphen_lang)
elif script_type == 'latin':
words = segment.split()
for w in words:
abbr_count = _abbreviation_syllable_count(w, lang)
if abbr_count is not None:
total += abbr_count
else:
total += _pyphen_syllable_count(w, pyphen_lang)
elif script_type == 'han':
# 只计算汉字,不包括标点
han_chars = re.findall(r'[一-鿿]', segment)
total += len(han_chars)
elif script_type == 'arabic':
# 混合内容中的阿拉伯语
ar_words = _AR_LETTER_RE.findall(segment)
for w in ar_words:
total += _arabic_word_syllables(w)
elif script_type == 'kana':
# 混合内容中的日语假名
total += _japanese_syllable_count(segment)
return total
def _expand_and_count_chinese(num_str):
"""展开数字并计算中文音节数"""
# 处理小数
if '.' in num_str:
parts = num_str.split('.')
if len(parts) == 2 and parts[0].isdigit() and parts[1].isdigit():
count = 0
# 整数部分逐位读
for digit in parts[0]:
count += 1
# 小数点:"点"
count += 1
# 小数部分逐位读
for digit in parts[1]:
count += 1
return count
# 其他数字格式:逐位读
digits = re.findall(r'\d', num_str)
return len(digits)
def _count_chinese(text, lang='zh'):
"""中文音节计数(改进版:处理数字和混合内容)"""
segments = _parse_mixed_content(text)
total = 0
for segment, script_type in segments:
if script_type == 'han':
# 只计算汉字,不包括标点
han_chars = re.findall(r'[一-鿿]', segment)
total += len(han_chars)
elif script_type == 'number':
# 统一使用 _expand_and_count_chinese 处理
total += _expand_and_count_chinese(segment)
elif script_type == 'latin':
words = segment.split()
for w in words:
abbr_count = _abbreviation_syllable_count(w, 'en')
if abbr_count is not None:
total += abbr_count
else:
total += _pyphen_syllable_count(w, 'en_US')
elif script_type == 'kana':
# 混合内容中的日语假名
total += _japanese_syllable_count(segment)
elif script_type == 'arabic':
# 混合内容中的阿拉伯语
ar_words = _AR_LETTER_RE.findall(segment)
for w in ar_words:
total += _arabic_word_syllables(w)
return total
def _count_arabic(text, lang='ar'):
"""阿拉伯语音节计数(改进版:处理混合内容)"""
segments = _parse_mixed_content(text)
total = 0
for segment, script_type in segments:
if script_type == 'arabic':
words = _AR_LETTER_RE.findall(segment)
for w in words:
total += _arabic_word_syllables(w)
elif script_type == 'number':
expanded = _expand_number(segment, 'ar')
ar_words = _AR_LETTER_RE.findall(expanded)
if ar_words:
for w in ar_words:
total += _arabic_word_syllables(w)
else:
# num2words 可能返回拉丁字母,用英语计算
for w in expanded.split():
if w.isalpha():
total += _pyphen_syllable_count(w, 'en_US')
elif script_type == 'latin':
words = segment.split()
for w in words:
abbr_count = _abbreviation_syllable_count(w, 'en')
if abbr_count is not None:
total += abbr_count
else:
total += _pyphen_syllable_count(w, 'en_US')
elif script_type == 'han':
# 混合内容中的汉字
han_chars = re.findall(r'[一-鿿]', segment)
total += len(han_chars)
elif script_type == 'kana':
# 混合内容中的日语假名
total += _japanese_syllable_count(segment)
return total
def _count_japanese_v2(text, lang='ja'):
"""日语音节计数(改进版:处理混合内容)"""
segments = _parse_mixed_content(text)
# 合并连续的 han 和 kana 段落,让 fugashi 整体处理
merged_segments = []
i = 0
while i < len(segments):
segment, script_type = segments[i]
if script_type in ('han', 'kana'):
# 收集连续的 han/kana 段落
japanese_text = segment
j = i + 1
while j < len(segments) and segments[j][1] in ('han', 'kana'):
japanese_text += segments[j][0]
j += 1
merged_segments.append((japanese_text, 'japanese'))
i = j
else:
merged_segments.append((segment, script_type))
i += 1
# 计算音节
total = 0
for segment, script_type in merged_segments:
if script_type == 'japanese':
total += _japanese_syllable_count(segment)
elif script_type == 'number':
# 日语中数字通过 fugashi 处理更准确
total += _japanese_syllable_count(segment)
elif script_type == 'latin':
words = segment.split()
for w in words:
abbr_count = _abbreviation_syllable_count(w, 'en')
if abbr_count is not None:
total += abbr_count
else:
total += _pyphen_syllable_count(w, 'en_US')
return total
# ========== 公共 API ==========
def cal_syllable_count(text, lang='en'):
if not text or not text.strip():
return 0
text = text.strip()
if lang.lower() == 'zh':
return _count_chinese(text)
elif lang.lower() == 'ja':
return _count_japanese_v2(text)
elif lang.lower() == 'ar':
return _count_arabic(text)
else:
return _count_european(text, lang.lower())
def cal_syllable_details(text, lang='en'):
"""返回详细的音节分解信息"""
if not text or not text.strip():
return {
'total_syllables': 0,
'word_count': 0,
'syllables_per_word': 0,
'syllable_breakdown': []
}
text = text.strip()
total_syllables = cal_syllable_count(text, lang)
# 根据语言使用不同的分解策略
breakdown = []
if lang.lower() == 'zh':
# 中文:按混合内容分段
segments = _parse_mixed_content(text)
for segment, script_type in segments:
if script_type == 'han':
# 汉字逐个计数(只计算汉字,不包括标点)
han_chars = re.findall(r'[一-鿿]', segment)
for char in han_chars:
breakdown.append({'word': char, 'syllables': 1})
elif script_type == 'number':
# 使用统一的计数逻辑
syllables = _expand_and_count_chinese(segment)
breakdown.append({
'word': segment,
'syllables': syllables,
})
elif script_type == 'latin':
words = segment.split()
for w in words:
abbr_count = _abbreviation_syllable_count(w, 'en')
if abbr_count is not None:
syllables = abbr_count
else:
syllables = _pyphen_syllable_count(w, 'en_US')
breakdown.append({'word': w, 'syllables': syllables})
elif script_type == 'kana':
# 混合内容中的日语假名
syllables = _japanese_syllable_count(segment)
breakdown.append({'word': segment, 'syllables': syllables})
elif script_type == 'arabic':
# 混合内容中的阿拉伯语
ar_words = _AR_LETTER_RE.findall(segment)
for w in ar_words:
syllables = _arabic_word_syllables(w)
breakdown.append({'word': w, 'syllables': syllables})
elif lang.lower() == 'ja':
# 日语:使用 fugashi 分词
segments = _parse_mixed_content(text)
# 合并连续的 han 和 kana 段落
merged_segments = []
i = 0
while i < len(segments):
segment, script_type = segments[i]
if script_type in ('han', 'kana'):
japanese_text = segment
j = i + 1
while j < len(segments) and segments[j][1] in ('han', 'kana'):
japanese_text += segments[j][0]
j += 1
merged_segments.append((japanese_text, 'japanese'))
i = j
else:
merged_segments.append((segment, script_type))
i += 1
# 对每个段落计算音节
for segment, script_type in merged_segments:
if script_type == 'japanese':
syllables = _japanese_syllable_count(segment)
breakdown.append({'word': segment, 'syllables': syllables})
elif script_type == 'number':
syllables = _japanese_syllable_count(segment)
breakdown.append({'word': segment, 'syllables': syllables})
elif script_type == 'latin':
words = segment.split()
for w in words:
abbr_count = _abbreviation_syllable_count(w, 'en')
if abbr_count is not None:
syllables = abbr_count
else:
syllables = _pyphen_syllable_count(w, 'en_US')
breakdown.append({'word': w, 'syllables': syllables})
elif script_type == 'han':
# 混合内容中的汉字
han_chars = re.findall(r'[一-鿿]', segment)
for char in han_chars:
breakdown.append({'word': char, 'syllables': 1})
elif script_type == 'kana':
# 混合内容中的日语假名
syllables = _japanese_syllable_count(segment)
breakdown.append({'word': segment, 'syllables': syllables})
elif lang.lower() == 'ar':
# 阿拉伯语
segments = _parse_mixed_content(text)
for segment, script_type in segments:
if script_type == 'arabic':
words = _AR_LETTER_RE.findall(segment)
for w in words:
syllables = _arabic_word_syllables(w)
breakdown.append({'word': w, 'syllables': syllables})
elif script_type == 'number':
expanded = _expand_number(segment, 'ar')
ar_words = _AR_LETTER_RE.findall(expanded)
if ar_words:
syllables = sum(_arabic_word_syllables(w) for w in ar_words)
else:
# 英语展开
syllables = sum(_pyphen_syllable_count(w, 'en_US') for w in expanded.split() if w.isalpha())
breakdown.append({
'word': segment,
'expanded': expanded,
'syllables': syllables,
})
elif script_type == 'latin':
words = segment.split()
for w in words:
abbr_count = _abbreviation_syllable_count(w, 'en')
if abbr_count is not None:
syllables = abbr_count
else:
syllables = _pyphen_syllable_count(w, 'en_US')
breakdown.append({'word': w, 'syllables': syllables})
elif script_type == 'han':
# 混合内容中的汉字
han_chars = re.findall(r'[一-鿿]', segment)
for char in han_chars:
breakdown.append({'word': char, 'syllables': 1})
elif script_type == 'kana':
# 混合内容中的日语假名
syllables = _japanese_syllable_count(segment)
breakdown.append({'word': segment, 'syllables': syllables})
else:
# 欧洲语言(英、德、法、西等)
pyphen_lang = _PYPHEN_LANG_MAP.get(lang.lower(), 'en_US')
segments = _parse_mixed_content(text)
for segment, script_type in segments:
if script_type == 'number':
expanded = _expand_number(segment, lang)
words = re.split(r'[\s\-]+', expanded)
syllables = 0
for w in words:
clean_w = w.strip(',-')
if clean_w and clean_w.isalpha():
syllables += _pyphen_syllable_count(clean_w, pyphen_lang)
breakdown.append({
'word': segment,
'expanded': expanded,
'syllables': syllables,
})
elif script_type == 'latin':
words = segment.split()
for w in words:
abbr_count = _abbreviation_syllable_count(w, lang)
if abbr_count is not None:
syllables = abbr_count
else:
syllables = _pyphen_syllable_count(w, pyphen_lang)
breakdown.append({'word': w, 'syllables': syllables})
elif script_type == 'han':
# 混合内容中的汉字
han_chars = re.findall(r'[一-鿿]', segment)
for char in han_chars:
breakdown.append({'word': char, 'syllables': 1})
elif script_type == 'arabic':
# 混合内容中的阿拉伯语
ar_words = _AR_LETTER_RE.findall(segment)
for w in ar_words:
syllables = _arabic_word_syllables(w)
breakdown.append({'word': w, 'syllables': syllables})
elif script_type == 'kana':
# 混合内容中的日语假名
syllables = _japanese_syllable_count(segment)
breakdown.append({'word': segment, 'syllables': syllables})
word_count = len(breakdown)
avg_syllables = round(total_syllables / word_count, 2) if word_count > 0 else 0
return {
'total_syllables': total_syllables,
'word_count': word_count,
'syllables_per_word': avg_syllables,
'syllable_breakdown': breakdown,
}
# ========== 测试入口 ==========
if __name__ == "__main__":
# 基础测试用例(和原版一致)
test_cases = [
("你好世界 Hello World", "zh"),
("Hello World", "en"),
("The quick brown fox jumps over the lazy dog", "en"),
("Der schnelle braune Fuchs springt über den faulen Hund", "de"),
("Le renard brun rapide saute par-dessus le chien paresseux", "fr"),
("مرحبا بك في العالم", "ar"),
]
# 数字展开测试
number_test_cases = [
("2024", "en"),
("100", "en"),
("I have 3 cats", "en"),
("2024年", "zh"),
("我有100个苹果", "zh"),
("123 مرحبا", "ar"),
]
# 缩写测试
abbreviation_test_cases = [
("USA", "en"),
("BMW", "en"),
("NASA", "en"),
("iPhone", "en"),
("The CEO of IBM", "en"),
]
# 混合内容测试
mixed_test_cases = [
("Hello世界2024年", "zh"),
("iPhone 15 Pro Max", "en"),
("مرحبا Hello 2024", "ar"),
]
# 日语测试
japanese_test_cases = [
("こんにちは", "ja"),
("きょう", "ja"),
("きっと", "ja"),
("お母さん", "ja"),
("コーヒー", "ja"),
("東京", "ja"),
("123", "ja"),
("Hello世界", "ja"),
]
print("=" * 70)
print("音节计数 V2 测试结果")
print("=" * 70)
for text, lang in test_cases:
count = cal_syllable_count(text, lang)
print(f"[{lang}] '{text}' → {count} 音节")
print("\n" + "=" * 70)
print("数字展开测试")
print("=" * 70)
for text, lang in number_test_cases:
count = cal_syllable_count(text, lang)
print(f"[{lang}] '{text}' → {count} 音节")
print("\n" + "=" * 70)
print("缩写识别测试")
print("=" * 70)
for text, lang in abbreviation_test_cases:
count = cal_syllable_count(text, lang)
print(f"[{lang}] '{text}' → {count} 音节")
print("\n" + "=" * 70)
print("混合内容测试")
print("=" * 70)
for text, lang in mixed_test_cases:
count = cal_syllable_count(text, lang)
details = cal_syllable_details(text, lang)
print(f"[{lang}] '{text}' → {count} 音节")
for item in details['syllable_breakdown']:
extra = f" (展开: {item['expanded']})" if 'expanded' in item else ""
print(f" '{item['word']}': {item['syllables']} 音节{extra}")
print("\n" + "=" * 70)
print("日语测试")
print("=" * 70)
for text, lang in japanese_test_cases:
count = cal_syllable_count(text, lang)
print(f"[{lang}] '{text}' → {count} 音拍")
print("\n" + "=" * 70)
|