From 1e1862e12016faca173c88028648fec4213e0047 Mon Sep 17 00:00:00 2001 From: Cutshion Date: Mon, 26 Oct 2026 00:37:18 +0900 Subject: [PATCH] Improve Korean request reading in every speech level Co-Authored-By: Claude Opus 5.5 --- assistant/wording.py | 155 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 155 insertions(+) diff --git a/assistant/wording.py b/assistant/wording.py index f3edf6e..eab45b9 100644 --- a/assistant/wording.py +++ b/assistant/wording.py @@ -115,6 +115,9 @@ def numbers(t): 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", @@ -160,6 +163,10 @@ def numbers(t): t = re.sub(r"(? 가, 오 -> 와, 따라오 -> 따라와, + 주 -> 줘, 기다리 -> 기다려, 하 -> 해, 집 -> 집어, 잡 -> 잡아, 놓 -> 놔, 누르 -> 눌러, 끄 -> 꺼.""" + 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) + 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): @@ -345,6 +498,8 @@ 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)