"""Text -> the words to say and the punctuation marks between them. Everything that is not a plain word is turned into words here, by rule: numbers, ordinals, money, dates, times, telephone numbers, units, abbreviations, initialisms, symbols, chat spellings, version numbers, codes and reference numbers, web addresses and file names. Emoticons and emoji are dropped: they are neither read nor a pause. A pronunciation can be given inline, ``{HH AH0 L OW1}`` or ``{h ə l ˈoʊ}``, alone or right after the word it replaces. Braces that hold anything else are read as text. The result is a list of ``Word`` and ``Punct`` tokens. Hand-written tables in ``data/`` hold the abbreviations, units, chat spellings and file endings. """ import re from typing import NamedTuple import unicodedata from .symbols import PUNCTUATION from . import tables from .convert import convert, from_text, is_arpabet class Word(NamedTuple): text: str # letters and apostrophes, lower case attach: bool = False # continues the previous word (hyphenated compound, parts of one token) raw: str = "" # as written spell: bool = False # read letter by letter phones: tuple = None # pronunciation given inline by the user class Punct(NamedTuple): mark: str # Private-use characters that carry decisions through the rewriting of the text: # letters to be named sit between _SPELL and _END, and an inline pronunciation # is a single character (its number added to _INLINE_BASE) between two _INLINE. _SPELL, _END, _INLINE, _INLINE_BASE = "", "", "", 0xE100 _ONES = ("zero one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen " "seventeen eighteen nineteen").split() _TENS = "twenty thirty forty fifty sixty seventy eighty ninety".split() _SCALES = ("thousand", "million", "billion", "trillion", "quadrillion", "quintillion") _NUMBER_WORDS = frozenset(_ONES) | frozenset(_TENS) | {"hundred"} _ORDINAL = {"one": "first", "two": "second", "three": "third", "five": "fifth", "eight": "eighth", "nine": "ninth", "twelve": "twelfth"} _MONTHS = "January February March April May June July August September October November December".split() _MONTH_NUMBER = {name.lower()[:3]: index + 1 for index, name in enumerate(_MONTHS)} _MONTH_PATTERN = "|".join(_MONTHS) + "|Jan|Feb|Mar|Apr|Jun|Jul|Aug|Sept|Sep|Oct|Nov|Dec" _DAY_NAMES = {"mon": "Monday", "tue": "Tuesday", "wed": "Wednesday", "thu": "Thursday", "fri": "Friday", "sat": "Saturday", "sun": "Sunday"} _DAY_PATTERN = "Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday|Thurs|Thur|Tues|Mon|Tue|Wed|Thu|Fri|Sat|Sun" _CURRENCY = {"$": ("dollar", "dollars", "cent", "cents"), "£": ("pound", "pounds", "penny", "pence"), "€": ("euro", "euros", "cent", "cents"), "¥": ("yen", "yen", "", ""), "₹": ("rupee", "rupees", "paisa", "paise")} _CURRENCY.update({"USD": _CURRENCY["$"], "EUR": _CURRENCY["€"], "GBP": _CURRENCY["£"], "JPY": _CURRENCY["¥"], "INR": _CURRENCY["₹"], "CHF": ("Swiss franc", "Swiss francs", "centime", "centimes"), "CAD": ("Canadian dollar", "Canadian dollars", "cent", "cents"), "AUD": ("Australian dollar", "Australian dollars", "cent", "cents"), "CNY": ("yuan", "yuan", "", "")}) _CURRENCY_CODES = "USD|EUR|GBP|JPY|INR|CHF|CAD|AUD|CNY" _SCALE_LETTER = {"k": "thousand", "m": "million", "b": "billion", "bn": "billion", "t": "trillion"} _FRACTION = {2: ("half", "halves"), 4: ("quarter", "quarters")} _SYMBOL_WORDS = {"&": "and", "@": "at", "+": "plus", "=": "equals", "%": "percent", "×": "times", "÷": "divided by", "±": "plus or minus", "©": "copyright", "§": "section", "°": "degrees", "≥": "greater than or equal to", "≤": "less than or equal to", "≠": "not equal to", "≈": "approximately", "∞": "infinity", "√": "square root of", "π": "pi"} _COMPARISONS = {">=": "greater than or equal to", "<=": "less than or equal to", "!=": "not equal to"} # Capitals read letter by letter although they also spell a word. _INITIALISMS = frozenset("US IT AI ID UN EU UK LA AD BC AM PM OS PE PA MA LED SAT RIP ER RAF SAS UPS".split()) # Two capitals that are a word to say, not letters to name ("OH", "HI"); any other pair # the dictionary happens to list is named: "MS" (not "Ms"), "CT", "ET", "IB". # "pp. 12-15": the range is already words when the abbreviations are read. # Words after an amount that show it does not describe a noun: "the $500 will be returned". _NOT_A_NOUN = frozenset("will would is was are were has had have can could should may might must shall did does do " "to for in on at of from and or but nor that which who whom whose i you he she we they it me him " "them us back each a an the per now today yesterday tomorrow already more less extra plus off " "only as by with so then if when every monthly weekly yearly annually daily later instead too again " "upfront up down out over under into onto than before after since until while because just still " "also even here there this these those what where how why worth apiece total altogether " "is being been not no cash".split()) _PAGE_LIKE = frozenset("p pp fig vol ch sec".split()) _CAPITAL_PAIRS = frozenset("OK OH HI YO UH UM AH HA OW OX EH OI HM MM SH EX".split()) # Sounds written without a vowel. "Ss" among them, or alone, is a hiss ("Sh! Ss! Tsk!"). _SOUNDS = frozenset("sh shh ss zz hm hmm mm tsk psst pfft hmph psh pssh".split()) # A comparison between two values is said with "is": "67 > 66" is "sixty seven # is greater than sixty six", "see if b>a" is "see if B is greater than A". A # value is a figure, a single letter or a closing bracket, with the same space # (or none) on both sides of the sign. "Is" is left out where the comparison # only describes the value: "for n > 4", "any q > 0", "an integer k ≥ 2". The # mark below is put after the first value and stays there while the figures # around it are turned into words. _IS = chr(0xE006) _COMPARED = re.compile(r"(?P[ \t]?)(?P>=|<=|!=|[<>≥≤≠])(?P=gap)") _VALUE_BEFORE = re.compile(r"(?:(?a", "k = k + 1", "(a)". After "=" or a spaced sign the letter # must not open a phrase ("mode = a set of notes"). _SIGN = r"[<>+*/^≤≥≠≈×÷±]" _SPACED_SIGN = r" [<>+*≤≥≠≈×÷±] " _NO_PHRASE = r"(?![\w'’-]|\s+(?!(?:and|or|then|if|when|where|while|for|is|are|was|but|so|in|to)\b)[A-Za-z])" _LETTER_PLACES = tuple(re.compile(pattern) for pattern in ( r"(?[Aakur])(?=" + _IS + r"?\s?[<>!=]?=)", # a=10, a = 10, a >= b r"[<>!=]?=\s?(?P[Aakur])" + _NO_PHRASE, # x=a, c = a + b r"(?[Aakur])(?=" + _IS + r"?" + _SIGN + r"(?:[\w($]|\.\d))", # a>b, a+b, a/b, a^2 r"(?<=[\w)" + _IS + r"]" + _SIGN + r")(?[Aakur])(?![\w'’-])", # b>a, 1/a; not "w/a friend" r"(?[Aakur])(?=" + _IS + r"?" + _SPACED_SIGN + r"(?:[A-Za-z]|\d[\d.,]*)(?![\w'’]))", # a > b r"(?[Aakur])" + _NO_PHRASE, # b > a r"(?<=\()(?P[Aakur])(?=\)|, )", # (a), f(a, b) r"(?<=, )(?P[Aakur])(?=\))", # f(b, a) r"(?:(?<=\s)|^)(?P[akur])(?=\)\s)", # a) the first point )) # "", "

