Improve command understanding with bigger wording banks; tested
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
"""Marker names said in other ways than wording.py reads them: the other language ("information desk" for 안내데스크,
|
||||
"레지스트레이션" for Registration), the words of a two-word name the other way round ("에이 부스" for booth_A, "삼 번 부스"
|
||||
for 부스3), letters and numbers as said ("booth bee", "부스 십이", "gate won"), and whisper's spellings ("hole A", "카패").
|
||||
|
||||
apply(t, markers) writes them as the marker's written name (wording.written) in text already normalised by
|
||||
wording.norm, so every reader after it (brain.named_place, the go rules, the intent bank) sees the marker.
|
||||
Every word of the name must be said: "booth" alone is no booth_A.
|
||||
|
||||
python assistant/place_words.py conference "레지스트레이션으로 가 줘"
|
||||
"""
|
||||
import functools
|
||||
import re
|
||||
import sys
|
||||
|
||||
from assistant import wording
|
||||
|
||||
EN_KO = { # an English word of a name -> how it is said in Korean, and other spellings
|
||||
"registration": ["레지스트레이션", "레지스트레이숀", "등록"], "gallery": ["갤러리", "겔러리", "갤러리이"],
|
||||
"information": ["인포메이션", "안내"], "info": ["인포", "information", "인포메이션", "안내"],
|
||||
"desk": ["데스크", "대스크", "데스끄"], "cafe": ["카페", "카패", "까페", "café"], "coffee": ["커피", "코피"],
|
||||
"corner": ["코너"], "elevator": ["엘리베이터", "엘레베이터", "엘베"], "lift": ["리프트"], "entrance": ["엔트런스", "입구"],
|
||||
"exit": ["엑시트", "엑싯", "출구"], "stage": ["스테이지", "무대"], "main": ["메인"], "booth": ["부스", "boot", "bootH"],
|
||||
"hall": ["홀", "hole", "hol"], "gate": ["게이트", "gait"], "photo": ["포토", "foto", "사진"], "zone": ["존", "zon"],
|
||||
"lounge": ["라운지", "휴게실"], "lobby": ["로비"], "bar": ["바"], "table": ["테이블", "table"], "front": ["프론트", "프런트", "앞"],
|
||||
"restroom": ["화장실"], "toilet": ["화장실"], "meeting": ["미팅", "회의"], "room": ["룸", "실"], "shop": ["샵", "숍"],
|
||||
"gift": ["기프트", "기념품"], "dock": ["도크", "독"], "demo": ["데모"], "lab": ["랩"], "office": ["오피스", "사무실"],
|
||||
}
|
||||
KO_EN = { # a Korean word of a name -> how it is said in English
|
||||
"안내": ["information", "info", "인포", "인포메이션"], "데스크": ["desk", "대스크"], "커피": ["coffee"], "코너": ["corner"],
|
||||
"포토": ["photo", "foto"], "존": ["zone"], "입구": ["entrance", "엔트런스"], "출구": ["exit", "엑시트"],
|
||||
"무대": ["stage", "스테이지"], "휴게실": ["lounge", "break room", "rest area", "라운지"], "회의실": ["meeting room", "conference room"],
|
||||
"화장실": ["restroom", "toilet", "bathroom", "washroom"], "앞": ["front"], "부스": ["booth"], "라운지": ["lounge"],
|
||||
"기념품점": ["gift shop", "souvenir shop"], "로비": ["lobby"], "카페": ["cafe", "café", "카패"], "갤러리": ["gallery"],
|
||||
}
|
||||
LETTER = {"a": ["a", "에이", "ay", "에이에"], "b": ["b", "bee", "be", "비", "삐"], "c": ["c", "see", "sea", "씨"],
|
||||
"d": ["d", "dee", "디"], "e": ["e", "이"]}
|
||||
NUM = {1: ["one", "won", "원", "일"], 2: ["two", "투", "이"], 3: ["three", "쓰리", "스리", "삼"],
|
||||
4: ["four", "포", "사"], 5: ["five", "파이브", "오"], 6: ["six", "식스", "육"], 7: ["seven", "세븐", "칠"],
|
||||
8: ["eight", "에잇", "팔"], 9: ["nine", "나인", "구"], 10: ["ten", "텐", "십"], 11: ["eleven", "십일"],
|
||||
12: ["twelve", "트웰브", "십이"], 13: ["thirteen", "십삼"], 14: ["fourteen", "십사"], 15: ["fifteen", "십오"]}
|
||||
SEP = r"[\s_.-]*(?:(?:번|number|no\.?|넘버)\s*)?" # between the words: "booth 3", "booth_3", "삼 번 부스"
|
||||
|
||||
|
||||
def _segments(tok):
|
||||
"""A Korean word cut into the known words of KO_EN ("커피코너" -> 커피, 코너), else the word whole."""
|
||||
out, rest = [], tok
|
||||
while rest:
|
||||
w = next((k for k in sorted(KO_EN, key=len, reverse=True) if rest.startswith(k)), None)
|
||||
if not w:
|
||||
return [tok]
|
||||
out.append(w)
|
||||
rest = rest[len(w):]
|
||||
return out
|
||||
|
||||
|
||||
def _alts(tok, alone):
|
||||
if tok.isdigit():
|
||||
return [tok] + NUM.get(int(tok), [])
|
||||
if tok in LETTER:
|
||||
return LETTER[tok] if not alone else [tok]
|
||||
return [tok] + EN_KO.get(tok, []) + KO_EN.get(tok, [])
|
||||
|
||||
|
||||
@functools.lru_cache(maxsize=64)
|
||||
def _patterns(markers):
|
||||
out = []
|
||||
for m in markers:
|
||||
if re.fullmatch(r"p\d+|home", m.lower()):
|
||||
continue
|
||||
toks = []
|
||||
for tok in re.findall(r"[a-z]+|\d+|[가-힣]+", m.lower()):
|
||||
toks += _segments(tok) if re.fullmatch(r"[가-힣]+", tok) else [tok]
|
||||
if not toks:
|
||||
continue
|
||||
parts = ["(?:" + "|".join(sorted({re.escape(a).replace(r"\ ", r"\s?") for a in _alts(t, len(toks) == 1)},
|
||||
key=len, reverse=True)) + ")" for t in toks]
|
||||
orders = [parts] + ([parts[::-1]] if len(parts) == 2 else [])
|
||||
body = "|".join(SEP.join(p) for p in orders)
|
||||
out.append((m, re.compile(rf"(?<![a-z0-9_가-힣])(?:the )?(?:{body})(?![a-z0-9_])(?:(?<=[a-z0-9])|{wording._KO_END})")))
|
||||
return out
|
||||
|
||||
|
||||
def apply(t, markers=()):
|
||||
"""Normalised text with the markers said in other ways written as their names."""
|
||||
if not markers or not t:
|
||||
return t
|
||||
for m, rx in _patterns(tuple(markers)):
|
||||
name = wording.written(m)
|
||||
t = rx.sub(lambda x: x.group(0) if x.group(0).strip() == name else name, t)
|
||||
return t
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
from tests.blind.maps import MAPS
|
||||
print(apply(wording.norm(" ".join(sys.argv[2:]), MAPS[sys.argv[1]]), MAPS[sys.argv[1]]))
|
||||
Reference in New Issue
Block a user