File size: 34,415 Bytes
a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 d00ff23 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 d00ff23 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 d00ff23 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 d00ff23 a05aaa6 ec11370 d00ff23 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 d00ff23 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 d00ff23 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 ec11370 a05aaa6 d00ff23 | 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 | """
Adapted and supplemented from origional at https://github.com/KittenML/KittenTTS/blob/main/kittentts/preprocess.py
See license at: https://github.com/KittenML/KittenTTS/blob/main/LICENSE (Apache 2.0)
"""
import re
import unicodedata
# βββββββββββββββββββββββββββββββββββββββββββββ
# Number β Words conversion
# βββββββββββββββββββββββββββββββββββββββββββββ
_ONES = [
'',
'one',
'two',
'three',
'four',
'five',
'six',
'seven',
'eight',
'nine',
'ten',
'eleven',
'twelve',
'thirteen',
'fourteen',
'fifteen',
'sixteen',
'seventeen',
'eighteen',
'nineteen',
]
_TENS = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']
_SCALE = ['', 'thousand', 'million', 'billion', 'trillion']
_ORDINAL_EXCEPTIONS = {
'one': 'first',
'two': 'second',
'three': 'third',
'four': 'fourth',
'five': 'fifth',
'six': 'sixth',
'seven': 'seventh',
'eight': 'eighth',
'nine': 'ninth',
'twelve': 'twelfth',
}
_CURRENCY_SYMBOLS = {
'$': 'dollar',
'β¬': 'euro',
'Β£': 'pound',
'Β₯': 'yen',
'βΉ': 'rupee',
'β©': 'won',
'βΏ': 'bitcoin',
}
_CURRENCY_SCALE_MAP = {
'K': 'thousand',
'M': 'million',
'B': 'billion',
'T': 'trillion',
'thousand': 'thousand',
'million': 'million',
'billion': 'billion',
'trillion': 'trillion',
}
_ROMAN = [
(1000, 'M'),
(900, 'CM'),
(500, 'D'),
(400, 'CD'),
(100, 'C'),
(90, 'XC'),
(50, 'L'),
(40, 'XL'),
(10, 'X'),
(9, 'IX'),
(5, 'V'),
(4, 'IV'),
(1, 'I'),
]
_RE_ROMAN = re.compile(r'\b(M{0,4})(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})\b')
def _three_digits_to_words(n: int) -> str:
"""Convert a number 0β999 to English words."""
if n == 0:
return ''
parts = []
hundreds = n // 100
remainder = n % 100
if hundreds:
parts.append(f'{_ONES[hundreds]} hundred')
if remainder < 20:
if remainder:
parts.append(_ONES[remainder])
else:
tens_word = _TENS[remainder // 10]
ones_word = _ONES[remainder % 10]
parts.append(f'{tens_word}-{ones_word}' if ones_word else tens_word)
return ' '.join(parts)
def number_to_words(n: int) -> str:
"""
Convert an integer to its English word representation.
Examples:
1200 β "twelve hundred"
1000 β "one thousand"
1_000_000 β "one million"
-42 β "negative forty-two"
0 β "zero"
"""
if not isinstance(n, int):
n = int(n)
if n == 0:
return 'zero'
if n < 0:
return f'negative {number_to_words(-n)}'
# X00βX999 read as "X hundred" (e.g. 1200 β "twelve hundred")
# Exclude exact multiples of 1000 (1000 β "one thousand", not "ten hundred")
if 100 <= n <= 9999 and n % 100 == 0 and n % 1000 != 0:
hundreds = n // 100
if hundreds < 20:
return f'{_ONES[hundreds]} hundred'
parts = []
for _i, scale in enumerate(_SCALE):
chunk = n % 1000
if chunk:
chunk_words = _three_digits_to_words(chunk)
parts.append(f'{chunk_words} {scale}'.strip() if scale else chunk_words)
n //= 1000
if n == 0:
break
return ' '.join(reversed(parts))
def float_to_words(value, decimal_sep: str = 'point') -> str:
"""
Convert a float (or numeric string) to words, reading decimal digits individually.
Accepts a string to preserve trailing zeros (e.g. "1.50" β "one point five zero").
Examples:
3.14 β "three point one four"
-0.5 β "negative zero point five"
"3.10" β "three point one zero"
1.007 β "one point zero zero seven"
"""
text = value if isinstance(value, str) else f'{value}'
negative = text.startswith('-')
if negative:
text = text[1:]
if '.' in text:
int_part, dec_part = text.split('.', 1)
int_words = number_to_words(int(int_part)) if int_part else 'zero'
# Read each decimal digit individually; "0" β "zero"
digit_map = ['zero'] + _ONES[1:] # index 0 β "zero"
dec_words = ' '.join(digit_map[int(d)] for d in dec_part)
result = f'{int_words} {decimal_sep} {dec_words}'
else:
result = number_to_words(int(text))
return f'negative {result}' if negative else result
def roman_to_int(s: str) -> int:
"""Convert a Roman numeral string to an integer."""
val = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
result = 0
prev = 0
for ch in reversed(s.upper()):
curr = val[ch]
result += curr if curr >= prev else -curr
prev = curr
return result
# βββββββββββββββββββββββββββββββββββββββββββββ
# Regex patterns
# βββββββββββββββββββββββββββββββββββββββββββββ
_RE_URL = re.compile(r'https?://\S+|www\.\S+')
_RE_EMAIL = re.compile(r'\b[\w.+-]+@[\w-]+\.[a-z]{2,}\b', re.IGNORECASE)
_RE_HASHTAG = re.compile(r'#\w+')
_RE_MENTION = re.compile(r'@\w+')
_RE_HTML = re.compile(r'<[^>]+>')
_RE_PUNCT = re.compile(r'[^\w\s]')
_RE_SPACES = re.compile(r'\s+')
_RE_AI = re.compile(r'\bAI\b')
_RE_DOT_COM = re.compile(r'\.com\b', re.IGNORECASE)
_RE_PLUS = re.compile(r'\+')
_RE_AMPERSAND = re.compile(r'&')
_RE_AT_SYMBOL = re.compile(r'@')
_RE_NEWLINE = re.compile(r'[\r\n]+')
_RE_TILDE = re.compile(r'~')
_MONTH_MAP = {
'Jan': 'January',
'Feb': 'February',
'Mar': 'March',
'Apr': 'April',
'Jun': 'June',
'Jul': 'July',
'Aug': 'August',
'Sep': 'September',
'Sept': 'September',
'Oct': 'October',
'Nov': 'November',
'Dec': 'December',
}
# Regex looks for Title Case months followed by a period or a digit
# We handle "May" separately because it's a common word.
_RE_MONTHS = re.compile(r'\b(Jan|Feb|Mar|Apr|Jun|Jul|Aug|Sep|Sept|Oct|Nov|Dec)\.?\b(?=\s*\d|\s*$)')
_RE_MAY = re.compile(r'\bMay\b(?=\s*\d)') # Only expand May if followed by a number (May 5)
# Number: do NOT match a leading minus if it is immediately preceded by a letter
# (handles "gpt-3", "gpl-3", "v-2" etc.)
_RE_NUMBER = re.compile(r'(?<![a-zA-Z])-?[\d,]+(?:\.\d+)?')
# Ordinals: 1st, 2nd, 3rd, 4th β¦ 21st, 101st β¦
_RE_ORDINAL = re.compile(r'\b(\d+)(st|nd|rd|th)\b', re.IGNORECASE)
# Percentages: 50%, 3.5%
_RE_PERCENT = re.compile(r'(-?[\d,]+(?:\.\d+)?)\s*%')
# Currency: $100, β¬1,200.50, Β£50, $85K, $2.5M (optional scale suffix)
_RE_CURRENCY = re.compile(
r'([$β¬Β£Β₯βΉβ©βΏ])\s*([\d,]+(?:\.\d+)?)\s*(million|billion|trillion|thousand|[KMBT])?\b',
re.IGNORECASE,
)
# Time: 3:30pm, 14:00, 3:30 AM β requires 2-digit minutes so "3:0" (score) doesn't match
_RE_TIME = re.compile(r'\b(\d{1,2}):(\d{2})(?::(\d{2}))?\s*(am|pm)?\b', re.IGNORECASE)
# Ranges: 10-20, 100-200 (both sides numeric, hyphen between them)
_RE_RANGE = re.compile(r'(?<!\w)(\d+)-(\d+)(?!\w)')
# Version/model names: gpt-3, gpt-3.5, v2.0, Python-3.10, GPL-3
# Letter(s) + hyphen + digit(s) [+ more version parts]
_RE_MODEL_VER = re.compile(r'\b([a-zA-Z][a-zA-Z0-9]*)-(\d[\d.]*)(?=[^\d.]|$)')
# Measurement units glued to numbers: 100km, 50kg, 25Β°C, 5GB
_RE_UNIT = re.compile(
r'(\d+(?:\.\d+)?)\s*(km|kg|mg|ml|gb|mb|kb|tb|hz|khz|mhz|ghz|mph|kph|Β°[cCfF]|[cCfF]Β°|ms|ns|Β΅s)\b',
re.IGNORECASE,
)
# Scale suffixes (uppercase only to avoid ambiguity): 7B, 340M, 1.5K, 2T
# Must NOT be preceded by a letter (so 'MB' is handled by unit regex first)
_RE_SCALE = re.compile(r'(?<![a-zA-Z])(\d+(?:\.\d+)?)\s*([KMBT])(?![a-zA-Z\d])')
# Scientific notation: 1e-4, 2.5e10, 6.022E23
_RE_SCI = re.compile(r'(?<![a-zA-Z\d])(-?\d+(?:\.\d+)?)[eE]([+-]?\d+)(?![a-zA-Z\d])')
# Fractions: 1/2, 3/4, 2/3
_RE_FRACTION = re.compile(r'\b(\d+)\s*/\s*(\d+)\b')
# Decades: 80s, 90s, 1980s, 2020s (number ending in 0 followed by 's')
_RE_DECADE = re.compile(r'\b(\d{1,3})0s\b')
# Leading decimal (no digit before the dot): .5, .75
_RE_LEAD_DEC = re.compile(r'(?<!\d)\.([\d])')
# βββββββββββββββββββββββββββββββββββββββββββββ
# Expansion helpers
# βββββββββββββββββββββββββββββββββββββββββββββ
def expand_abbreviations(text: str) -> str:
"""
Handles specific abbreviations before lowercase normalization.
AI -> A.I.
.com -> dot com
"""
# 1. AI to A.I. (Case sensitive)
text = _RE_AI.sub('A.I.', text)
# 2. .com to dot com
text = _RE_DOT_COM.sub(' dot com', text)
return text
def expand_symbols(text: str) -> str:
"""
Translates mathematical and connector symbols to words.
"""
text = _RE_PLUS.sub(' plus ', text)
text = _RE_AMPERSAND.sub(' and ', text)
text = _RE_AT_SYMBOL.sub(' at ', text)
return text
def _ordinal_suffix(n: int) -> str:
"""Return the ordinal word for n (e.g. 1 β 'first', 5 β 'fifth', 21 β 'twenty-first')."""
word = number_to_words(n)
# For hyphenated compounds like "twenty-one", convert only the last part
if '-' in word:
prefix, last = word.rsplit('-', 1)
joiner = '-'
else:
parts = word.rsplit(' ', 1)
prefix, last, joiner = (parts[0], parts[1], ' ') if len(parts) == 2 else ('', parts[0], '')
# Check exception table
for base, ordinal in _ORDINAL_EXCEPTIONS.items():
if last == base:
last_ord = ordinal
break
else:
# General rule
if last.endswith('t'):
last_ord = last + 'h'
elif last.endswith('e'):
last_ord = last[:-1] + 'th'
else:
last_ord = last + 'th'
return f'{prefix}{joiner}{last_ord}' if prefix else last_ord
def expand_ordinals(text: str) -> str:
"""
Convert ordinal numbers to words.
Examples:
"1st place" β "first place"
"2nd floor" β "second floor"
"3rd base" β "third base"
"21st century" β "twenty-first century"
"100th day" β "one hundredth day"
"""
def _replace(m: re.Match) -> str:
return _ordinal_suffix(int(m.group(1)))
return _RE_ORDINAL.sub(_replace, text)
def expand_percentages(text: str) -> str:
"""
Expand percentage expressions.
Examples:
"50% off" β "fifty percent off"
"3.5% rate" β "three point five percent rate"
"-2% change" β "negative two percent change"
"""
def _replace(m: re.Match) -> str:
raw = m.group(1).replace(',', '')
if '.' in raw:
return float_to_words(float(raw)) + ' percent'
return number_to_words(int(raw)) + ' percent'
return _RE_PERCENT.sub(_replace, text)
def expand_newlines(text: str) -> str:
"""Change newlines/returns to a period and space for TTS pausing."""
return _RE_NEWLINE.sub('. ', text)
def expand_tilde(text: str) -> str:
"""Change ~ to 'about'."""
return _RE_TILDE.sub('about ', text)
def expand_currency(text: str) -> str:
"""
Expand currency amounts, including optional scale suffixes.
Examples:
"$100" β "one hundred dollars"
"β¬1,200.50" β "twelve hundred euros and fifty cents"
"Β£9.99" β "nine pounds and ninety-nine cents"
"$85K" β "eighty five thousand dollars"
"$2.5M" β "two point five million dollars"
"""
def _replace(m: re.Match) -> str:
symbol = m.group(1)
raw = m.group(2).replace(',', '')
scale_suffix = m.group(3)
unit = _CURRENCY_SYMBOLS.get(symbol, '')
# Handle Scaled Currency ($17.5 billion or $17.5B)
if scale_suffix:
# Normalize suffix (e.g., 'B' or 'billion' -> 'billion')
scale_word = _CURRENCY_SCALE_MAP.get(scale_suffix.upper(), scale_suffix.lower())
num = float_to_words(raw) if '.' in raw else number_to_words(int(raw))
return f'{num} {scale_word} {unit}{"s" if unit else ""}'.strip()
# Handle Standard Currency ($17.50)
if '.' in raw:
int_part, dec_part = raw.split('.', 1)
dec_val = int(dec_part[:2].ljust(2, '0'))
int_words = number_to_words(int(int_part))
result = f'{int_words} {unit}s' if unit else int_words
if dec_val:
cents = number_to_words(dec_val)
result += f' and {cents} cent{"s" if dec_val != 1 else ""}'
else:
val = int(raw)
words = number_to_words(val)
result = f'{words} {unit}{"s" if val != 1 and unit else ""}' if unit else words
return result
return _RE_CURRENCY.sub(_replace, text)
def expand_time(text: str) -> str:
"""
Expand time expressions.
Examples:
"3:30pm" β "three thirty pm"
"14:00" β "fourteen hundred"
"9:05 AM" β "nine oh five am"
"12:00pm" β "twelve pm"
"""
def _replace(m: re.Match) -> str:
h = int(m.group(1))
mins = int(m.group(2))
suffix = (' ' + m.group(4).lower()) if m.group(4) else ''
h_words = number_to_words(h)
if mins == 0:
return f'{h_words} hundred{suffix}' if not m.group(4) else f'{h_words}{suffix}'
elif mins < 10:
return f'{h_words} oh {number_to_words(mins)}{suffix}'
else:
return f'{h_words} {number_to_words(mins)}{suffix}'
return _RE_TIME.sub(_replace, text)
def expand_ranges(text: str) -> str:
"""
Expand numeric ranges.
Examples:
"10-20 items" β "ten to twenty items"
"pages 100-200" β "pages one hundred to two hundred"
"2020-2024" β "twenty twenty to twenty twenty-four"
"""
def _replace(m: re.Match) -> str:
lo = number_to_words(int(m.group(1)))
hi = number_to_words(int(m.group(2)))
return f'{lo} to {hi}'
return _RE_RANGE.sub(_replace, text)
def expand_model_names(text: str) -> str:
"""
Normalise version/model names that use letter-hyphen-number patterns,
so the number is not misread as negative.
Examples:
"GPT-3" β "GPT 3"
"gpt-3.5" β "gpt 3.5"
"GPL-3" β "GPL 3"
"Python-3.10"β "Python 3.10"
"v2.0" stays as "v2.0" (no hyphen β handled by number replacement)
"IPv6" stays as "IPv6"
"""
return _RE_MODEL_VER.sub(lambda m: f'{m.group(1)} {m.group(2)}', text)
def expand_units(text: str) -> str:
"""
Expand common measurement units glued to numbers.
Examples:
"100km" β "one hundred kilometers"
"50kg" β "fifty kilograms"
"25Β°C" β "twenty-five degrees Celsius"
"5GB" β "five gigabytes"
"""
_unit_map = {
'km': 'kilometers',
'kg': 'kilograms',
'mg': 'milligrams',
'ml': 'milliliters',
'gb': 'gigabytes',
'mb': 'megabytes',
'kb': 'kilobytes',
'tb': 'terabytes',
'hz': 'hertz',
'khz': 'kilohertz',
'mhz': 'megahertz',
'ghz': 'gigahertz',
'mph': 'miles per hour',
'kph': 'kilometers per hour',
'ms': 'milliseconds',
'ns': 'nanoseconds',
'Β΅s': 'microseconds',
'Β°c': 'degrees Celsius',
'cΒ°': 'degrees Celsius',
'Β°f': 'degrees Fahrenheit',
'fΒ°': 'degrees Fahrenheit',
}
def _replace(m: re.Match) -> str:
raw = m.group(1)
unit = m.group(2).lower()
expanded = _unit_map.get(unit, m.group(2))
num = float_to_words(float(raw)) if '.' in raw else number_to_words(int(raw))
return f'{num} {expanded}'
return _RE_UNIT.sub(_replace, text)
def expand_roman_numerals(text: str, context_words: bool = True) -> str:
"""
Expand Roman numerals that appear as standalone tokens (optionally
only when preceded by a title-like word to avoid false positives).
Examples:
"World War II" β "World War two"
"Chapter IV" β "Chapter four"
"Louis XIV" β "Louis fourteen"
"mix I with V" β left unchanged (ambiguous single letters)
"""
_TITLE_WORDS = re.compile(
r'\b(war|chapter|part|volume|act|scene|book|section|article|'
r'king|queen|pope|louis|henry|edward|george|william|james|'
r'phase|round|level|stage|class|type|version|episode|season)\b',
re.IGNORECASE,
)
def _replace(m: re.Match) -> str:
roman = m.group(0)
if not roman.strip():
return roman
# Skip single ambiguous letters (I, V, X) unless context present
if len(roman) == 1 and roman in 'IVX':
# Only expand if preceded by a title word
start = m.start()
preceding = text[max(0, start - 30) : start]
if not _TITLE_WORDS.search(preceding):
return roman
try:
val = roman_to_int(roman)
if val == 0:
return roman
return number_to_words(val)
except Exception:
return roman
return _RE_ROMAN.sub(_replace, text)
def normalize_leading_decimals(text: str) -> str:
"""
Normalise bare leading-decimal floats so the number pipeline handles them.
Examples:
".5 teaspoons" β "0.5 teaspoons"
"-.25 adjustment" β "-0.25 adjustment"
"""
# Handle -.5 β -0.5 and .5 β 0.5
text = re.sub(r'(?<!\d)(-)\.([\d])', r'\g<1>0.\2', text)
return _RE_LEAD_DEC.sub(r'0.\1', text)
def expand_scientific_notation(text: str) -> str:
"""
Expand scientific-notation numbers to spoken form.
Examples:
"1e-4" β "one times ten to the negative four"
"2.5e10" β "two point five times ten to the ten"
"6.022E23"β "six point zero two two times ten to the twenty three"
"""
def _replace(m: re.Match) -> str:
coeff_raw = m.group(1)
exp = int(m.group(2))
coeff_words = (
float_to_words(coeff_raw) if '.' in coeff_raw else number_to_words(int(coeff_raw))
)
exp_words = number_to_words(abs(exp))
sign = 'negative ' if exp < 0 else ''
return f'{coeff_words} times ten to the {sign}{exp_words}'
return _RE_SCI.sub(_replace, text)
def expand_scale_suffixes(text: str) -> str:
"""
Expand standalone uppercase scale suffixes attached to numbers.
Examples:
"7B parameters" β "seven billion parameters"
"340M model" β "three hundred forty million model"
"1.5K salary" β "one point five thousand salary"
"$100K budget" β "$100K budget" (currency handled upstream)
"""
_map = {'K': 'thousand', 'M': 'million', 'B': 'billion', 'T': 'trillion'}
def _replace(m: re.Match) -> str:
raw = m.group(1)
suffix = m.group(2)
scale_word = _map.get(suffix, suffix)
num = float_to_words(raw) if '.' in raw else number_to_words(int(raw))
return f'{num} {scale_word}'
return _RE_SCALE.sub(_replace, text)
def expand_fractions(text: str) -> str:
"""
Expand simple numeric fractions.
Examples:
"1/2 cup" β "one half cup"
"3/4 mile" β "three quarters mile"
"2/3 done" β "two thirds done"
"5/8 inch" β "five eighths inch"
"""
def _replace(m: re.Match) -> str:
num = int(m.group(1))
den = int(m.group(2))
if den == 0:
return m.group()
num_words = number_to_words(num)
if den == 2:
denom_word = 'half' if num == 1 else 'halves'
elif den == 4:
denom_word = 'quarter' if num == 1 else 'quarters'
else:
denom_word = _ordinal_suffix(den)
if num != 1:
denom_word += 's'
return f'{num_words} {denom_word}'
return _RE_FRACTION.sub(_replace, text)
def expand_decades(text: str) -> str:
"""
Expand decade expressions to words.
Examples:
"the 80s" β "the eighties"
"the 1980s" β "the nineteen eighties"
"the 2020s" β "the twenty twenties"
"'90s music" β "nineties music"
"""
_decade_map = {
0: 'hundreds',
1: 'tens',
2: 'twenties',
3: 'thirties',
4: 'forties',
5: 'fifties',
6: 'sixties',
7: 'seventies',
8: 'eighties',
9: 'nineties',
}
def _replace(m: re.Match) -> str:
base = int(m.group(1)) # e.g. 8 for "80s", 198 for "1980s"
decade_digit = base % 10
decade_word = _decade_map.get(decade_digit, '')
if base < 10:
return decade_word
century_part = base // 10 # e.g. 19 for 198
return f'{number_to_words(century_part)} {decade_word}'
return _RE_DECADE.sub(_replace, text)
def expand_ip_addresses(text: str) -> str:
"""
Expand IPv4 addresses to spoken digits per octet.
Examples:
"192.168.1.1" β "one nine two dot one six eight dot one dot one"
"10.0.0.1" β "one zero dot zero dot zero dot one"
"""
_d = {
'0': 'zero',
'1': 'one',
'2': 'two',
'3': 'three',
'4': 'four',
'5': 'five',
'6': 'six',
'7': 'seven',
'8': 'eight',
'9': 'nine',
}
def _octet(s: str) -> str:
return ' '.join(_d[c] for c in s)
def _replace(m: re.Match) -> str:
return ' dot '.join(_octet(g) for g in m.groups())
return re.sub(r'\b(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\b', _replace, text)
def expand_phone_numbers(text: str) -> str:
"""
Expand US phone numbers to spoken digits before range expansion claims the hyphens.
Examples:
"555-1234" β "five five five one two three four"
"555-123-4567" β "five five five one two three four five six seven"
"1-800-555-0199" β "one eight zero zero five five five zero one nine nine"
"""
_d = {
'0': 'zero',
'1': 'one',
'2': 'two',
'3': 'three',
'4': 'four',
'5': 'five',
'6': 'six',
'7': 'seven',
'8': 'eight',
'9': 'nine',
}
def _digits(s: str) -> str:
return ' '.join(_d[c] for c in s)
def _join(*groups) -> str:
return ' '.join(_digits(g) for g in groups)
# Match longest pattern first to avoid partial matches
# 11-digit: 1-800-555-0199
text = re.sub(
r'(?<!\d-)(?<!\d)\b(\d{1,2})-(\d{3})-(\d{3})-(\d{4})\b(?!-\d)',
lambda m: _join(*m.groups()),
text,
)
# 10-digit: 555-123-4567
text = re.sub(
r'(?<!\d-)(?<!\d)\b(\d{3})-(\d{3})-(\d{4})\b(?!-\d)', lambda m: _join(*m.groups()), text
)
# 7-digit local: 555-1234 (not preceded or followed by digit-hyphen to avoid sub-matching)
text = re.sub(r'(?<!\d-)\b(\d{3})-(\d{4})\b(?!-\d)', lambda m: _join(*m.groups()), text)
return text
def expand_months(text: str) -> str:
"""
Expands Jan, Feb, etc. to January, February.
Only triggers if the abbreviation is likely a date.
"""
def _replace(m: re.Match) -> str:
return _MONTH_MAP.get(m.group(1), m.group(1))
# 1. Standard abbreviations
text = _RE_MONTHS.sub(_replace, text)
# 2. May (Special case: only if followed by a digit)
text = _RE_MAY.sub('May', text) # Essentially just ensuring it's treated as a word
return text
# βββββββββββββββββββββββββββββββββββββββββββββ
# Core preprocessing functions
# βββββββββββββββββββββββββββββββββββββββββββββ
def replace_numbers(text: str, replace_floats: bool = True) -> str:
"""
Replace all numeric tokens with their word equivalents.
Examples:
"There are 1200 students" β "There are twelve hundred students"
"Pi is 3.14" β "Pi is three point one four"
"gpt-3 rocks" β "gpt-3 rocks" (hyphen not treated as minus)
"""
def _replace(m: re.Match) -> str:
raw = m.group().replace(',', '')
try:
if '.' in raw and replace_floats:
# Pass raw string so trailing zeros are preserved ("1.50" β "one point five zero")
return float_to_words(raw)
else:
return number_to_words(int(float(raw)))
except (ValueError, OverflowError):
return m.group()
return _RE_NUMBER.sub(_replace, text)
def to_lowercase(text: str) -> str:
"""Convert text to lowercase."""
return text.lower()
def remove_urls(text: str, replacement: str = '') -> str:
"""Remove URLs from text."""
return _RE_URL.sub(replacement, text).strip()
def remove_emails(text: str, replacement: str = '') -> str:
"""Remove email addresses from text."""
return _RE_EMAIL.sub(replacement, text).strip()
def remove_html_tags(text: str) -> str:
"""Strip HTML tags from text."""
return _RE_HTML.sub(' ', text)
def remove_hashtags(text: str, replacement: str = '') -> str:
"""Remove hashtags (e.g. #NLP) from text."""
return _RE_HASHTAG.sub(replacement, text)
def remove_mentions(text: str, replacement: str = '') -> str:
"""Remove @mentions from text."""
return _RE_MENTION.sub(replacement, text)
def remove_punctuation(text: str) -> str:
"""Remove all punctuation characters."""
return _RE_PUNCT.sub(' ', text)
def remove_extra_whitespace(text: str) -> str:
"""Collapse multiple whitespace characters into a single space and strip ends."""
return _RE_SPACES.sub(' ', text).strip()
def normalize_unicode(text: str, form: str = 'NFC') -> str:
"""Normalize unicode characters (NFC, NFD, NFKC, or NFKD)."""
return unicodedata.normalize(form, text)
def remove_accents(text: str) -> str:
"""Remove diacritical marks (accents) from characters."""
nfkd = unicodedata.normalize('NFD', text)
return ''.join(c for c in nfkd if unicodedata.category(c) != 'Mn')
def expand_contractions(text: str) -> str:
"""
Expand common English contractions.
Examples:
"don't" β "do not"
"they're" β "they are"
"I've" β "I have"
"""
contractions = {
r"\bcan't\b": 'cannot',
r"\bwon't\b": 'will not',
r"\bshan't\b": 'shall not',
r"\bain't\b": 'is not',
r"\blet's\b": 'let us',
r"\b(\w+)n't\b": r'\1 not',
r"\b(\w+)'re\b": r'\1 are',
r"\b(\w+)'ve\b": r'\1 have',
r"\b(\w+)'ll\b": r'\1 will',
r"\b(\w+)'d\b": r'\1 would',
r"\b(\w+)'m\b": r'\1 am',
r"\bit's\b": 'it is',
}
for pattern, replacement in contractions.items():
text = re.sub(pattern, replacement, text, flags=re.IGNORECASE)
return text
def remove_stopwords(text: str, stopwords: set | None = None) -> str:
"""
Remove stopwords from text.
Args:
stopwords: Set of words to remove. Uses a built-in English set if None.
"""
if stopwords is None:
stopwords = {
'a',
'an',
'the',
'and',
'or',
'but',
'in',
'on',
'at',
'to',
'for',
'of',
'with',
'by',
'from',
'is',
'was',
'are',
'were',
'be',
'been',
'being',
'have',
'has',
'had',
'do',
'does',
'did',
'will',
'would',
'could',
'should',
'may',
'might',
'this',
'that',
'these',
'those',
'it',
'its',
'i',
'me',
'my',
'we',
'our',
'you',
'your',
'he',
'she',
'him',
'her',
'they',
'them',
'their',
}
tokens = text.split()
return ' '.join(t for t in tokens if t.lower() not in stopwords)
# βββββββββββββββββββββββββββββββββββββββββββββ
# Pipeline helper
# βββββββββββββββββββββββββββββββββββββββββββββ
class TextPreprocessor:
"""
Configurable preprocessing pipeline.
Usage:
pp = TextPreprocessor(
lowercase=True,
replace_numbers=True,
remove_urls=True,
remove_html=True,
remove_punctuation=True,
)
clean = pp("GPT-3 costs $0.002 per token β 50% cheaper than before!")
# β "gpt three costs zero dollars and zero point two cents per token fifty percent cheaper than before"
"""
def __init__(
self,
lowercase: bool = True,
replace_numbers: bool = True,
replace_floats: bool = True,
expand_newlines: bool = True,
expand_tilde: bool = True,
expand_abbreviations: bool = True,
expand_symbols: bool = True,
expand_contractions: bool = True,
expand_model_names: bool = True,
expand_ordinals: bool = True,
expand_percentages: bool = True,
expand_currency: bool = True,
expand_time: bool = True,
expand_ranges: bool = True,
expand_units: bool = True,
expand_scale_suffixes: bool = True,
expand_scientific_notation: bool = True,
expand_fractions: bool = True,
expand_decades: bool = True,
expand_phone_numbers: bool = True,
expand_ip_addresses: bool = True,
normalize_leading_decimals: bool = True,
expand_roman_numerals: bool = False,
remove_urls: bool = True,
remove_emails: bool = True,
remove_html: bool = True,
remove_hashtags: bool = False,
remove_mentions: bool = False,
remove_punctuation: bool = True,
remove_stopwords: bool = False,
stopwords: set | None = None,
normalize_unicode: bool = True,
remove_accents: bool = False,
remove_extra_whitespace: bool = True,
):
self.config = {k: v for k, v in locals().items() if k != 'self'}
self._stopwords = stopwords
def __call__(self, text: str) -> str:
return self.process(text)
def process(self, text: str) -> str:
cfg = self.config
if cfg.get('expand_abbreviations'):
text = expand_abbreviations(text)
text = expand_months(text)
if cfg.get('expand_newlines'):
text = expand_newlines(text)
if cfg.get('expand_symbols'):
text = expand_symbols(text)
if cfg.get('expand_tilde'):
text = expand_tilde(text)
if cfg['normalize_unicode']:
text = normalize_unicode(text)
if cfg['remove_html']:
text = remove_html_tags(text)
if cfg['remove_urls']:
text = remove_urls(text)
if cfg['remove_emails']:
text = remove_emails(text)
if cfg['remove_hashtags']:
text = remove_hashtags(text)
if cfg['remove_mentions']:
text = remove_mentions(text)
if cfg['expand_contractions']:
text = expand_contractions(text)
# IP addresses before normalize_leading_decimals (IPs contain dots before digits)
if cfg['expand_ip_addresses']:
text = expand_ip_addresses(text)
# Normalise bare leading decimals early so downstream regexes see "0.5" not ".5"
if cfg['normalize_leading_decimals']:
text = normalize_leading_decimals(text)
# Expand special forms before generic number replacement
if cfg['expand_currency']:
text = expand_currency(text)
if cfg['expand_percentages']:
text = expand_percentages(text)
# Scientific notation before model-name expansion (e.g. "1e-4" contains "e-4")
if cfg['expand_scientific_notation']:
text = expand_scientific_notation(text)
if cfg['expand_time']:
text = expand_time(text)
if cfg['expand_ordinals']:
text = expand_ordinals(text)
if cfg['expand_units']:
text = expand_units(text)
# Scale suffixes after units (units handles "MB"/"GB"; this handles bare "B"/"M")
if cfg['expand_scale_suffixes']:
text = expand_scale_suffixes(text)
if cfg['expand_fractions']:
text = expand_fractions(text)
if cfg['expand_decades']:
text = expand_decades(text)
# Phone numbers before ranges, otherwise NNN-NNNN is treated as a range
if cfg['expand_phone_numbers']:
text = expand_phone_numbers(text)
if cfg['expand_ranges']:
text = expand_ranges(text)
if cfg['expand_model_names']:
text = expand_model_names(text)
if cfg['expand_roman_numerals']:
text = expand_roman_numerals(text)
if cfg['replace_numbers']:
text = replace_numbers(text, replace_floats=cfg['replace_floats'])
if cfg['remove_accents']:
text = remove_accents(text)
if cfg['remove_punctuation']:
text = remove_punctuation(text)
if cfg['lowercase']:
text = to_lowercase(text)
if cfg['remove_stopwords']:
text = remove_stopwords(text, self._stopwords)
if cfg['remove_extra_whitespace']:
text = remove_extra_whitespace(text)
return text
|