"""Short drives and the drive speed as people say them, in English, Korean and mixed, with whisper's slips. which way and how far: "come back 20 cm", "move back 5 centimeters", "20센티 뒤로", "앞으로 반 미터", "back up a tiny bit", "a hair to the left", "살짝 뒤로", "한 뼘 앞으로", "two steps back", "반 걸음 뒤로" faster / slower: "a little faster", "can you pick up the pace", "not so fast", "ease off", "slow it down a notch", "way too slow", "너무 빨라", "좀 천천히 가 줄래요", "속도 줄여" after one of them: "more", "again", "a bit more", "same again", "조금 더", "한 번 더", "just a few centimetres more" Reads text normalised by brain._norm (wording.norm: "20 cm" -> "0.2 m", "이십 센티" -> "0.2 m", "two" -> "2"). Nothing is guessed: a number said without a unit that is read is asked about, a drive past MOVE_MAX_M is refused with the limit, "more" with nothing before it is asked about. python assistant/driving.py "back up a tiny bit" """ import re import sys MOVE_MAX_M = 3.0 # m; the longest short drive (lumi.MAX_MOVE_M); longer is refused with this limit MOVE_MIN_M = 0.01 # m; a shorter drive is refused DEFAULT_M = 1.0 # m; "move back" with no amount (as before) LITTLE_M = 0.5 # m; "a little", "a bit", "조금", "좀" (as before) TINY_M = 0.05 # m; "a tiny bit", "a hair", "a few centimetres", "살짝"; estimate SPAN_M = 0.2 # m; "한 뼘", one hand span; estimate STEP_M = 0.5 # m; one step ("two steps", "한 걸음", "반 걸음"); estimate of a person's step TURN_MORE_DEG = 15.0 # deg; "a bit more" right after a turn: this much further the same way DIRS = (("back", r"\bback(?:wards?)?\b(?! down\b)|\bbackward\b|\breverse\b|^(?:let'?s )?(?:bake|big)\b(?= \d| half| a )|뒤로|뒤쪽|후진|" r"물러"), # whisper's "let's bake / big one meter" ("forward", r"\bforwards?\b|\bahead\b|앞으로|앞쪽|전진"), ("left", r"\b(?:to the |to your )?left\b|왼쪽|좌측"), ("right", r"(? MOVE_MAX_M or n < MOVE_MIN_M: return ("refuse", f"{d} {n:g}") return ("move", f"{d} {n:g}", exact) def last_command(history): """The move, turn, speed change, look or lift of the turn just before, from brain history ("ACTION: move back 0.2", "ACTION: look left" -> ("look", "left small")), else None.""" if not history: return None m = re.match(r"ACTION: (look|lift) (left|right|up|down)\b", history[-1][1] or "") if m: # "look left" then "one more": a little further that way return (m.group(1), f"{m.group(2)} small") m = re.match(r"ACTION: (move|speed|turn) (\S+)(?: (\S+))?", history[-1][1] or "") if not m: return None if m.group(1) == "turn": # "turn left" then "again": the same turn return ("turn", "around 180" if m.group(2) == "around" else f"{m.group(2)} {m.group(3) or 90}") if m.group(1) == "speed": return ("speed", {"faster": 1, "slower": -1}.get(m.group(2)) or int(m.group(2))) return ("move", f"{m.group(2)} {m.group(3)}", True) if m.group(3) else None def talking(history): """The turn before was conversation (not a command): a bare "more" belongs to it.""" return bool(history) and bool(re.match(r"ACTION: none\b|(?!ACTION:)", history[-1][1] or "")) def spoken(m, lang="en"): """0.2 -> "20 centimetres" / "20센티", 1.5 -> "1.5 metres" / "1.5미터".""" if m >= 1: return f"{m:g} metre{'' if m == 1 else 's'}" if lang == "en" else f"{m:g}미터" if m >= 0.01: cm = round(m * 100, 1) return f"{cm:g} centimetre{'' if cm == 1 else 's'}" if lang == "en" else f"{cm:g}센티" return f"{m * 1000:g} millimetres" if lang == "en" else f"{m * 1000:g}밀리" KO_DIR = {"forward": "앞으로", "back": "뒤로", "left": "왼쪽으로", "right": "오른쪽으로"} def say_move(d, m, exact, lang="en"): """The read-back of a drive; "about" when the amount was not said exactly.""" if lang == "en": return f"Okay, moving {d} {'' if exact else 'about '}{spoken(m)}." return f"네, {KO_DIR.get(d, '')} {'' if exact else '약 '}{spoken(m, 'ko')} 갈게요." def say_refused(d, n, lang="en"): if n < MOVE_MIN_M: return ("Sorry, that's too short for me to drive." if lang == "en" else "죄송해요, 너무 짧아서 갈 수 없어요.") return (f"Sorry, I can only drive up to {spoken(MOVE_MAX_M)} at a time." if lang == "en" else f"죄송해요, 한 번에 {spoken(MOVE_MAX_M, 'ko')}까지만 갈 수 있어요.") def say_ask(d, lang="en"): if not d: return ("More of what? Please tell me what to do." if lang == "en" else "무엇을 더 할까요? 다시 말씀해 주세요.") return (f"How far should I go {d}? Please say it with a unit, like 20 centimetres." if lang == "en" else f"{KO_DIR.get(d, '')} 얼마나 갈까요? 20센티처럼 단위와 함께 말씀해 주세요.") def say_speed(sign, factor, changed, lang="en"): """The answer to a speed change: the new speed in plain words.""" pct = round(factor * 100) if not changed: return (("I'm already at my top speed." if sign > 0 else "I'm already at my slowest.") if lang == "en" else ("이미 가장 빠른 속도예요." if sign > 0 else "이미 가장 느린 속도예요.")) if pct == 100: return "Okay, back to my normal speed." if lang == "en" else "네, 보통 속도로 갈게요." if lang == "en": return f"Okay, a bit {'faster' if sign > 0 else 'slower'}: {pct} percent of my normal speed." return f"네, 조금 더 {'빨리' if sign > 0 else '천천히'} 갈게요. 보통 속도의 {pct}퍼센트예요." if __name__ == "__main__": from assistant import brain print(read(brain._norm(" ".join(sys.argv[1:]))))