"""Marker names and numbers as people say them (English, Korean, mixed, and as whisper writes them) -> the written form the readers use. brain (keyword rules, fetch, named_place) and steps (missions, the LLM check) both read through norm(), so a place or a number is read the same way everywhere. "피쓰리로 가 줘", "피 삼으로", "포인트 쓰리", "삼 번 포인트", "point three", "P.3", "피3" -> "p3" "파이널로" -> "final로" "도크 투", "dock two" -> "dock2" "엑시트" -> "exit" "홈으로" -> "home으로" "일 미터 반", "1미터 50센치", "one and a half meters" -> "1.5 m" "오십 센치", "half a meter" -> "0.5 m" Only clear forms are changed: "피로 가" (no number), "PE로", "피자", "이번에" stay as they are, and Lumi asks. With the map's marker names (norm(text, names)) any name is read as said: "table front" / "테이블 프론트" -> "table_front", "booth two" / "부스 투" -> "booth_2", "스타트로" -> "start로", and a word after "to" / "from" one letter off one name only ("go to the stagee") -> that name. python assistant/wording.py "피쓰리로 가서 일 미터 앞으로 가" """ import difflib import re import sys EN_NUM = {"one": 1, "two": 2, "three": 3, "four": 4, "five": 5, "six": 6, "seven": 7, "eight": 8, "nine": 9, "ten": 10, "eleven": 11, "twelve": 12} KO_EN_NUM = {"원": 1, "투": 2, "쓰리": 3, "스리": 3, "뜨리": 3, "트리": 3, "포": 4, "파이브": 5, "화이브": 5, "식스": 6, "씩스": 6, "세븐": 7, "쎄븐": 7, "에잇": 8, "에이트": 8, "나인": 9, "텐": 10, "일레븐": 11, "트웰브": 12} # English numbers said in Korean ("피쓰리", "포인트 원") NATIVE = {"한": 1, "두": 2, "세": 3, "네": 4, "다섯": 5, "여섯": 6, "일곱": 7, "여덟": 8, "아홉": 9, "열": 10} SINO = {"일": 1, "이": 2, "삼": 3, "사": 4, "오": 5, "육": 6, "륙": 6, "칠": 7, "팔": 8, "구": 9} _D = "[일이삼사오육륙칠팔구]" _SINO = rf"(?:{_D}?천(?:{_D}?백)?(?:{_D}?십)?{_D}?|(?:{_D}?백)(?:{_D}?십)?{_D}?|{_D}?십{_D}?|{_D})" # 일 … 구천구백구십구 TENS = {"twenty": 20, "thirty": 30, "forty": 40, "fifty": 50, "sixty": 60, "seventy": 70, "eighty": 80, "ninety": 90} NUM_WORDS = {"one": "1", "two": "2", "three": "3", "four": "4", "five": "5", "six": "6", "seven": "7", "eight": "8", "nine": "9", "ten": "10", "a": "1", "an": "1", "한": "1", "두": "2", "세": "3", "네": "4"} UNIT = (r"(?:m|meters?|metres?|metals?|cm|centimet(?:er|re)s?|mm|millimet(?:er|re)s?|km|kilomet(?:er|re)s?|miles?|" r"미터|센티(?:미터)?|센치(?:미터)?|밀리(?:미터)?|킬로(?:미터)?(?!그램)|마일)") # whisper writes "metal" for meter _CM = r"(?:cm|centimet(?:er|re)s?|센티(?:미터)?|센치(?:미터)?)" # what may follow a number said in Korean words for it to be a marker: a particle, a place word, or no more Hangul # ("피삼으로", "피파이브 앞에", "피일에서"; not "피투성이", "피오나") _KO_END = (r"(?=(?:(?:으로|로|에서|에게|에다|에|까지|부터|중에서|중에|중|또는|번|이|가|은|는|도|랑|이랑|하고|의|한테)(?![가-힣])" r"|(?:으로|로|에)(?=가|와|돌아|이동|갔)|(?:이나|나)(?=\s|$)|(?:앞|뒤|옆|위|쪽|근처))|(?![가-힣]))") _P = r"(?:(? the name (r"(?:파이[널날]|화이[널날])(?:\s*포인트)?|파이너(?=로|에)", "final"), (r"\bdo(?:ck|c|k)\s*-?\s*(?:2|two)(?![a-z0-9])|\bdock(?:y|u|ie)\s?2\b|(?:도크|독|닥)\s*(?:2|투|트)" + _KO_END, "dock2"), (r"(?:엑시트|엑싯|엑짓|엑지트|에그짓|이그짓)", "exit"), (r"(? stage) def sino(word): """'사십오' -> 45, '백팔십' -> 180, '십' -> 10.""" n, part = 0, 0 for ch in word: if ch in SINO: part = SINO[ch] else: n += (part or 1) * {"십": 10, "백": 100, "천": 1000}[ch] part = 0 return n + part def _num(word): return EN_NUM.get(word) or KO_EN_NUM.get(word) or (int(word) if word.isdigit() else sino(word)) def markers(t): """Marker names in lower-case text -> "p3", "final", "dock2", "exit", "pinit", "wp", "home", "point a".""" ko = "|".join(sorted(KO_EN_NUM, key=len, reverse=True)) t = re.sub(rf"(? "3번으로" lambda m: m.group(0) if m.group(0) == "이번" and t[m.end():m.end() + 1] == "에" # "이번에": this time else f"{sino(m.group(1))}번", t) for name_re, name in NAMED: t = re.sub(name_re, name, t) t = re.sub(rf"{_P}\s*[.-]?\s*(\d+)(?!\d|\.\d)", lambda m: f"p{m.group(1)}", t) # "P.3", "p 3", "피3", "포인트 3" t = re.sub(r"(? digits and metres: "half a meter" -> "0.5 m", "오십 센치" -> "0.5 m", "일 미터 반" -> "1.5 m", "구십 도" -> "90도".""" t = re.sub(r"\bone and a half\b", "1.5", t) t = re.sub(rf"\b({'|'.join(TENS)})(?:[ -]({'|'.join(list(EN_NUM)[:9])}))?\b", lambda m: str(TENS[m.group(1)] + EN_NUM.get(m.group(2), 0)), t) t = re.sub(rf"\b(?:a|one|1) thousand\b|\b(\d+|{'|'.join(EN_NUM)}) thousand\b", lambda m: str(1000 * _num(m.group(1)) if m.group(1) else 1000), t) # "a thousand", "five thousand" t = re.sub(rf"\b([2-9]|{'|'.join(list(EN_NUM)[1:9])}) hundred(?: and)?(?: (\d+))?\b", lambda m: str(100 * _num(m.group(1)) + int(m.group(2) or 0)), t) # "two hundred and fifty" t = re.sub(rf"\b({'|'.join(TEEN)}|eleven|twelve)\b(?=\s*{UNIT}(?![a-z]))", lambda m: str(TEEN.get(m.group(1)) or EN_NUM[m.group(1)]), t) # "fifteen centimetres" t = re.sub(r"\b(?:a|one) hundred(?: and)? (\d+)\b", lambda m: str(100 + int(m.group(1))), t) t = re.sub(r"\b(?:a|one) hundred\b", "100", t) t = re.sub(rf"(? seconds written " s": "2 minutes" -> "120 s", "half an hour" -> "1800 s", "1분 30초" -> "90 s", "오 분" -> "300 s", "한 시간" -> "3600 s". Vague ones ("a few minutes", "몇 분") stay as they are. Used by the mission reader only (brain keeps the words).""" words = {**EN_NUM, **TEEN, "a": 1, "an": 1} en = rf"(\d+(?:\.\d+)?|{'|'.join(sorted(words, key=len, reverse=True))})" val = lambda w: float(w) if w[0].isdigit() else words[w] t = re.sub(r"\bhalf (?:an|a) hour\b", "1800 s", t) t = re.sub(r"\b(?:a )?quarter (?:of an )?hour\b", "900 s", t) t = re.sub(r"\bhalf a minute\b", "30 s", t) t = re.sub(rf"\b{en} and a half {_MIN}|\b{en} {_MIN} and a half\b", lambda m: f"{(val(m.group(1) or m.group(2)) + 0.5) * 60:g} s", t) t = re.sub(rf"\b{en} {_HOUR}", lambda m: f"{val(m.group(1)) * 3600:g} s", t) t = re.sub(rf"\b{en} ?{_MIN}(?:,? (?:and )?(\d+) ?{_SEC})?", lambda m: f"{val(m.group(1)) * 60 + float(m.group(2) or 0):g} s", t) sec = en.replace("|an|a)", ")") # "a second" is a short wait, not 1 s (steps.WAIT_SHORT_S) t = re.sub(rf"(? 1 or tok not in KO_NOT_ALONE else []) if tok.isdigit(): alts += [w for w, n in {**EN_NUM, **KO_EN_NUM, **(SINO if len(toks) > 1 else {})}.items() if n == int(tok)] # "부스 삼"; a lone "이" / "오" is a word, not 2 / 5 alts = sorted(map(re.escape, alts), key=len, reverse=True) if re.fullmatch(r"[가-힣]{3,}", tok): alts.insert(0, r"\s?".join(tok)) # Korean words said or written apart: "커피 코너" for 커피코너 parts.append("(?:" + "|".join(alts) + ")") return rf"(? "coffee_corner").""" return re.sub(r"\s+", "_", name.strip().lower()) def marker(word, names): """The marker of the map written as `word` in normalised text ("coffee_corner" -> "Coffee Corner"), or None.""" return next((n for n in names if written(n) == word), None) def map_names(t, names): """Marker names of this map in normalised text written as the names (written()), then one-name slips after a place word ("to the stagee").""" names = [n for n in names if not re.fullmatch(r"p\d+", n.lower())] # p-numbers: markers() above for n in sorted(names, key=len, reverse=True): t = re.sub(_spoken(n), written(n), t) known = {written(n) for n in names} def slip(m): for w in (m.group(2).split()[0], m.group(2)): # one word, then two ("table fronts") if w in known: break close = close_names(w, names) if len(close) == 1: return m.group(1) + written(close[0]) + m.group(2)[len(w):] return m.group(0) return re.sub(r"(\b(?:to|from|at|toward|towards) (?:the )?)([a-z][a-z0-9_]*(?: [a-z0-9]+)?)", slip, t) ASR_SLIPS = [ # how whisper writes Korean commands (tests/asr_check.py, 2026-10-21) -> as said (r"([a-z0-9])\.?\s+(으로|로)\s*(?=가|와|이동|돌아)", r"\1\2 "), # "P3. 로가줘", "final point. 로 가": one word (r"(?<=로)\s*가죠(?![가-힣])", " 가 줘"), # "p3로 가 줘" written "p3로 가죠" (r"\blook (?:last|lest|less)(?=[\s.!,]*$)", "look left"), (r"\bdoc t\b", "dock 2"), # said at the end (r"고게(?=\s*(?:들|숙))", "고개"), (r"찝어", "집어"), (r"파이나(?=\s*(?:갔|가|로))", "파이널"), # the owner's English as whisper wrote it live (logs/lumi.log 2026-10-21 / 22) (r"\bcount to(?= p\s?\d)", "come to"), # "Let's count to p5." (r"\b(?:go )?(?:copy|pick) (\d+|one|two|three|four|five|six|seven|eight|nine|ten) point\b", r"go to p\1"), (r"\bfall(?:ow)? (?:lonely|of him)\b", "follow me"), # "Let's fall lonely.", "Let's fall of Him." # hearing round 2 (tests/asr_check.py blind r3 / set 5, 2026-10-22 night) (r"(\d)\s*센트(?=\s*(?:앞|뒤|가|와|이동|옆|만|[.!]|$))", r"\1센티"), # "왼쪽으로 30센트 가죠" (r"\bboot(?:h)?(?: number)? (?=\d|(?:one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve)\b)", "booth "), # "Go to boot number 12.", "Go to boot three." (r"\b(go|move|turn) write\b", r"\1 right"), (r"\bturn rate\b", "turn right"), # "Go write one meter." (r"\bto \.(\d+)\b", r"to point \1"), # "Please drive to .8." (r"\bmean stage\b", "main stage"), ] def norm(text, names=()): """Lower case, whisper's slips (ASR_SLIPS), numbers first ("point five meters" is a distance), then marker names (names: the map's).""" t = " " + (text or "").lower().replace("’", "'") + " " for slip, said in ASR_SLIPS: t = re.sub(slip, said, t) return re.sub(r"\s+", " ", markers(map_names(numbers(t), names))).strip() # the map's names first: "booth 3 point" # is booth_3, not "booth p3" if __name__ == "__main__": print(norm(" ".join(sys.argv[1:])))