", "", "Vec", "map": angle brackets that # enclose something. The "<" and ">" that are left may compare two things. _ANGLES = re.compile(r"(?(){}+*;|\n]*>" r"|(?<=[\w" + _END + _IS + r"])<[^<>(){}+*;|\n]{1,80}>(?![\w$])" r"|") _ANGLE_MARK = chr(0xE005) def _without_angles(text): """``text`` with the angle brackets that enclose something replaced by a mark, one for one, the inner ones first.""" while "<" in text and ">" in text: marked = _ANGLES.sub(lambda match: _ANGLE_MARK + match.group(0)[1:-1] + _ANGLE_MARK, text) if marked == text: break text = marked return text # "<" or ">" with no space on either side: "b>a", "x<5", "p<.05". It is read as a # comparison when a figure stands on one side, or a single letter or a bracket on # both. "File>Open" and "M>Train" are not comparisons. _JOINED_COMPARISON = re.compile(r"(?<=[\w)" + _END + _IS + r"])[<>](?=[\w(" + _SPELL + r"]|\.\d| minus \d)") _FIGURE_BEFORE = re.compile(r"(?<]+|[)(\]\[][-^']?[:;=]|<3+|_<|T_T|\\o/|:'[()])(?![A-Za-z0-9])" r"|[:;]['^-]?[)(]+" r"|(?\d{1,3}(?:,\d{3})+|\d+)(?:\.(?P\d+))?" _MONEY = re.compile(r"(?:(?-)\s?)?(?P[$£€¥₹]|(?-)?" + _AMOUNT + r"(?:\s?(?Pthousand|million|billion|trillion|bn|[kKmMbB])(?![A-Za-z]))?") # A currency written after the amount: "20 €", "20€", "12.50 EUR". Those who write # it there often part the thousands with spaces: "750 000 $". _SIGN_AFTER = r"[$£€¥₹](?![\w$£€¥₹])|(?:" + _CURRENCY_CODES + r")(?![A-Za-z])" _MONEY_AFTER = re.compile(r"(?:(?-)\s?)?(?\d{1,3}(?:,\d{3})+|[1-9]\d{0,2}(?: \d{3})+|\d+)(?:\.(?P\d+))?" r"\s?(?P" + _SIGN_AFTER + r")") _RANGE_BEFORE_SIGN = re.compile(r"(?][^$\n]{0,80}" r"|(?=\s)[^$\n]{0,80}[=^_\\{}+*<>][^$\n]{0,80})\d\s?\$(?![\w$])") _VULGAR = re.compile(r"(?(?:https?://|www\.)[^\s<>\"']+)" r"|(?P[\w.+-]+@[\w-]+(?:\.[\w-]+)+)" r"|(?P(?/?(?:[\w~-]+/)+)?(?P[\w-]+(?:\.[\w-]+)*)\.(?P[A-Za-z][A-Za-z0-9]{0,9})" r"(?![\w-])(?P/[^\s<>\"']*)?)") _DAY_RANGE = re.compile(r"\b(" + _DAY_PATTERN + "|" + _MONTH_PATTERN + r")\b(\.?)\s?[-–]\s?(" + _DAY_PATTERN + "|" + _MONTH_PATTERN + r")\b(\.?)") _RATIO = re.compile(r"(?\+)?(?:(?P[A-Z]{2}) )?" r"(?P[A-Z0-9]*\d[A-Z0-9]*(?: [A-Z0-9]*\d[A-Z0-9]*)+)" r"(?(lead)(?: (?P[A-Z]{2})(?![\w]))?)" r"(?![\w]|[.,:/-]\d|\s?[%°])") _GLUED = re.compile(r"(? ``"one thousand two hundred thirty four"``.""" if number < 0: return "minus " + cardinal(-number) if number < 20: return _ONES[number] if number < 100: tens, ones = divmod(number, 10) return _TENS[tens - 2] + (" " + _ONES[ones] if ones else "") if number < 1000: hundreds, rest = divmod(number, 100) return _ONES[hundreds] + " hundred" + (" " + cardinal(rest) if rest else "") if number >= 1000 ** (len(_SCALES) + 1): return digits(str(number)) parts = [] for power in range(len(_SCALES), 0, -1): count, number = divmod(number, 1000 ** power) if count: parts.append(cardinal(count) + " " + _SCALES[power - 1]) if number: parts.append(cardinal(number)) return " ".join(parts) def ordinal(number): words = cardinal(number).split() last = words[-1] words[-1] = _ORDINAL.get(last) or (last[:-1] + "ieth" if last.endswith("y") else last + "th") return " ".join(words) def digits(text, zero="zero"): return " ".join(zero if c == "0" else _ONES[int(c)] for c in text if c.isdigit()) def year(number): """The way a year is said: 1984, 1905, 1900, 2005, 2026.""" if not 1000 <= number <= 2999: return cardinal(number) high, low = divmod(number, 100) if low == 0: return cardinal(number) if high % 10 == 0 else cardinal(high) + " hundred" if high == 20 and low < 10: return "two thousand " + _ONES[low] return cardinal(high) + (" oh " + _ONES[low] if low < 10 else " " + cardinal(low)) def pairs(number): """A number read two digits at a time, the way model numbers are: 4070 "forty seventy", 9001 "ninety oh one", 720 "seven twenty".""" high, low = divmod(number, 100) if not 100 <= number <= 9999 or (low == 0 and (high < 10 or high % 10 == 0)): return cardinal(number) if low == 0: return cardinal(high) + " hundred" return cardinal(high) + (" oh " + _ONES[low] if low < 10 else " " + cardinal(low)) def number_words(text): """An unsigned number as written: ``1,234``, ``3.14``, ``007``.""" whole, _, fraction = text.replace(",", "").partition(".") if fraction: return (cardinal(int(whole)) if whole else "") + " point " + digits(fraction) if len(whole) > 1 and whole.startswith("0") or len(whole) > 18: return digits(whole) return cardinal(int(whole)) def duration(hours, minutes, seconds): """``2:03:45`` as a length of time: "two hours, three minutes and forty five seconds".""" parts = [f"{cardinal(count)} {name if count == 1 else name + 's'}" for count, name in ((hours, "hour"), (minutes, "minute"), (seconds, "second")) if count] if len(parts) < 2: return parts[0] if parts else "zero seconds" return ", ".join(parts[:-1]) + " and " + parts[-1] def _plural_of(words): """Plural of a spelled number, for decades: "nineteen nineties".""" head, _, last = words.rpartition(" ") last = last[:-1] + "ies" if last.endswith("y") else last + "s" return (head + " " if head else "") + last def _fraction_words(top, bottom, article=False): """"three quarters", "one half"; after a whole number "a half", "an eighth".""" one, many = _FRACTION.get(bottom, (ordinal(bottom), ordinal(bottom) + "s")) if top != 1: return f"{cardinal(top)} {many}" return (("an " if one[0] in "aeiou" else "a ") if article else "one ") + one def _version_part(part): """One part of a version number: "4" four, "12" twelve, "03" zero three, "129" one two nine.""" return digits(part) if len(part) > 2 else number_words(part) def _minor(part): """The part after the point of a version: "12" twelve, "04" oh four.""" return digits(part, "oh") if part.startswith("0") else cardinal(int(part)) def _short_number(text): """A number in a code that is said as a number: one or two digits, or a round one (B-12, KX-300).""" if text.startswith("0"): return False return len(text) <= 2 or set(text[1:]) == {"0"} def _spelled(letters): return f"{_SPELL}{letters}{_END}" def _before(text, position, count=4): """The words just before ``position`` in the same clause, in lower case.""" clause = re.split(r"[.!?;…]\s|[()\"]", text[max(0, position - 80):position])[-1] return [word.lower() for word in re.findall(r"[A-Za-z]+", clause)][-count:] def _identifier(text, position): """What the words before a number say it is: ``"phone"``, ``"pin"``, ``"strong"`` (a number, an ID, a reference), ``"weak"`` (an order, a ticket), ``"enter"`` or ``None``.""" words = _before(text, position) if not words: return None if words[-1] in _PHONE_WORDS or (words[-1] in _PHONE_LINKS and any(word in _PHONE_WORDS for word in words[-3:-1])): return "phone" index = len(words) - 1 while index > 0 and words[index] in _LINK_WORDS: index -= 1 for kind, group in (("phone", _PHONE_WORDS), ("pin", _PIN_WORDS), ("strong", _STRONG_WORDS), ("weak", _WEAK_WORDS)): if words[index] in group: return kind return "enter" if words[-1] == "enter" else None def _decimal_comma(text): """"1,5" before a unit is one number: a comma with one or two digits after it does not part thousands. "1,250" is left as it is.""" return text.replace(",", ".") if re.fullmatch(r"\d+,\d{1,2}", text) else text def _address_digits(text): """Digits inside a file name or an address: 3, 2026, 0412.""" if len(text) <= 2 and not (len(text) == 2 and text[0] == "0"): return cardinal(int(text)) if len(text) == 4 and 1900 <= int(text) <= 2099: return year(int(text)) return digits(text) # ---- rewriting ------------------------------------------------------------- class Normalizer: def __init__(self, is_word=None): self.is_word = is_word or (lambda word: True) self.abbreviations = {columns[0]: (columns[1], columns[2]) for _, columns in tables.rows("abbreviations.txt")} # A unit marked "exact" is written in one letter case only: "m" is a meter, "M" is not. self.units, self.exact_units = {}, {} for _, columns in tables.rows("units.txt"): if len(columns) > 3 and columns[3] == "exact": self.exact_units[columns[0]] = (columns[1], columns[2]) else: self.units[columns[0].lower()] = (columns[1], columns[2]) # A chat spelling marked "alone" is read that way only as a reply by itself ("k", "k, thanks"). self.chat, self.replies = {}, set() for number, columns in tables.rows("chat.txt"): if len(columns) > 2 and columns[2:] != ["alone"]: raise ValueError(f"chat.txt line {number}: after the words to say there can only be \"alone\"") self.chat[columns[0]] = columns[1] if len(columns) > 1 else "-" if len(columns) > 2: self.replies.add(columns[0]) self.endings = tables.mapping("endings.txt") names = sorted({re.escape(unit) for unit in list(self.units) + list(self.exact_units)}, key=lambda name: (-len(name), name)) self._unit_names = "|".join(names) self._unit = re.compile(r"(? 2 or not cent): return f"{number_words(whole + '.' + fraction)} {many}" amount, change = int(whole), int((fraction or "0").ljust(2, "0")) parts = [] if amount or not change: parts.append(f"{cardinal(amount)} {one if amount == 1 else many}") if change and cent: parts.append(f"{cardinal(change)} {cent if change == 1 else cents}") return " and ".join(parts) @staticmethod def _continental(match): """"1.299,50" as "1,299.50". Four groups that could be an internet address are left alone.""" groups = match.group(1).split(".") if len(groups) == 4 and all(int(group) <= 255 for group in groups): return match.group(0) return ",".join(groups) + "." + match.group(2) @staticmethod def _before_noun(match): """True when an amount describes the noun after it: "a $5 fee", "the $3 million deal", "an $8bn contract". "the $500 will be", "a $5 or $10 fee" are not.""" before = re.search(r"(?:^|[\s(])(a|an|the|this|that|your|my|our|their|his|her|its)\s+$", match.string[:match.start()], re.IGNORECASE) after = re.match(r"\s+([a-z]+)\b(?!['’])", match.string[match.end():]) if not before or not after or after.group(1) in _NOT_A_NOUN: return False if before.group(1).lower() in ("a", "an"): return not after.group(1).endswith(("ed", "ly")) word = after.group(1) # "the $500 deposited", "your $50 remaining" are amounts return not word.endswith(("ed", "ing", "ly")) and (not word.endswith("s") or word.endswith("ss")) def _money(self, match): found = match.groupdict() whole = found["whole"].replace(",", "").replace(" ", "") words = self._amount(found["symbol"], whole, found["fraction"], found.get("scale"), self._before_noun(match)) return (" minus " if found.get("minus") or found.get("inner") else " ") + words + " " @staticmethod def _half_day(match, dotted, plain): """" A M" or " P M" as letters. A full stop that closes "p.m." is kept as a sentence end only when a new sentence follows.""" if not (dotted or plain): return "" following = match.string[match.end():].lstrip() first = re.match(r"[A-Za-z']+", following) closes = bool(dotted) and match.group(0).endswith(".") and ( not following or (following[0].isupper() and first is not None and first.group(0).lower() in _SENTENCE_STARTERS)) return f"{_SPELL}{(dotted or plain).upper()}M{_END}{' .' if closes else ''} " def _time(self, match): hour, minute, second = int(match.group(1)), int(match.group(2)), match.group(3) half = match.group(4) or match.group(5) before = _before(match.string, match.start(), 3) if second is not None and not half and not (before and before[-1] == "at"): return f" {duration(hour, minute, int(second))} " # 2:03:45 is a length of time if hour > 24 and second is None and not half: after = re.match(r"\s*([A-Za-z]+)", match.string[match.end():]) if set(before) & _DURATION_WORDS or (after and after.group(1).lower() in _DURATION_WORDS): return f" {duration(0, hour, minute)} " # 48:12 long return f" {cardinal(hour)} {'oh ' if minute < 10 else ''}{cardinal(minute)} " if hour > 24: return match.group(0) words = cardinal(hour) if minute == 0: if half: pass elif hour == 0: words = "midnight" elif hour > 12: words += " hundred" # 20:00 else: words += " o'clock" else: words += (" oh " if minute < 10 else " ") + cardinal(minute) if second: words += " and " + cardinal(int(second)) + (" second" if int(second) == 1 else " seconds") return f" {words} " + self._half_day(match, match.group(4), match.group(5)) def _dot_time(self, match): """"6.30" as a time of day where the words around say so: "at 7.15", "6.30 in the morning", "until 10.45 tonight". "at 6.25 percent", "at 7.15 each" stay figures.""" hour, minute = int(match.group(1)), int(match.group(2)) following = match.string[match.end():] if not 1 <= hour <= 23: return match.group(0) if re.match(r"\s?(?:in the (?:morning|afternoon|evening)|at night|tonight|this (?:morning|afternoon|evening)|o'clock)\b", following, re.IGNORECASE): return f"{hour}:{match.group(2)}" before = _before(match.string, match.start(), 1) if before and before[-1] in ("at", "until", "till") and minute % 5 == 0 \ and re.match(r"\s*(?:$|[.,;:!?)—]|(?:and|or|to|on|in|the|we|i|you|he|she|they|it|tomorrow|today|tonight|sharp|every|when)\b)", following, re.IGNORECASE): return f"{hour}:{match.group(2)}" return match.group(0) @staticmethod def _short_years(match): """"1939-45" is nineteen thirty nine to forty five; "1984-12" (lower) is no range of years.""" first, last = int(match.group(1)), int(match.group(2)) if last <= first % 100: return f" {number_words(match.group(1))} to {number_words(match.group(2))} " return f" {year(first)} to {'oh ' + _ONES[last] if last < 10 else cardinal(last)} " def _dot_times(self, match): """"9.30 to 11.30" where the words before say it is a time of day.""" following = match.string[match.end():] if int(match.group(1)) > 23 or int(match.group(4)) > 23 or not set(_before(match.string, match.start())) & _CLOCK_WORDS \ or re.match(r"\s?(?:%|percent|million|billion|" + self._unit_names + r")(?![A-Za-z])", following, re.IGNORECASE): return match.group(0) return f"{match.group(1)}:{match.group(2)}{match.group(3)}{match.group(4)}:{match.group(5)}" def _ratio(self, match): """16:9, 3:1, 1:25,000. Two digits after the colon are a time unless the words say "ratio".""" first, second = match.group(1), match.group(2) if len(first) <= 2 and len(second) == 2 and second[0] in "012345": after = re.match(r"\s*([A-Za-z]+)", match.string[match.end():]) if not (set(_before(match.string, match.start(), 3)) & _RATIO_WORDS or (after and after.group(1).lower() in _RATIO_WORDS)): return match.group(0) return f" {number_words(first)} to {number_words(second)} " def _date(self, month, day, year_text=None, the=False, plain=False): if not 1 <= month <= 12 or not 1 <= day <= 31: return None if plain: # "March 20" as written: "March twenty" ("March 20th" and "March 20, 2026" read the ordinal) words = f"{_MONTHS[month - 1]} {cardinal(day)}" else: words = f"the {ordinal(day)} of {_MONTHS[month - 1]}" if the else f"{_MONTHS[month - 1]} {ordinal(day)}" if year_text: words += " " + (year(int(year_text)) if len(year_text) == 4 else ("oh " if int(year_text) < 10 else "") + cardinal(int(year_text))) return f" {words} " def _slash_date(self, match): first, second = int(match.group(1)), int(match.group(2)) if first > 12 >= second or (self.day_first and second <= 12): first, second = second, first # day first return self._date(first, second, match.group(3)) or match.group(0) def _dot_date(self, match): """15.11.2026: dates written with full stops put the day first.""" day, month = int(match.group(1)), int(match.group(2)) if month > 12 >= day: day, month = month, day return self._date(month, day, match.group(3)) or match.group(0) def _day_month(self, match): """"22 February", "5 Jan. 2026". The stop after a short month name is dropped; any other one ends the sentence.""" month = match.group(2) spoken = self._date(_MONTH_NUMBER[month.lower()[:3]], int(match.group(1)), match.group(4), the=True) if spoken is None: return match.group(0) return spoken + (". " if match.group(3) and month in _MONTHS else "") def _days_of_month(self, match): """"3-17 November" and "November 3-17".""" month_first = match.group(1)[0].isalpha() month, first, second = (match.group(1), match.group(2), match.group(3)) if month_first else \ (match.group(3), match.group(1), match.group(2)) first, second = int(first), int(second) if not (1 <= first <= 31 and 1 <= second <= 31): return match.group(0) name = _MONTHS[_MONTH_NUMBER[month.lower()[:3]] - 1] words = f"{name} {ordinal(first)} to {ordinal(second)}" if month_first else \ f"the {ordinal(first)} to the {ordinal(second)} of {name}" return f" {words}{' ' + year(int(match.group(4))) if match.group(4) else ''} " def _day_range(self, match): """"Mon.-Fri.", "Jan-Mar": from one day or month to another.""" names = [] for name in (match.group(1), match.group(3)): key = name.lower() names.append(self.abbreviations[key][1] if key in self.abbreviations else _DAY_NAMES.get(key, name)) following = match.string[match.end():].lstrip() first = re.match(r"[A-Za-z']+", following) ends = bool(match.group(4)) and (not following or (following[0].isupper() and first is not None and first.group(0).lower() in _SENTENCE_STARTERS)) return f"{names[0]} to {names[1]}{' .' if ends else ''}" def _short_date(self, match): """"14/03", "12/10" as a day of the year, or ``None`` when it is a fraction.""" first_text, second_text = match.group(1), match.group(2) if len(first_text) > 2 or len(second_text) > 2: return None first, second = int(first_text), int(second_text) before = _before(match.string, match.start()) if 1 <= first <= 12 and 20 <= second <= 49 and "card" in before: return f" {_MONTHS[first - 1]} {year(2000 + second)} " # a card that expires 08/27 last = before[-1] if before else "" classic = first < second and second in _CLASSIC_FRACTIONS # 1/2, 3/4, 7/10 zero = len(first_text) == 2 and first_text[0] == "0" or len(second_text) == 2 and second_text[0] == "0" if not (zero or (12 < first <= 31 and second <= 12 and (first, second) != (24, 7)) or (last in _DATE_BEFORE and (not classic or last == "on")) or (self.dated and not classic and first != second)): return None if first > 12 or (self.day_first and second <= 12): first, second = second, first # day first return self._date(first, second) def _fraction(self, match): spoken = self._short_date(match) if spoken: return spoken top, bottom = int(match.group(1)), int(match.group(2)) if (top, bottom) == (24, 7) or top == bottom: return f" {cardinal(top)} {cardinal(bottom)} " # twenty four seven, fifty fifty if not 0 < top < 100 or not 1 < bottom <= 100: return f" {cardinal(top)} over {cardinal(bottom)} " return f" {_fraction_words(top, bottom)} " def _mixed_fraction(self, match): """"2 1/2": two and a half.""" top, bottom = int(match.group(2)), int(match.group(3)) if not 0 < top < bottom: return match.group(0) return f" {number_words(match.group(1))} and {_fraction_words(top, bottom, article=True)} " def _vulgar(self, match): """"½" and "1½". Runs before the text is normalised, which would turn "1½" into "11/2".""" top, bottom = (int(part) for part in unicodedata.normalize("NFKC", match.group(2)).split("⁄")) if match.group(1) is None: return f" {top}/{bottom} " return f" {cardinal(int(match.group(1)))} and {_fraction_words(top, bottom, article=True)} " def _phone(self, match): groups = [g for g in match.groups() if g] return " " + ", ".join("eight hundred" if g == "800" else digits(g, "oh") for g in groups) + " " def _measure(self, match): unit = match.group(3) names = self.exact_units.get(unit) or self.units.get(unit.lower()) if names is None or (len(unit) == 1 and match.string[match.end(3):match.end(3) + 1] in ("-", "'", "/")): return match.group(0) one, many = names value, upper = _decimal_comma(match.group(1)), match.group(2) and _decimal_comma(match.group(2)) if upper: return f" {number_words(value)} to {number_words(upper)} {many} " return f" {number_words(value)} {one if value in ('1', '1.0') else many} " def _square(self, match): """"120 m²", "3 m3": square and cubic measures.""" unit = match.group(2) one, many = {"in": ("inch", "inches")}.get(unit) or self.exact_units.get(unit) or self.units[unit] kind = "square" if match.group(3) in "²2" else "cubic" value = _decimal_comma(match.group(1)) return f" {number_words(value)} {kind} {one if value in ('1', '1.0') else many} " def _counted(self, text, end): """True when a plural noun follows a number: it is then an amount, not a reference.""" after = re.match(r"\s*([A-Za-z]+)", text[end:]) if not after: return False word = after.group(1).lower() if word in ("people", "men", "women", "children", "feet", "miles"): return True return len(word) > 3 and word.endswith("s") and not word.endswith("ss") and word not in _NOT_COUNTED \ and self.is_word(word[:-1]) def _number(self, match, text): value = match.group(0) plain = value.replace(",", "") if "." not in plain and "," not in value and not plain.startswith("0") and len(plain) > 2: kind = _identifier(text, match.start()) limit = {"phone": 3, "pin": 3, "strong": 4, "enter": 4, "weak": 5}.get(kind, 7) if len(plain) >= limit and not (kind is None and plain.endswith("000")) \ and not (kind == "enter" and len(plain) == 4 and 1900 <= int(plain) <= 2099) \ and not (kind in (None, "phone", "weak") and self._counted(text, match.end())): return f" {digits(plain, 'oh' if kind == 'phone' else 'zero')} " # a reference, not an amount if "." not in plain and "," not in value and len(plain) == 4 and not plain.startswith("0"): number = int(plain) before = re.findall(r"[A-Za-z]+", text[:match.start()][-24:]) after = re.match(r"\s*([A-Za-z]+)", text[match.end():]) counted = bool(after) and re.search(r"(?:[^s]s|people|men|women|children|feet|miles)$", after.group(1).lower()) is not None dated = bool(before) and (before[-1].lower() in _YEAR_CONTEXT or before[-1].lower()[:3] in _MONTH_NUMBER) model = re.search(r"(? 3 or any(self.is_word(lower + ending) for ending in ("s", "es", "ed", "ing")) @staticmethod def _alnum(group, zero="zero"): """A group that mixes letters and digits: the letters are named, the digits read one by one.""" return " ".join(digits(run, zero) if run.isdigit() else _spelled(run) for run in re.findall(r"[A-Za-z]+|\d+", group)) @staticmethod def _digit_groups(groups, zero="zero"): """Groups of digits with a short break between them. Small numbers stay numbers: 9-1-1.""" if all(_short_number(group) and len(group) <= 2 for group in groups): return " , ".join(cardinal(int(group)) for group in groups) return " , ".join(digits(group, zero) for group in groups) def _code_words(self, groups, match): token = "-".join(groups) if not any(char.isdigit() for char in token): return None # well-known, Wi-Fi kinds = ["d" if g.isdigit() else "m" if any(c.isdigit() for c in g) else "u" if g.isupper() else "l" for g in groups] if all(kind == "d" for kind in kinds): if len(groups) == 2: years = all(len(g) == 4 and 1000 <= int(g) <= 2100 for g in groups) if not (match.group(1) or any(len(g) > 1 and g[0] == "0" for g in groups) or (min(len(g) for g in groups) >= 4 and not years) or _identifier(match.string, match.start()) in ("strong", "pin")): return None # 10-15 and 2019-2021 are ranges return self._digit_groups(groups) if len(groups) == 2 and kinds[1] == "d" and kinds[0] in "ul": letters, number = groups if (kinds[0] == "l" and len(letters) > 1) or self._reads_as_word(letters): return None # pre-2020, COVID-19, SAVE-20 if _short_number(number): return f"{_spelled(letters)} {cardinal(int(number))}" # B-12, KX-300 return f"{_spelled(letters)} , {digits(number)}" if len(groups) == 2 and kinds[0] == "d" and (kinds[1] == "l" or len(groups[1]) == 1 or re.fullmatch(r"\d+[A-Za-z]+", groups[1])): return None # 5-year, 3-D, 5-10MB if any((kind == "l" and len(g) > 2) or (kind == "m" and re.fullmatch(r"\d+(?:s|st|nd|rd|th)", g)) for kind, g in zip(kinds, groups)): return None # 20-year-old, F-16s if not any(char.isupper() for char in token) and ("l" in kinds or any(kind == "m" for kind in kinds)): return None # 3-in-1, 50s-60s parts = [] for kind, group in zip(kinds, groups): if kind == "d": parts.append(digits(group)) elif kind == "m": parts.append(self._alnum(group)) elif len(group) > 3 and self.is_word(group.lower()): parts.append(group) else: parts.append(_spelled(group) if len(group) <= 5 else group) return " , ".join(parts) def _code(self, match): """Codes written with hyphens: AB-4471-Z, 91-220-0017, #A7-4419.""" words = self._code_words(match.group(2).split("-"), match) if words is None: return match.group(0) return (" number " if match.group(1) else " ") + words + " " def _grouped(self, match): """Digits written in groups with spaces: a telephone number, a card or tracking number, or a large number with spaces between the thousands.""" text, plus, lead, trail = match.string, match.group("plus"), match.group("lead"), match.group("trail") core = match.group("core").split(" ") head = tail = "" if lead in _CAPITAL_WORDS: # "IN 2019 150 ..." in capitals head, tail, lead, trail = lead + " ", (" " + trail if trail else ""), None, None kind = _identifier(text, match.start()) pure = all(part.isdigit() for part in core) if pure and not plus and not lead: following = text[match.end():] amount = self._counted(text, match.end()) or re.match(r"\s?(?:" + self._unit_names + r")(?![A-Za-z])", following, re.IGNORECASE) is not None if kind is None and core[0][0] != "0" and len(core[0]) <= 3 and all(len(part) == 3 for part in core[1:]) \ and (core[-1] == "000" or (amount and len(core) > 2)): return head + ",".join(core) + tail # 1 000 000: the number rules read it if not (kind or (len(core[0]) > 1 and core[0][0] == "0") or (len(core) > 2 and any(len(part) > 2 for part in core))): return match.group(0) elif not pure and not lead and not plus and kind is None: return match.group(0) zero = "oh" if plus or kind == "phone" or (pure and core[0][0] == "0" and sum(len(part) for part in core) >= 10) else "zero" if pure: spoken = self._digit_groups(core, zero) else: spoken = " , ".join(digits(part, zero) if part.isdigit() else self._alnum(part, zero) for part in core) if lead: spoken = f"{_spelled(lead)} , {spoken}" if trail: spoken = f"{spoken} , {_spelled(trail)}" return head + (" plus " if plus else " ") + spoken + " " + tail def _glued(self, match): """Letters and digits written together: B12, 14C, X7K, PS5. A single letter is always named; it is never the article "a".""" out = [] for run in re.findall(r"[A-Za-z]+|\d+", match.group(0)): if run.isdigit(): out.append(cardinal(int(run)) if _short_number(run) else digits(run)) elif len(run) == 1 or ((run.isupper() or len(run) <= 3) and not self._reads_as_word(run)): out.append(_spelled(run)) else: out.append(run) return " " + " ".join(out) + " " # -- web addresses and file names -- def _address_part(self, part): """One run of letters inside an address: a word, or letters to name.""" lower = part.lower() if len(part) > 1 and self.is_word(lower) and (len(part) > 2 or lower in _SHORT_WORDS): return part return _spelled(part) if len(part) <= 3 else part def _spoken_address(self, core): pieces = re.findall(r"[A-Za-z]+|\d+|.", core) mail, host = "@" in core, False out = [] for index, piece in enumerate(pieces): following = pieces[index + 1] if index + 1 < len(pieces) else "" if piece == "@": host = True if piece.isdigit(): out.append(_address_digits(piece)) elif not piece.isalpha(): out.append(_SEPARATORS.get(piece, "")) elif index and pieces[index - 1] == "." and (host or not mail) and ( following in ("", "/", ":", "?", "#") or (len(piece) <= 3 and (piece.lower() in self.endings or not self.is_word(piece.lower())))): # the ending of a file name or an address: a word if the table says so, otherwise letters out.append(piece.lower() if self.endings.get(piece.lower()) == "word" else _spelled(piece)) else: letters = "" for part in re.findall(r"[A-Z]?[a-z]+|[A-Z]+(?![a-z])", piece): spoken = self._address_part(part) if spoken.startswith(_SPELL): letters += part continue if letters: out.append(_spelled(letters)) letters = "" out.append(spoken) if letters: out.append(_spelled(letters)) return " ".join(item for item in out if item) def _address(self, match): """Web and mail addresses, file names and paths: the dots, slashes, underscores and @ are spoken. The ending is a word when ``data/endings.txt`` says so ("dot com") and letters otherwise ("dot P D F").""" whole = match.group(0) text = whole.rstrip(".,;:!?)") # punctuation of the sentence, not of the address tail = whole[len(text):] if match.group("file"): ending = match.group("ending") known = ending.lower() in self.endings marked = bool(match.group("folder") or match.group("path")) or "_" in match.group("stem") if not (known or (marked and len(ending) <= 5 and (ending.islower() or ending.isupper()) and not self.is_word(ending.lower()))): return whole # "the end.Then": a missing space, not a file core = re.sub(r"^(?:https?://)?(?:www\.)?", "", text).rstrip("/") return f" {self._spoken_address(core)} {tail}" def _chat_form(self, match): """Chat spellings that contain a digit or a slash: gr8, b4, 2day.""" said = self.chat.get(match.group(0).lower()) if said is None: return match.group(0) if said == "-": return " " return " " + " ".join(f"{_SPELL}{part[1:]}{_END}" if part.startswith("*") else part for part in said.split()) + " " def _inline(self, match): """``{...}``: a pronunciation written by the user. Braces that hold anything else are no error: they stay and are read like brackets ("{name}", "{1, 2, 3}", "\\frac{1}{2}"). So are single letters, which are phones only by chance ("x_{i}", "{n}").""" symbols = match.group(2).split() if not symbols or all(len(symbol) == 1 and symbol.isascii() for symbol in symbols): return match.group(0) try: phones = convert("", symbols) if is_arpabet(symbols) else from_text(" ".join(symbols)) except ValueError: return match.group(0) self.inline.append((match.group(1) or "", phones)) return f" {_INLINE}{chr(_INLINE_BASE + len(self.inline) - 1)}{_INLINE} " def _dotted(self, match, text): """``U.S.``, ``e.g.``, ``p.m.``: letters with full stops.""" letters = match.group(0).replace(".", "") key = letters.lower() following = text[match.end():].lstrip() if key == "eg": words = "for example" elif key == "ie": words = "that is" else: words = f"{_SPELL}{letters.upper()}{_END}" first = re.match(r"[A-Za-z']+", following) ends_sentence = not following or (following[0].isupper() and first and first.group(0).lower() in _SENTENCE_STARTERS) return f" {words}{' .' if ends_sentence and match.group(0).endswith('.') else ''} " def _initial(self, match, text): """"George W. Bush", "J. R. R. Tolkien": the initial of a name is named and its full stop is no sentence end. "an A. Then", "Plan B. The" keep the stop.""" before = re.search(r"(?:([A-Za-z]+)|([A-Z])\.)\s+$", text[:match.start()]) after = re.match(r"\s+(?:([A-Z])\.(?=\s)|([A-Z][a-z'’]+|[A-Z]{2,})\b)", text[match.end():]) if not after: return match.group(0) if after.group(2): word = after.group(2).lower().rstrip("'’s") if after.group(2).lower().endswith(("'s", "’s")) else after.group(2).lower() if word in _SENTENCE_STARTERS | _CLAUSE_OPENERS: return match.group(0) if self.is_word(word + "ing") or (word.endswith("e") and self.is_word(word[:-1] + "ing")): return match.group(0) # a verb opens a new sentence: "Meet the Zephyr X. Schedule a drive" if before is None: if text[:match.start()].strip() and not re.search(r"[.!?;:\"'(]\s*$", text[:match.start()]): return match.group(0) # after a small word or a figure: "an A. Then" if not after.group(1): return match.group(0) # "A. Smith" alone at the start is too uncertain elif before.group(1) and not after.group(1): # "by T. R. Hale": a run of initials needs no name before word = before.group(1) if not word[0].isupper() or word.lower() in _LABELLED or word.lower() in _CARDINAL_AFTER: return match.group(0) return f" {_SPELL}{match.group(1)}{_END} " def _abbreviation(self, match, text): token, stop = match.group(1), match.group(2) key = token.lower() following = text[match.end():] next_word = re.match(r"\s*([A-Za-z]+|\d)", following) capital_next = bool(next_word) and next_word.group(1)[0].isupper() capital_before = re.search(r"[A-Z][a-z]+\s+$", text[:match.start()]) is not None if key in ("st", "dr") and token[0].isupper(): if capital_next and (stop or key == "dr" or not capital_before): return (" saint " if key == "st" else " doctor ") # "St. Mary's", "St Heliers"; "Main St" stays if capital_before and stop: return (" street" if key == "st" else " drive") + (" ." if not following.strip() else " ") return match.group(0) if key not in self.abbreviations: return match.group(0) when, words = self.abbreviations[key] number_next = bool(next_word) and (next_word.group(1).isdigit() or (key in _PAGE_LIKE and next_word.group(1).lower() in _NUMBER_WORDS)) if when == "title": if not (token[0].isupper() and (capital_next or stop)): return match.group(0) elif when == "number": previous = re.search(r"([A-Za-z]+)\.?\s+$", text[:match.start()]) if not (stop and (number_next or (key == "no" and previous and previous.group(1).lower() in _NUMBERED))): return match.group(0) elif not stop or (when == "small" and token.isupper()): return match.group(0) elif key in ("mar", "sun", "sat", "jan", "dec", "max", "min", "est", "no") and not token[0].isupper() and not number_next: # also words; "max. load" and "est. time" are the abbreviation, "he sat." is not if not (key in ("max", "min", "est") and next_word and next_word.group(1)[0].islower()): return match.group(0) if key == "est" and not number_next: words = "estimated" # "Est. arrival"; "est. 1885" is "established" ends_sentence = when != "title" and (not following.strip() or (capital_next and next_word.group(1).lower() in _SENTENCE_STARTERS)) return f" {words}{' .' if ends_sentence else ''} " def rewrite(self, text): """The text with every non-word written out, markers included.""" text = _VULGAR.sub(self._vulgar, text) # before "1½" becomes "11/2" and "m²" becomes "m2" text = self._area.sub(self._square, text) text = text.replace("²", " squared ").replace("³", " cubed ") text = unicodedata.normalize("NFKC", text).replace("⁄", "/").replace("−", " minus ") if not text.isascii(): text = _ACCENTED.sub(lambda m: "{" + _ACCENT_DECIDES[m.group(1).lower()] + (" Z" if m.group(2) else "") + "}", text) text = "".join(_plain_letter(char) for char in text) text = re.sub(r"(?<=\d)–(?=\d)", "-", text) # 10–15 is a range, not a pause text = _DAY_RANGE.sub(self._day_range, text) for old, new in (("‘", "'"), ("’", "'"), ("‚", "'"), ("‛", "'"), ("`", "'"), ("´", "'"), ("“", '"'), ("”", '"'), ("„", '"'), ("‟", '"'), ("«", '"'), ("»", '"'), ("–", "—"), ("―", "—"), ("[", "("), ("]", ")")): text = text.replace(old, new) self.inline = [] text = re.sub(r"([A-Za-z']+)?\{([^{}]*)\}", self._inline, text) text = text.replace("{", "(").replace("}", ")") text = _ADDRESS.sub(self._address, text) text = text.replace("→", " to ") text = _EMOJI.sub(" ", text) text = _EMOTICON.sub(" ", text) text = re.sub(r"\.{3,}", "…", text) text = re.sub(r"(?<=\d)\s-\s(?=\d[\d.,]*\s?=)", " minus ", text) text = re.sub(r"(?≥≤≠]|!=", text): text = self._mark_comparisons(text) if re.search(r"\d", text): # most text has no digit and skips these passes text = self._figures(text) return self._signs(text) def _figures(self, text): """The passes that need a digit: money, times, dates, telephone numbers, versions, codes, units, fractions, ranges.""" text = _FORMULA_END.sub(lambda match: match.group(0)[:-1] + _FORMULA_SIGN, text) # a height: 6'2", 5' 11'', 5ft 6in text = re.sub(r"(? 12 >= b for a, b in seen) and not any(b > 12 >= a for a, b in seen) self.dated = re.search(r"(?a", "(k)".""" if not any(sign in text for sign in "=<>+*/^()≤≥≠≈×÷±"): return text plain = _without_angles(text) places = {match.start("letter") for pattern in _LETTER_PLACES for match in pattern.finditer(plain)} if not places: return text return "".join(f"{_SPELL}{char.upper()}{_END}" if index in places else char for index, char in enumerate(text)) @staticmethod def _mark_comparisons(text): """Put the mark for "is" after the first value of each comparison between two values: "67 > 66", "b>a", "f(x) >= 0". Not where a word that opens no clause stands before that value: "for n > 4", "any q > 0".""" plain = _without_angles(text) def mark(match): before, after = plain[:match.start()], plain[match.end():] value = _VALUE_BEFORE.search(before) if plain[match.start("sign")] != match.group("sign")[0] or value is None or not _VALUE_AFTER.match(after): return match.group(0) start = value.start() if value.group(0) in ")]": # "f(x) > 0", "a[i] < b": the whole of it is the value depth = 0 while start >= 0: depth += (before[start] in ")]") - (before[start] in "([") if depth == 0: break start -= 1 while start > 0 and (before[start - 1].isalnum() or before[start - 1] in "_."): start -= 1 word = re.search(r"[A-Za-z']+$", before[:max(start, 0)].rstrip()) if word and word.group(0).lower() not in _CLAUSE_OPENERS: return match.group(0) return _IS + match.group(0) return _COMPARED.sub(mark, text) @staticmethod def _compared(match, words): """``words`` for a comparison sign, after "is" where the mark for it stands before the sign.""" marked = match.group(0).startswith(_IS) or match.string[:match.start()].rstrip(" ").endswith(_IS) return f" {'is ' if marked else ''}{words} " def _joined_comparisons(self, text): """"b>a", "x<5", "p<.05": the words for "<" and ">" written with no spaces.""" plain = _without_angles(text) def read(match): before, after = text[:match.start()].rstrip(_IS), text[match.end():] if plain[match.start()] != match.group(0): # the bracket of a tag return match.group(0) if not (_FIGURE_BEFORE.search(before) or _FIGURE_AFTER.match(after) or (_LETTER_BEFORE.search(before) and _LETTER_AFTER.match(after))): return match.group(0) return self._compared(match, "less than" if match.group(0) == "<" else "greater than") return _JOINED_COMPARISON.sub(read, text) def _signs(self, text): """The passes that follow: signs, abbreviations, and the numbers that are left.""" text = self._named_letters(text) if "<" in text or ">" in text: text = self._joined_comparisons(text) text = re.sub(r"(?=|<=|!=)(?=[\w\s" + _SPELL + r"])", lambda m: self._compared(m, _COMPARISONS[m.group(1)]), text) text = re.sub(r"(?<=\d) Ti\b", f" {_spelled('TI')}", text) # RTX 4070 Ti text = re.sub(r"\b([Rr])ef\.\s?[Nn]o\.(?=\s)", r"\1eference number", text) text = re.sub(r"(?]\s?(?=\d)|(?<=\s)[<>](?=\s)", lambda m: self._compared(m, "less than" if "<" in m.group(0) else "greater than"), text) text = re.sub("[" + re.escape("".join(_SYMBOL_WORDS)) + "]", lambda m: self._compared(m, _SYMBOL_WORDS[m.group(0)]) if m.group(0) in "≥≤≠" else f" {_SYMBOL_WORDS[m.group(0)]} ", text) return text.replace(_IS, "") # -- words -- @staticmethod def _unstretched(word): """Spellings a word stretched for emphasis may stand for. A letter typed three times or more is cut to one or to two: at the end of a word one is the usual spelling ("sooo", "heyyy") unless the letter is one that doubles there ("alll", "offf"); inside a word two ("goood", "cooool"). "mmm" is tried with two first: it is the hum "mm", not the letter M.""" if not re.search(r"(.)\1\1", word): return [] def cut(double_inside, double_final): def replace(match): final = match.end() == len(word) return match.group(1) * (2 if (double_final if final else double_inside) else 1) return re.sub(r"(.)\1{2,}", replace, word) liked = bool(re.search(r"([lfsz])\1\1+$", word)) or set(word) == {"m"} or re.sub(r"(.)\1{2,}$", r"\1", word) == "to" ordered = [cut(True, liked), cut(True, not liked), cut(False, liked), cut(False, not liked)] return list(dict.fromkeys(ordered)) @staticmethod def _chat_words(chat, raw): """Tokens for one row of the chat table: words, letters to name, or nothing.""" if chat == "-": return [] return [Word(part[1:].lower(), raw=raw, spell=True) if part.startswith("*") else Word(part.lower(), raw=part) for part in chat.split()] @staticmethod def _reply(out, place): """True when the word at ``place`` is a clause by itself and can be a reply: "k", "k, thanks", "see you there, k?". Between two commas, or with a single letter next to it across a comma, it is one of a list: "i, j, k".""" marks = [] for step in (-1, 1): index = place + step while 0 <= index < len(out) and isinstance(out[index], Punct) and out[index].mark in "\"'()": index += step # quotation marks and brackets part nothing if not 0 <= index < len(out): marks.append("") # the text starts or ends here elif isinstance(out[index], Word): return False # a word in the same clause: "the value of k" else: marks.append(out[index].mark) beyond = out[index + step] if 0 <= index + step < len(out) else None if out[index].mark == "," and isinstance(beyond, Word) and len(beyond.text) == 1 and beyond.text != "i": return False return marks != [",", ","] def _word_tokens(self, raw, all_caps, previous, following=True, fresh=False): """Tokens for one run of letters and apostrophes as written. ``previous`` is the word before it, ``following`` whether a word follows directly, ``fresh`` whether a new sentence starts here.""" lower = raw.lower() chat = self.chat.get(lower) name = raw[:1].isupper() and raw[1:].islower() and not fresh # "thank you, Ty", "Dr. Ehud Ur" if chat is not None and (len(raw) > 1 or raw.islower()) and lower not in self.replies and not name: return self._chat_words(chat, raw) roman, owner = (raw[:-2], "'s") if raw.endswith("'s") else (raw, "") if roman in _ROMAN and previous and previous[0].isupper(): number = _ROMAN[roman] words = cardinal(number) if previous.lower() in _CARDINAL_AFTER else "the " + ordinal(number) return [Word(word, raw=raw) for word in (words + owner).split()] # "Elizabeth II's" if raw == "I" and previous and previous[0].isupper() and previous.lower() in _CARDINAL_AFTER - {"world"}: return [Word("one", raw=raw)] # "World War I", "Type I" letters = raw.replace("'", "") possessive = lower.endswith("'s") stem = raw[:-2] if possessive else raw if len(stem) == 1 and stem.isalpha(): # "A" is the article unless it labels something: "Plan A.", "vitamin A" label = stem == "A" and previous and (not following or (not fresh and previous.lower() in _LABELLED)) if stem in ("a", "A", "I", "i") and not possessive and not label: return [Word(stem.lower(), raw=raw)] return [Word(lower, raw=raw, spell=True)] if len(stem) > 2 and stem[0] in "SsZz" and len(set(lower)) == 1 and (all_caps or not stem.isupper()): return [Word(lower, raw=raw, phones=((lower[0], 0),))] # "Sss", "Zzzz": a hiss, a snore if stem.isupper() and stem.isalpha() and 2 <= len(stem) <= 5 and (not all_caps or not self.is_word(stem.lower())): if stem in _INITIALISMS or not self.is_word(stem.lower()) \ or (len(stem) == 2 and stem not in _CAPITAL_WORDS and stem not in _CAPITAL_PAIRS): return [Word(lower, raw=raw, spell=True)] if not letters.isalpha(): return [] # iOS, macOS, eSIM: small letters, then capitals that are named. The dictionary's "ios" is another word. capitals = re.fullmatch(r"[a-z]+(?:[A-Z][a-z]+)*[A-Z]{2,}(?:'s)?", raw) is not None if capitals or not self.is_word(lower): for candidate in ([] if capitals else self._unstretched(lower)): # "soooo" -> "so", "goooood" -> "good" if self.is_word(candidate): return [Word(candidate, raw=raw)] parts = re.findall(r"[A-Z]?[a-z']+|[A-Z]+(?![a-z])", raw) if len(parts) > 1 and "".join(parts) == raw: # iPhone, YouTube, JavaScript out = [] for part in parts: named = part in _INITIALISMS if capitals and part.isupper() else False spelled = len(part) == 1 or named or (part.isupper() and not self.is_word(part.lower())) if capitals and out and out[-1].spell and (spelled or part == "'s"): out[-1] = out[-1]._replace(text=out[-1].text + part.lower(), raw=out[-1].raw + part) # i + OS else: out.append(Word(part.lower(), attach=bool(out), raw=part, spell=spelled)) return out return [Word(lower, raw=raw)] def tokens(self, text): text = self.rewrite(text) runs = re.findall(r"[A-Za-z]{2,}", text) all_caps = len(runs) >= 3 and sum(run.isupper() for run in runs) >= 0.6 * len(runs) out = [] pattern = re.compile( _SPELL + r"([A-Za-z ]+)" + _END + r"|" + _INLINE + r"(.)" + _INLINE + r"|('?[A-Za-z]+(?:'[A-Za-z]+)*)|(\d+)|(-)|(\S)") attach = False previous = "" fresh = True # a sentence starts here: "a plan. A dog" is the article short = [] # where "Ss" or "Zz" stands apart: perhaps a hiss, a snore replies = [] # where a chat spelling stands that is read only as a reply for match in pattern.finditer(text): spelled, inline, word, number, hyphen, other = match.groups() before = text[match.start() - 1] if match.start() else " " if hyphen: attach = bool(out) and isinstance(out[-1], Word) and not before.isspace() \ and match.end() < len(text) and text[match.end()].isalnum() continue new = [] if spelled is not None: new = [Word(spelled.replace(" ", "").lower(), raw=spelled, spell=True)] elif inline is not None: written, phones = self.inline[ord(inline) - _INLINE_BASE] new = [Word(written.lower(), raw=written, phones=phones)] elif word is not None: if word.startswith("'") and word.lower() not in _LEADING_APOSTROPHE: out.append(Punct("'")) word = word[1:] elif word.startswith("'") and not self.is_word(word.lower()): word = word[1:] new = self._word_tokens(word, all_caps, previous, re.match(r"\s*[A-Za-z]", text[match.end():]) is not None, fresh) previous = word if word in ("Ss", "ss", "Zz", "zz") and len(new) == 1 and (before.isspace() or before in "\"'(") \ and re.match(r"[.!?…,;:—\"')]*(?:\s|$)", text[match.end():]): short.append(len(out)) # a reply stands free: not "(k)", "/k/", "[:k]", and not before a colon or a mid-text "!" ("k: king", "k! = mk") if word.lower() in self.replies and (len(word) > 1 or word.islower()) and len(new) == 1 \ and (before.isspace() or before in "\"'") \ and re.match(r"(?:[.?…,;—]*[\"']?(?:\s|$)|!+[\"']?\s*$)", text[match.end():]): replies.append(len(out)) elif number is not None: new = [Word(part, raw=number) for part in number_words(number).split()] elif other in PUNCTUATION: if not (other == "'" and before.isalnum() and match.end() < len(text) and text[match.end()].isalnum()): out.append(Punct(other)) if other in ".!?;…": fresh = True attach = False continue if new and attach: new[0] = new[0]._replace(attach=True) out.extend(new) attach = False fresh = fresh and not new # "Ss!" and "Sh, ss, tsk" are a hiss. Beside an ordinary word the two letters # stay letters, as in "the ss", "mm:ss" and a variable named ss. Quotation # marks and brackets do not part them from that word: "written as 'ss'". for place in short: first, last = place, place + 1 while first and (isinstance(out[first - 1], Word) or out[first - 1].mark in "\"'()"): first -= 1 while last < len(out) and (isinstance(out[last], Word) or out[last].mark in "\"'()"): last += 1 if all(token.text in _SOUNDS or (token.phones is not None and len(set(token.text)) == 1) for token in out[first:last] if isinstance(token, Word)): out[place] = out[place]._replace(phones=((out[place].text[0], 0),)) # "k" is "okay" as a reply by itself; beside a word or a figure it is the letter. for place in reversed(replies): if self._reply(out, place): raw = out[place].raw out[place:place + 1] = self._chat_words(self.chat[raw.lower()], raw) return out _DEFAULT = None def tokens(text, is_word=None): """Words and punctuation marks of ``text``. ``is_word(spelling)`` tells whether the dictionary knows a spelling (used for capitals and chat forms).""" global _DEFAULT if is_word is None: if _DEFAULT is None: _DEFAULT = Normalizer() return _DEFAULT.tokens(text) return Normalizer(is_word).tokens(text)