"""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})" # 일 … 구천구백구십구 NATIVE_TENS = {"열": 10, "스물": 20, "스무": 20, "서른": 30, "마흔": 40, "쉰": 50, "예순": 60, "일흔": 70, "여든": 80, "아흔": 90} NATIVE_ONES = {"하나": 1, "한": 1, "둘": 2, "두": 2, "셋": 3, "세": 3, "넷": 4, "네": 4, "다섯": 5, "여섯": 6, "일곱": 7, "여덟": 8, "아홉": 9} _NATIVE_NUM = (rf"(?:({'|'.join(NATIVE_TENS)})\s?({'|'.join(NATIVE_ONES)})?|(다섯|여섯|일곱|여덟|아홉))") # 한 / 두 / 세 / # 네 alone: NUM_WORDS (before a unit only; "한 도" is no angle) _ABOUT = r"(?:만|쯤|정도|가량|씩)?" # "45도만", "30도쯤": still that amount 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) if re.search(r"\b(?:turn|rotat\w*|spin|swivel|pivot|degrees?)\b|돌|회전", t): # "turn left one eighty": 180 t = re.sub(r"\b(one|two|three) ([2-9]0)\b(?!\s*(?:m|cm|mm|meters?|metres?|centimet\w*)\b)", lambda m: str(100 * _num(m.group(1)) + int(m.group(2))), 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"(? 1 or m.group(1) in "십백" else m.group(0), 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." # the head up / down, said or heard loosely (2026-10-23 15:08-15:16: "Look at the bellow.", "Look it up.") (r"\blook (?:at |to(?:wards?)? )?(?:the )?(?:upwards?|above)\b|\blook (?:at |to(?:wards?)? )(?:the )?up\b", "look up"), (r"\blook (?:at |to(?:wards?)? )?(?:the )?(?:downwards?|below|bellow)\b|\blook (?:at |to(?:wards?)? )(?:the )?down\b", "look down"), (r"^\W*look it up\W*$", " look up "), # alone it is the head; "look it up for me" / "look up the news" stay a search (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"), ] _NO = r"(? the Korean word ("레프트로 반 미터 가 줘요", "라이트 턴 해") (_NO + r"레프트\s*(?:쪽으로|으로|로|쪽)?", "왼쪽으로 "), (_NO + r"라이트\s*(?:쪽으로|으로|로|쪽)(?![가-힣])|" + _NO + r"라이트(?=\s*(?:턴|turn|무브|move|\d|반\s*미터))", "오른쪽으로 "), # "라이트 켜" is a light (_NO + r"포워드\s*(?:쪽으로|으로|로|쪽)?", "앞으로 "), (_NO + r"백\s*(?:쪽으로|으로|쪽)(?![가-힣])|" + _NO + r"백(?=\s+(?:\d|반\s*미터|무브|move|고(?![가-힣])|해(?![가-힣])))", "뒤로 "), # "백 미터", "백팔십" stay numbers (r"(?:" + _NO + r"턴|(?<=으로 )turn\b)(?:\s*(?:해|해요|해라|하세요|하십시오|해\s*줘|해\s*주세요|해\s*줄래요?))?(?![가-힣])", "돌아"), # "왼쪽으로 턴 해", "왼쪽으로 turn 해" (r"(?:" + _NO + r"무브|(?:(?<=미터 )|(?<=센티 )|(?<=센치 )|(?<=cm )|(?<=\dm )|(?<=으로 ))고|" + _NO + r"고\s*고)(?:\s*(?:해|해요|하세요|해\s*줘|해\s*주세요))?(?![가-힣])", "이동해"), # "고" alone is a particle / a place's "go": after a way only (_NO + r"업\s*(?=(?:좀\s*)?(?:봐|보세요|봐요))", "위 "), # "업 봐" (_NO + r"다운\s*(?=(?:좀\s*)?(?:봐|보세요|봐요))", "아래 "), # "다운 봐" (r"(고개|머리|헤드|얼굴)\s*(?:를|을)?\s*업(?![가-힣])", r"\1 들어"), (r"(고개|머리|헤드|얼굴)\s*(?:를|을)?\s*다운(?![가-힣])", r"\1 숙여"), (_NO + r"헤드(?=\s)", "고개"), ] _DIR = r"(?:forward|backwards?|back|left|right|앞으로|뒤로|왼쪽|오른쪽|옆으로)" _AMOUNT = r"(?:(?:\d+(?:\.\d+)?|one|two|three|four|five|six|seven|eight|nine|ten|and|a|half)\s+)+" # whisper's slips of amounts in a move (tests/asr_check.py set 5, blind train hearing; 2026-10-23). Each needs a move # direction in the text (MOVE_SAID); none guesses a lost digit: "1.m" (1.5미터 with the 5 dropped) becomes "1점 몇 m", # which is asked ("how far?") instead of driving 1 m. NUMBER_SLIPS = [ (r"(? 'wave', 'stopped' -> 'stop', 'turned' -> 'turn'.""" if w.endswith("ed") and len(w) > 4: if w[-3] == "e" or w[-3] in "cvsz" and w[-4] not in "aeiou" or w.endswith(("aved", "oved", "ived")): return w[:-1] return w[:-3] if w[-3] == w[-4] else w[:-2] return w def polite(t): """The words without an English polite frame (POLITE_FRAME): "wave, if you don't mind" -> "wave", "I'd love it if you could wave" -> "please wave", "I'd love it if you waved at us" -> "please wave at us". Korean frames are ko_plain's.""" t = re.sub(r"\b((?:love|like|appreciate) it|be (?:really |so |very )?(?:great|nice|good|lovely|awesome|cool|helpful|" r"wonderful|amazing|fantastic)) if you (\w+ed)\b", lambda m: f"{m.group(1)} if you could {_base_verb(m.group(2))}", t or "", flags=re.I) out = POLITE_FRAME.sub(lambda m: " please " if re.search(r"\bif you(?: could| would| can)?$", m.group(0), re.I) and not re.search(r"mind|please", m.group(0), re.I) else " ", t or "") return re.sub(r"\s{2,}", " ", out).strip(" ,") or (t or "") def now_not_later(t): """The words without "not later" / "나중 말고" (NOT_LATER).""" return re.sub(r"\s{2,}", " ", NOT_LATER.sub(" ", t or "")).strip() or (t or "") def _jamo(c): n = ord(c) - 0xAC00 return (n // 588, n // 28 % 21, n % 28) if 0 <= n < 11172 else None def _syl(l, v, t=0): return chr(0xAC00 + (l * 21 + v) * 28 + t) def aform(stem): """The 아/어 form of a Korean verb stem, the form a plain request ends in: 가 -> 가, 오 -> 와, 따라오 -> 따라와, 주 -> 줘, 기다리 -> 기다려, 하 -> 해, 집 -> 집어, 잡 -> 잡아, 놓 -> 놔, 누르 -> 눌러, 끄 -> 꺼.""" if not stem or not _jamo(stem[-1]): return stem if stem.endswith("하"): return stem[:-1] + "해" if stem.endswith("놓"): return stem[:-1] + "놔" l, v, t = _jamo(stem[-1]) if t: return stem + ("아" if v in (0, 8) else "어") if stem[-1] == "르" and len(stem) > 1 and _jamo(stem[-2]) and not _jamo(stem[-2])[2]: pl, pv, _ = _jamo(stem[-2]) return stem[:-2] + _syl(pl, pv, 8) + ("라" if pv in (0, 8) else "러") v = {8: 9, 13: 14, 20: 6, 18: 4, 11: 10}.get(v, v) # 오 -> 와, 주 -> 줘, 리 -> 려, 끄 -> 꺼, 되 -> 돼 return stem[:-1] + _syl(l, v) + ("어" if v == 16 else "") KO_SOCIAL = {"안녕하", "수고하", "고생하", "감사하", "축하하", "조심하", "계", "드", "주무", "잡수", "들어가", "쉬", "반갑", "알", "맞", "아니", "뭐", "그", "이", "누구", "어디", "왜", "마", "말"} # "안녕하세요", "들어가세요", "뭐라고?": kept as said KO_SOCIAL_BEFORE = re.compile(r"(?:안녕히|조심히|살펴|잘|편히|어서)\s*$") # "안녕히 가세요", "어서 오세요": greetings KO_L_DROP = {"도": "돌", "흔드": "흔들", "여": "열", "내미": "내밀", "미": "밀", "도시": "돌"} # 도세요 is 돌다's _END = r"(?=(?:\s+(?:빨리|얼른|어서|좀|제발|당장|지금|please|now|루미야?|로봇아?))*\s*(?:[,.!?~…]|$))" # a clause's end _END_NOT_ASKED = r"(?=(?:\s+(?:빨리|얼른|어서|좀|제발|당장|지금|please|now|루미야?|로봇아?))*\s*(?:[,.!~…]|$))" _HON = r"(?:세요|세용|세여|십시오|시오|셔요|시죠|실래요|실래|시겠어요|시겠습니까|시겠어|시게|이소(?:마|예)?|거라|너라|렴|라고요?|랑께요?|래이|이라|쇼|시라|시지요|실래예|씨요|세예)" _LOOK_AT = r"(왼쪽|오른쪽|위|위쪽|아래|아래쪽|뒤|뒤쪽|앞|하늘|천장|바닥|여기|이쪽|저쪽|나|날|저|정면|좌측|우측)" KO_PLAIN = [ (re.compile(r"(?<=아니 )(\S+?)(?:이요|요)(?=\s*(?:[,.!?~…]|$))"), r"\1"), # "아 아니 정면이요": the noun (re.compile(r"오른쭉|오른족"), "오른쪽"), (re.compile(r"(? 이동해 (re.compile(r"(\S+) ?주(?:시|)면 (?:돼요?|됩니다|돼|좋겠(?:어요?|습니다|다|네요)|고맙겠(?:어요?|습니다)|감사하겠(?:어요?|습니다)|" r"감사(?:하겠습니다|드리겠습니다|해요)?)" + _END), r"\1 줘"), # "가 주면 돼", "주시면 감사하겠습니다" (re.compile(r"(? 가, 숙일 -> 숙이, 집을 -> 집, 들 -> 들, 돌 -> 돌; None when it is none.""" if word.endswith("을") and len(word) > 1: return word[:-1] j = _jamo(word[-1]) if not j or j[2] != 8: return None stem = word[:-1] + _syl(j[0], j[1]) return {"드": "들", "도": "돌", "여": "열", "미": "밀", "내미": "내밀", "흔드": "흔들"}.get(stem, stem) def _can_you(m, kind): """"홈으로 갈 수 있을까요?", "고개 숙일 수 있겠어요?", "가는 거 돼요?": a polite request -> "가 줘?".""" w = m.group(1) if w in ("그럴", "이럴", "저럴", "알", "될", "있을", "없을", "믿을", "잘", "그렇게", "어떻게", "그러는", "이러는"): return m.group(0) # "그럴 수 있을까요?", "알 수 있을까요?": no request stem = _stem_l(w) if kind == "CAN_L" else (w[:-1] if w.endswith("는") and len(w) > 1 else None) if kind != "CAN_L" and stem: stem = {"도": "돌", "드": "들", "여": "열", "흔드": "흔들", "내미": "내밀"}.get(stem, stem) if not stem or stem in KO_SOCIAL: return m.group(0) if stem in ("주", "주시"): return "줘?" # "가 줄 수 있을까요?": the 줘 is said already return aform(stem) + " 줘?" def _stem_aform(word): """'따라오' -> '따라와', with KO_SOCIAL kept as they are and 도 -> 돌아 (KO_L_DROP); None when kept.""" if word in KO_SOCIAL or word.split()[-1] in KO_SOCIAL: return None return aform(KO_L_DROP.get(word, word)) def ko_plain(t): """Korean request endings of every speech level and dialect as one plain form, so the readers see one wording: "가세요" / "가십시오" / "가요" / "가라" / "가이소" -> "가"; "따라오세요" / "따라온나" / "따라오라고" -> "따라와"; "가 주세요" / "가 주실래요?" / "가 주시겠습니까?" / "가 주면 돼" / "가 주시면 감사하겠습니다" -> "가 줘"; "도세요" -> "돌아"; "흔드세요" -> "흔들어"; "해부러" / "혀" -> "해". Greetings ("안녕하세요", "들어가세요") stay; questions keep their "?".""" if not t or not re.search(r"[가-힣]", t): return t for rx, to in KO_PLAIN: if to in ("CAN_L", "CAN_N"): t = rx.sub(lambda m: _can_you(m, to), t) elif to == "STEM": def stem(m): w = m.group(1) if KO_SOCIAL_BEFORE.search(m.string[:m.start()]): return m.group(0) got = _stem_aform(w[:-1] if w.endswith("으") and len(w) > 1 and _jamo(w[-2]) and _jamo(w[-2])[2] else w) return got if got else m.group(0) t = rx.sub(stem, t) else: t = rx.sub(to, t) return t def number_slips(t): """Whisper's slips of a distance in a move command (NUMBER_SLIPS); text without a move is left as it is.""" if not MOVE_SAID.search(t): return t for slip, said in NUMBER_SLIPS: if "to me" in slip and NOT_MOVE.search(t): continue t = re.sub(slip, said, t) return t def norm(text, names=()): """Lower case, whisper's slips (ASR_SLIPS), Konglish directions (KONGLISH), numbers first ("point five meters" is a distance), then marker names (names: the map's).""" t = " " + (text or "").lower().replace("’", "'") + " " t = re.sub(r"(\S+)[—–-]+\s*(?=\1)", "", t) # a stutter: "육십 센— 센티", "go ah— ahead" t = re.sub(r"(?<=[가-힣])(?=\d+(?:\.\d+)?\s*(?:미터|센티|센치|cm|m\b|도))", " ", t) # "오른쪽으로80센티미터" for slip, said in ASR_SLIPS + KONGLISH: t = re.sub(slip, said, t) t = number_slips(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:])))