From 446d824e21e14cedb9c3a11f76d963595469e063 Mon Sep 17 00:00:00 2001 From: Cutshion Date: Mon, 26 Oct 2026 16:15:10 +0900 Subject: [PATCH] Improve memory recall and where-are-you answers Co-Authored-By: Claude Opus 5.5 --- assistant/memory.py | 90 ++++++++++++++++++++++++++++--------------- tests/memory_check.py | 2 + tests/test_memory.py | 58 +++++++++++++++++++++++++--- 3 files changed, 112 insertions(+), 38 deletions(-) diff --git a/assistant/memory.py b/assistant/memory.py index 71dc744..9e7d9e3 100644 --- a/assistant/memory.py +++ b/assistant/memory.py @@ -231,7 +231,7 @@ HAND_Q = re.compile( # questions about the hand / arm now (not orders: "open y r"\bis your (?:hand|gripper|arm) (?:open|opened|closed|shut|empty|free|full|out|up|down|folded|stretched)\b|" r"\b(?:your )?(?:hand|gripper) (?:is )?(?:open|opened|close|closed)(?: or (?:open|opened|close|closed))?\s*\?|" r"\b(?:are|do) you (?:holding|have|carrying) (?:anything|something)(?: in your (?:hand|gripper))?\b|" - r"\bwhat(?:'s| is) (?:in|on) your (?:hand|gripper)\b|\bstate of your (?:hand|arm|gripper)\b|" + r"\bwhat(?:'s| is) (?:in|on) your (?:hand|gripper)\b|\bwhat (?:are )?you (?:holding|carrying)\b|\b(?:is (?:there )?)(?:anything|something) in your (?:hand|gripper)\b|\bstate of your (?:hand|arm|gripper)\b|" r"(?:손|팔|그리퍼|arm|hand|gripper)(?:은|는|이|가)?\s*(?:지금\s*)?(?:어디|어떻게|(?:무슨|어떤)\s*상태|상태)|" r"(?:손|그리퍼)(?:은|는|이|가)?\s*(?:지금\s*)?(?:펴져|펼쳐져|벌어져|열려|닫혀|오므려져|쥐어져|비어|비었|펴졌|열렸|닫혔|오므려졌|쥐어졌)\s*(?:있)?(?:어|니|나|나요|어요|습니까|있어|있니|있나요|있어요|냐|노|나요)?\s*\?|" r"(?:팔|arm)(?:은|는|이|가)?\s*(?:지금\s*)?(?:어떤|무슨)\s*자세|" @@ -259,9 +259,15 @@ def _held_ko(name): return (colour + " " if colour else "") + (seeing.KO.get(thing) or ("물건" if thing in ("something", "") else thing)) -PLACE_Q = re.compile(r"^\W*(?:(?:lumi|hey|so|and|okay)\W+)*(?:where are you(?: now| right now| standing)?|where you are|where are you at)\W*$|" - r"\bwhich (?:marker|point|place) are you (?:at|near)\b|(?:너|루미|루미야)?\s*지금\s*어디(?:야|에 있어|에 있어요|예요|에요|입니까|에 계세요)\W*$|" - r"^\W*(?:너|루미야?)?\s*어디(?:에)? 있(?:어|어요|니|습니까)\W*$", re.I) +PLACE_Q = re.compile( # "where are you (now)?" at every level; not "where are you from / going / looking", "어디 가?", "어디 보고 있어?" + r"^\W*(?:(?:lumi|hey|so|and|okay|ok|um|uh|hmm|wait|right|robot|well)\W+)*(?:where are you|you are where|where you are|where're you|where you at)" + r"(?:\W+(?:now|right now|at|standing|at the moment|currently))*\W*$|" + r"\b(?:which|what) (?:marker|point|place|spot|position)(?: on the map)? are you (?:at|near|in)\b|\bwhere on the map are you\b|\b(?:tell me|say) where you are\b|\bwhat(?:'s| is) your (?:current )?(?:location|position)\b|" + r"(?:지금|현재)\s*(?:너의?|네|루미)?\s*위치(?:가|를|는|좀|\s)*(?:알려|말해|어디|뭐)|" + r"^\W*(?:(?:너|루미야?|루미 ?씨|로봇아?|자네|음|어|저기|근데|그럼|그런데)\W*)*(?:(?:지금|현재)\s*)?" + r"(?:있는\s*(?:곳|데|위치)|위치|map에서|맵에서|지도에서)?(?:은|는|이|가)?\s*" + r"(?:어딘|어딨|(?:어디|어데|어느\s*(?:위치|곳|쪽|마커))(?!\s*(?:로|를|서|까지|에서)?\s*(?:보|가|갈|갔|와|왔|봐|향|돌|출신|고향|나라|사람)))", re.I) +_PLACE_SAID = re.compile(r"지금|현재|너|루미|있는\s*(?:곳|데|위치)|위치|map|맵|지도|계시|계세|계십|있어|있니|있습|있는가|있나", re.I) # whose place: Lumi's LIFT_Q = re.compile(r"\bhow (?:high|tall) (?:is your (?:lift|body|torso)|are you raised|is your body raised)\b|\bis your (?:lift|body) (?:up|down|raised)\b|" r"\b(?:lift|body) (?:height|position)\b|(?:몸|리프트|허리)(?:이|가|은|는)?\s*(?:지금\s*)?(?:얼마나|몇)\s*(?:올라|높|내려)|" r"리프트\s*(?:높이|위치)", re.I) @@ -273,10 +279,10 @@ def self_answer(text, lang="en"): if time.time() - _state["at"] > 10 * STATE_S: return None ko = lang == "ko" - if PLACE_Q.search(text): - return None if not _state.get("place") else \ - ("지금 홈에 있어요." if _state["place"] == "home" else f"지금 {_state['place']} 근처에 있어요.") if ko \ - else f"I'm at {_state['place']} right now." + if PLACE_Q.search(text) and (text.isascii() or _PLACE_SAID.search(text)): + place = (_state.get("place") or "").replace("_", " ") # "Gate_A3" is said "Gate A3" + return None if not place else ("지금 홈에 있어요." if place == "home" else f"지금 {place} 근처에 있어요.") if ko \ + else f"I'm at {place} right now." if LIFT_Q.search(text): mm = _state.get("lift") if mm is None: @@ -534,15 +540,15 @@ def talk_over(history): RECALL_Q = re.compile( # a question about an earlier talk; "I remember when ..." / "last time I was here" are not - r"\b(?:do|did|can|could|don'?t|didn'?t|you) (?:you )?(?:still |even )?(?:remember|recall)\b|^\W*(?:\w+\W+)?(?:remember|recall)\b|" - r"\bwe (?:talked|spoke|chatted|discussed|were talking)\b|\bour (?:talk|chat|conversation)\b|" + r"\b(?:do|did|can|could|don'?t|didn'?t|you) (?:you )?(?:still |even |happen to )?(?:remember|recall)\b|^\W*(?:\w+\W+)?(?:remember|recall)\b|" + r"\bwe (?:talked|spoke|chatted|discussed|were (?:talking|chatting|discussing|speaking))\b|\b(?:any|have(?: a| any)?) memory of (?:us|me|our|we|you)\b|\bremember or (?:no|not)\b|\bour (?:talk|chat|conversation)\b|" r"\b(?:what|which) did (?:we|i) (?:talk|speak|chat|say|tell|ask)\b|\bwhat did you (?:tell|say to) me about\b|" - r"\bwhat (?:did )?we (?:talk|talked|discuss|discussed|chat|chatted)\b|\bdid (?:we|i) (?:talk|speak|chat|tell you|ask you|mention)\b|" - r"\bearlier (?:i|we) (?:said|told|asked|talked)\b|\bremember(?: (?:that|this|it))?\W*$|\bring a bell\b|\bremind me\b|\bwe had (?:a|our|that) (?:talk|chat|conversation)\b|" - r"\bdid we have (?:a|any) (?:talk|chat|conversation)\b|\bdid i (?:tell|give) you\b|\bwhat (?:we|you) (?:said|told me|talked about)\b|" + r"\bwhat (?:did )?we (?:talk|talked|discuss|discussed|chat|chatted)\b|\bdid (?:we|i) (?:not )?(?:talk|speak|chat|tell you|ask you|mention)\b|" + r"\bearlier (?:i|we) (?:said|told|asked|talked)\b|\bremember(?: (?:that|this|it))?\W*$|\bring (?:a|any) bells?\b|\bremind me\b|\bwe had (?:a|our|that) (?:talk|chat|conversation)\b|" + r"\b(?:did|didn'?t|did not) we (?:have|had) (?:a|any|our|that) (?:talk|chat|conversation)\b|\b(?:didn'?t|did not) (?:we|i) (?:talk|speak|chat|discuss|mention|tell you)\b|\bdid i (?:tell|give) you\b|\bwhat (?:we|you) (?:said|told me|talked about)\b|" r"(?:말해|알려|얘기해|이야기해) ?(?:줬|주었|드렸)(?:던|었|지|잖|죠)|(?:했던|나눴던|한) (?:얘기|이야기|대화)|" r"(?:얘기|이야기|대화)\s*(?:\S+\s+)?(?:했잖|했었|했던|나눴)|무슨\s*(?:topic|주제)(?:였|이었)|" - r"기억\s*(?:이\s*)?(?:나|해|하|날)|생각\s*(?:이\s*)?나|" + r"기억\s*(?:이\s*)?(?:나|해|하|날|허)|생각\s*(?:이\s*)?나|(?:얘기|이야기|대화)(?:를|을)?\s*(?:하지 않았|안 했|나누지 않았)\S*\s*\?|" r"(?:얘기|이야기|대화|말)(?:를|을)?\s*(?:했던|했었|나눴|나누었|나누셨|하셨|한 거|했지|했잖|했는|했죠|했어|했노|했나|했니|했냐|했더라|했습니까|했을까)|" r"\b(?:remember|recall)\s*(?:해|하|나|돼|되)|\b(?:talk|chat|conversation)\s*(?:했던|했었|한 거|나눴)", # Konglish: "remember 해?" re.I) @@ -552,7 +558,7 @@ GENERIC = re.compile(r"\b(?:what|which) did (?:we|i) (?:talk|speak|chat|say)(?: r"무슨 ?(?:얘기|이야기|대화|말)|뭐(?:라고)? ?(?:얘기|이야기|말)", re.I) FORGET = re.compile( # an order to forget: "did you forget me?", "제 이름 잊어버렸어요?", "I forget what we said" are not r"(?= 0.99 or hit and cos >= RECALL_PART or cos >= RECALL_MIN: out.append((hit, cos, s)) return [s for _, _, s in sorted(out, key=lambda x: (-x[0], -x[1]))] @@ -725,11 +741,14 @@ def recall(text, lang="en", now=None, embed=None, history=()): past = list(history[:-1] if history and history[-1][0] == text else history) about = topic(text) other_q = HAND_Q.search(text) or SEEN_Q.search(text) or PLACE_Q.search(text) or LIFT_Q.search(text) - if _recall["offered"] and now - _recall["at"] < OFFER_WAIT_S and not other_q: + own = [w for w in about.split() if len(w) > 2 or len(w) > 1 and not w.isascii()] + new_q = _asked_days(text) == "too old" or _seen_more(text) or _recall["offered"] and RECALL_Q.search(text) and \ + not NOT_RECALL.search(text) and own and _hits(own, _recall["offered"][-1]) < 0.5 # "Right, did we talk about jaka?" is no "right" + if _recall["offered"] and now - _recall["at"] < OFFER_WAIT_S and not other_q and not new_q: yes = brain.confirm_answer(text) - if yes == "yes" or yes == "correction" and re.match(r"\W*(?:yes|yeah|yep|right|exactly|네|예|응|맞아|맞습니다|맞아요|그래|그렇|그거)", text, re.I): + if yes == "yes" or yes == "correction" and re.match(r"\W*(?:yes|yeah|yep|yup|right|exactly|correct|mm-?hmm|uh-?huh|that'?s (?:the one|it|right)|네|예|응|ㅇㅇ|맞|그래|그렇|그거)", text, re.I): return _told(_recall["offered"][-1], lang, turn=len(past)) - if yes in ("no", "cancel") or yes == "correction" and re.match(r"\W*(?:no|nope|not that|아니|아뇨)\b", text, re.I): + if yes in ("no", "cancel") or yes == "correction" and re.match(r"\W*(?:(?:no|nope|nah|not that|not really|wrong)\b|아니|아뇨|아닙|아냐|ㄴㄴ|아이다)", text, re.I): if yes == "cancel": _recall.update(offered=[], left=[]) return None @@ -740,12 +759,15 @@ def recall(text, lang="en", now=None, embed=None, history=()): return _offer(s, lang, first=False, now=now) _recall.update(offered=[], left=[], talk=None) return CANT_REMEMBER[lang] + if _talk["skip"] and not _person() and (DO_SAID.search(text) or RECALL_Q.search(text)) and not NOT_RECALL.search(text): + return CANT_REMEMBER[lang] # "forget this" said, nobody known to forget: nothing is recalled if DO_SAID.search(text) and not NOT_DO.search(text): out = do_said(text, lang, now) _recall["turn"] = len(past) return out recent = _recall.get("talk") and len(past) - _recall.get("turn", -99) <= RECENT_TURNS - if recent and DETAIL.search(text) and (not about or _hits(about.split(), _recall["talk"]) >= 0.5): # not "back to chess: what was that?" + if recent and DETAIL.search(text) and not TOPICS_Q.search(text) and (not about or _hits(about.split(), _recall["talk"]) >= 0.99): + # not "back to chess: what was that?", not "제주도 여행" for the talk "부산 여행" (that one is offered), not "주제가 뭐였어?" return _told(_recall["talk"], lang, detail=True, turn=len(past)) # "remind me what you said", "결론이 뭐였어요?" if NAME_Q.search(text): return name_answer(lang) @@ -753,7 +775,9 @@ def recall(text, lang="en", now=None, embed=None, history=()): return None days = _asked_days(text) words = [w for w in about.split() if len(w) > 2 or len(w) > 1 and not w.isascii()] - if words and days in (None, {0}) and any(w in f"{q} {a}".lower() for q, a in past if not RECALL_Q.search(q) for w in words): + said_now = " ".join(f"{q} {a}" for q, a in past if not RECALL_Q.search(q)).lower() + if days in (None, {0}) and any(w in said_now if len(w) > 1 else re.search(rf"(? "what we talked", "is ur hand open" -> "is your hand open".""" text = re.sub(r"(?