From b85c320caeb0fb78f70aa1bb22ad7011d5762f14 Mon Sep 17 00:00:00 2001 From: Cutshion Date: Fri, 23 Oct 2026 16:27:29 +0900 Subject: [PATCH] =?UTF-8?q?Weather=20follow-ups=20keep=20the=20topic=20and?= =?UTF-8?q?=20place=20and=20move=20the=20day=20(tomorrow,=20next=20Monday,?= =?UTF-8?q?=20=EB=AA=A8=EB=A0=88,=20the=20weekend);=20only=20the=20asked?= =?UTF-8?q?=20days=20reach=20the=20model;=20one=20"Let=20me=20check"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- assistant/web.py | 68 ++++++++++++++++++ tests/dialogues_weather.py | 136 +++++++++++++++++++++++++++++++++++ tests/followup_cases.py | 18 ++--- tests/followup_cases_more.py | 12 ++-- tests/test_web.py | 23 +++++- 5 files changed, 240 insertions(+), 17 deletions(-) create mode 100644 tests/dialogues_weather.py diff --git a/assistant/web.py b/assistant/web.py index 2ae2517..d0bbc5d 100644 --- a/assistant/web.py +++ b/assistant/web.py @@ -122,6 +122,74 @@ def weather(place=DEFAULT_PLACE): return None +WEEKDAYS = {"monday": 0, "tuesday": 1, "wednesday": 2, "thursday": 3, "friday": 4, "saturday": 5, "sunday": 6, + "월": 0, "화": 1, "수": 2, "목": 3, "금": 4, "토": 5, "일": 6} +NUMBER_WORDS = {"one": 1, "two": 2, "three": 3, "four": 4, "five": 5, "six": 6, "seven": 7, "이틀": 2, "사흘": 3, "나흘": 4} +DAY_AFTER = re.compile(r"\bthe (?:day after|next day|following day)\b(?! tomorrow)|그 ?다음 ?날|다음 ?날|그 ?담 ?날", re.I) +DAY_BEFORE = re.compile(r"\bthe day before\b(?! yesterday)|그 ?전 ?날", re.I) +FIXED_DAY = re.compile(r"\bday after tomorrow\b|\bday before yesterday\b|내일 ?모레|글피|그저께|그제|모레|" + r"\btoday\b|\btonight\b|\bthis (?:morning|afternoon|evening)\b|\bright now\b|\bnow\b|오늘|지금|" + r"\btomorrow\b|내일|\byesterday\b|어제", re.I) +FIXED_OFFSET = {"day after tomorrow": 2, "day before yesterday": -2, "내일모레": 2, "내일 모레": 2, "글피": 3, + "그저께": -2, "그제": -2, "모레": 2, "tomorrow": 1, "내일": 1, "yesterday": -1, "어제": -1} +IN_DAYS = re.compile(r"\bin (\d+|one|two|three|four|five|six|seven) days?\b|(\d+|이틀|사흘|나흘) ?(?:일)? ?(?:후|뒤)", re.I) +WEEKDAY = re.compile(r"\b(monday|tuesday|wednesday|thursday|friday|saturday|sunday)s?\b|([월화수목금토일])요일", re.I) +WEEKEND = re.compile(r"\bweekend\b|주말", re.I) +NEXT_WEEK = re.compile(r"\bnext\b|다음 ?주|담 ?주", re.I) # "next Monday", "다음 주 월요일", "next weekend" +WEEK = re.compile(r"\b(?:this|next) week\b|이번 ?주|다음 ?주|담 ?주", re.I) +DAY_PHRASE = re.compile("|".join(r.pattern for r in (DAY_AFTER, DAY_BEFORE, IN_DAYS, WEEKDAY, WEEKEND, WEEK, NEXT_WEEK, FIXED_DAY)) + + r"|\b(?:this|coming|on)\b", re.I) # all the words that name a day, for lumi's follow-ups + + +def asked_day(text, before=None, today=None): + """The days a question asks about, as days from today: [0] today, [1] tomorrow, [2] 모레, a weekday its next + one (today's own name is today), "next Monday" / "다음 주 월요일" the Monday of next week, "this weekend" / "주말" + Saturday and Sunday, "next week" its seven days; negative for days past. "The day after" / "다음 날" is the day + after `before` (the days asked the turn before). None when no day is named.""" + t = text or "" + wd = (today or datetime.date.today()).weekday() + before = before or [0] + if DAY_AFTER.search(t): + return [before[-1] + 1] + if DAY_BEFORE.search(t): + return [before[0] - 1] + m = IN_DAYS.search(t) + if m: + n = m.group(1) or m.group(2) + return [int(n) if n.isdigit() else NUMBER_WORDS[n.lower()]] + week = 7 if NEXT_WEEK.search(t) else 0 + m = WEEKDAY.search(t) + if m: + day = WEEKDAYS[(m.group(1) or m.group(2)).lower()] + return [7 - wd + day] if week else [(day - wd) % 7] + if WEEKEND.search(t): + return [d for d in (week + (5 - wd) % 7, week + (6 - wd) % 7) if wd != 6 or week or d == 0] # Sunday: today + if WEEK.search(t): + return list(range(7 - wd, 14 - wd)) if week else list(range(0, 7 - wd)) + m = FIXED_DAY.search(t) + if m: + return [FIXED_OFFSET.get(m.group(0).lower(), 0)] + return None + + +def weather_on(forecast, days, today=None): + """Of weather()'s text only what the days asked need: the line of each day (the weather now with today), and a + plain line for a day past or beyond the forecast, so the answer can't take another day's line.""" + lines = (forecast or "").split("\n") + if days is None or len(lines) < 2: + return forecast + today = today or datetime.date.today() + place = re.match(r"Weather in (.+?) now:", lines[0]) + out = [lines[0]] if 0 in days else [f"Forecast for {place.group(1) if place else 'here'}:"] + last = (today + datetime.timedelta(days=len(lines) - 2)).strftime("%A %d %B") + for d in days: + name = (today + datetime.timedelta(days=d)).strftime("%A %d %B") + out.append(lines[d + 1] if 0 <= d < len(lines) - 1 else + f"{name}: already past, there is no forecast for a day gone by." if d < 0 else + f"{name}: too far ahead, the forecast reaches only to {last}.") + return "\n".join(out) + + def _place(place): """{name, latitude, longitude} of a place (English or a Korean name of KO_PLACES), or None; geocoded once.""" place = en_place(place) diff --git a/tests/dialogues_weather.py b/tests/dialogues_weather.py new file mode 100644 index 0000000..3eefaa3 --- /dev/null +++ b/tests/dialogues_weather.py @@ -0,0 +1,136 @@ +"""Weather talks of several turns (English, Korean, mixed): a follow-up keeps the topic and the place and moves the +day ("and tomorrow?", "모레는요?", "next Monday?", "what about Busan?", "the weather then?" after a date answer); a +day past or beyond the 7-day forecast is said plainly. Checked without the internet or the LLM, on a fake forecast +with today fixed (Friday 23 October 2026): the route lumi.answer() takes (keyword rule, else brain._after on the +model's "none" or "search"), the place, the days (web.asked_day: 0 today, 1 Saturday ... 6 Thursday) and the forecast +lines the answer is made from. python tests/dialogues_weather.py (also run by tests/test_web.py) + +W(text, place, days): a weather lookup at `place` ("" = here, Seoul) for `days` (None = the whole forecast). +S(text, said): a turn answered by talk (the model's own answer, e.g. a date). N(text): not a weather lookup. +""" +import datetime + +from assistant import brain, web + +TODAY = datetime.date(2026, 10, 23) # a Friday +MARKS = ["home", "dock2", "p1", "p2"] + + +def W(text, place, days, llm="none"): + return {"text": text, "place": place, "days": days, "llm": llm} + + +def S(text, said): + return {"text": text, "said": said} + + +def N(text): + return {"text": text, "place": None} + + +FAR, PAST = "too far ahead", "already past" +DIALOGUES = [ + # the owner's talk of 2026-10-23 15:37 (logs/lumi.log), Korean and English + [W("오늘 날씨 어때요?", "", [0]), W("내일 어때요?", "", [1], "search"), W("다음 주 월요일 어때요?", "", [3], "search"), + W("날씨 어때요?", "", [3])], + [W("How is the weather today?", "", [0]), W("And tomorrow?", "", [1]), W("What about next week Monday?", "", [3]), + W("And the weather?", "", [3])], + # a date answered first, then "the weather then?" + [S("What date is next Monday?", "Next Monday is the 26th of October."), W("And the weather then?", "", [3])], + [S("다음 주 월요일이 며칠이에요?", "다음 주 월요일은 10월 26일이에요."), W("그날 날씨는요?", "", [3])], + [S("What day is it tomorrow?", "Tomorrow is Saturday."), W("How's the weather?", "", [1])], + # the day moves, the place stays + [W("What's the weather in Busan?", "Busan", None), W("And tomorrow?", "Busan", [1]), W("And Sunday?", "Busan", [2])], + [W("부산 날씨 어때요?", "Busan", None), W("내일은요?", "Busan", [1]), W("모레는요?", "Busan", [2])], + [W("제주 날씨 알려줘", "Jeju City", None), W("그럼 토요일은 비 와요?", "Jeju City", [1])], + [W("Weather in Tokyo tomorrow?", "Tokyo", [1]), W("How about the day after?", "Tokyo", [2]), + W("And the day after that?", "Tokyo", [3])], + [W("내일 날씨 어때?", "", [1]), W("그 다음 날은?", "", [2]), W("다음날은요?", "", [3])], + # the place moves, the day stays + [W("Will it rain tomorrow?", "", [1]), W("What about Busan?", "Busan", [1]), W("And Daegu?", "Daegu", [1])], + [W("내일 비 와요?", "", [1]), W("그럼 부산은?", "Busan", [1])], + [W("What's the weather on Saturday?", "", [1]), W("What about Busan on Sunday?", "Busan", [2])], + [W("오늘 서울 날씨 어때요?", "Seoul", [0]), W("부산은 내일 어때요?", "Busan", [1])], + # weekends and weeks + [W("What's the weather this weekend?", "", [1, 2]), W("And next weekend?", "", [8, 9])], + [W("이번 주말 날씨 어때요?", "", [1, 2]), W("다음 주 수요일은요?", "", [5])], + [W("주말에 비 와요?", "", [1, 2]), W("일요일은?", "", [2])], + [W("How's the weather next week?", "", [3, 4, 5, 6, 7, 8, 9])], + [W("Will it rain this week?", "", [0, 1, 2])], + # beyond the forecast, in the past + [W("What's the weather next Friday?", "", [7]), W("And Thursday?", "", [6])], + [W("다음 주 토요일 날씨 어때요?", "", [8]), W("그럼 화요일은?", "", [4])], + [W("Weather in 10 days?", "", [10])], + [W("어제 날씨 어땠어요?", "", [-1]), W("오늘은요?", "", [0])], + [W("What was the weather yesterday?", "", [-1]), W("And tomorrow?", "", [1])], + # mixed English and Korean + [W("내일 weather 어때?", "", [1]), W("And Busan 내일은?", "Busan", [1])], + [W("What's the weather in Seoul?", "Seoul", None), W("모레는?", "Seoul", [2])], + [W("오늘 날씨?", "", [0]), W("And Monday?", "", [3])], + # in three days, today's own weekday + [W("What will the weather be in three days?", "", [3]), W("And Friday?", "", [0])], + [W("3일 후 날씨 어때요?", "", [3]), W("글피는요?", "", [3])], + # the day moves through a long talk, a remark in between ends the follow-up + [W("Weather today?", "", [0]), W("Tomorrow?", "", [1]), W("And Sunday?", "", [2]), W("What about Monday?", "", [3]), + W("And Tuesday?", "", [4])], + [W("오늘 날씨 어때요?", "", [0]), N("고마워요"), W("날씨 어때요?", "", None)], + [W("What's the weather like tomorrow?", "", [1]), N("Thanks, see you tomorrow!")], + [W("내일 기온 몇 도예요?", "", [1]), W("다음 주 월요일은요?", "", [3]), W("부산은요?", "Busan", [3])], +] + + +class _Date(datetime.date): + @classmethod + def today(cls): + return TODAY + + +def forecast(place): + days = [(TODAY + datetime.timedelta(days=i)).strftime("%A %d %B") for i in range(web.WEATHER_DAYS)] + lines = [f"Today ({days[0]})" if i == 0 else f"Tomorrow ({days[1]})" if i == 1 else d for i, d in enumerate(days)] + return f"Weather in {place or 'Seoul'} now: clear sky, 15 degrees, wind 3 km/h.\n" + \ + "\n".join(f"{n}: partly cloudy, {8 + i} to {18 + i} degrees, chance of rain {10 * i} percent." for i, n in enumerate(lines)) + + +def check(): + saved = web.datetime + web.datetime = type("dt", (), {"date": _Date, "timedelta": datetime.timedelta}) + try: + for n, talk in enumerate(DIALOGUES): + b = brain.Brain() + for turn in talk: + text, where = turn["text"], f"dialogue {n}: {turn['text']!r}" + if "said" in turn: + b.history.append((text, f"ACTION: none -\n{turn['said']}")) + continue + rule = brain.intent(text, MARKS) + if rule and rule[0] == "weather": + target = rule[1] + else: + h = b._after({"action": turn.get("llm", "none"), "target": text}, text) + target = h["target"] if h["action"] == "weather" else None + if turn["place"] is None: + assert target is None, (where, "not weather", target) + b.history.append((text, "ACTION: none -\nYou're welcome.")) + continue + assert target is not None, (where, "not routed to the weather", rule) + place = b.weather_place(target, text) + days = b.weather_on(text) + assert (place, days) == (turn["place"], turn["days"]), (where, place, days) + facts = web.weather_on(forecast(place), days, TODAY) + for d in days or []: + name = (TODAY + datetime.timedelta(days=d)).strftime("%A %d %B") + line = next((l for l in facts.split("\n") if name in l), "") + assert line and ((FAR in line) == (d >= web.WEATHER_DAYS)) and ((PAST in line) == (d < 0)), (where, facts) + if days: + assert len(facts.split("\n")) == len(days) + 1, (where, facts) # no other day's line + asked = text + brain.WEATHER_ASKED + b.history.append((asked, "It will be partly cloudy.")) + b.last_lookup = asked + finally: + web.datetime = saved + return len(DIALOGUES) + + +if __name__ == "__main__": + print(f"weather dialogues ok: {check()}") diff --git a/tests/followup_cases.py b/tests/followup_cases.py index 8e4e27d..cf83e72 100644 --- a/tests/followup_cases.py +++ b/tests/followup_cases.py @@ -170,15 +170,15 @@ CASES = [ C([PRES], "thanks", "closing", "thanks for the answer: you're welcome, silence ends"), # ---------- after the weather ---------- - C([WX], "and tomorrow?", "search", "tomorrow's weather in Seoul", r"weather.*tomorrow"), - C([WX], "Tomorrow?", "search", "tomorrow's weather in Seoul", r"weather.*Tomorrow"), - C([WX], "what about tomorrow?", "search", "tomorrow's weather in Seoul", r"weather.*tomorrow"), - C([WX], "and the weekend?", "search", "the weather this weekend", r"weather.*weekend"), - C([WX], "and the day after tomorrow?", "search", "the weather the day after tomorrow", r"weather.*tomorrow"), - C([WX], "sorry, I meant tomorrow", "search", "tomorrow's weather", r"weather.*tomorrow"), - C([WX_KO], "그럼 내일은?", "search", "내일 날씨", r"weather.*내일"), - C([WX_KO], "내일은?", "search", "내일 날씨", r"weather.*내일"), - C([WX_KO], "주말은요?", "search", "주말 날씨", r"weather.*주말"), + C([WX], "and tomorrow?", "weather", "tomorrow's weather in Seoul", r"weather.*tomorrow"), + C([WX], "Tomorrow?", "weather", "tomorrow's weather in Seoul", r"weather.*Tomorrow"), + C([WX], "what about tomorrow?", "weather", "tomorrow's weather in Seoul", r"weather.*tomorrow"), + C([WX], "and the weekend?", "weather", "the weather this weekend", r"weather.*weekend"), + C([WX], "and the day after tomorrow?", "weather", "the weather the day after tomorrow", r"weather.*tomorrow"), + C([WX], "sorry, I meant tomorrow", "search|weather", "tomorrow's weather", r"weather.*tomorrow"), + C([WX_KO], "그럼 내일은?", "weather", "내일 날씨", r"weather.*내일"), + C([WX_KO], "내일은?", "weather", "내일 날씨", r"weather.*내일"), + C([WX_KO], "주말은요?", "weather", "주말 날씨", r"weather.*주말"), C([WX], "what about Busan?", "weather", "the weather in Busan", r"Busan"), C([WX], "and Tokyo?", "weather", "the weather in Tokyo", r"Tokyo"), C([WX], "How about in London?", "weather", "the weather in London", r"London"), diff --git a/tests/followup_cases_more.py b/tests/followup_cases_more.py index c082c20..c8fbfee 100644 --- a/tests/followup_cases_more.py +++ b/tests/followup_cases_more.py @@ -323,7 +323,7 @@ CASES_MORE = [ C([WX], "what about Incheon?", "weather", "the weather in Incheon", r"Incheon"), C([WX], "and New York?", "weather", "the weather in New York", r"New York"), C([WX], "in Daejeon?", "weather", "the weather in Daejeon", r"Daejeon"), - C([WX], "and next week?", "search", "the weather next week", r"weather.*next week"), + C([WX], "and next week?", "weather", "the weather next week", r"weather.*next week"), C([WX], "tonight?", "search|weather", "the weather tonight"), C([WX], "what about this evening?", "search|weather", "the weather this evening"), C([WX], "should I take an umbrella?", "weather", "will it rain today"), @@ -332,20 +332,20 @@ CASES_MORE = [ C([WX], "great", "talk", "a reaction"), C([WX], "nice, thanks", "closing", "thanks for the answer"), C([WX], "what time is it?", "llm", "a topic shift: the time"), - C([WX, T("cool", "")], "and tomorrow?", "search", "a reaction between keeps the weather topic", r"tomorrow"), + C([WX, T("cool", "")], "and tomorrow?", "search|weather", "a reaction between keeps the weather topic", r"tomorrow"), C([WX, NAME_Q], "and tomorrow?", "llm", "small talk between: no weather to go on"), C([WX_KO], "그럼 부산은요?", "weather", "부산 날씨는요", r"Busan"), C([WX_KO], "대구는?", "weather", "대구 날씨는", r"Daegu"), C([WX_KO], "인천 날씨는?", "weather", "인천 날씨는", r"Incheon"), - C([WX_KO], "모레는?", "search", "모레 날씨는", r"weather.*모레"), - C([WX_KO], "다음 주는요?", "search", "다음 주 날씨는요", r"weather.*다음 ?주"), + C([WX_KO], "모레는?", "weather", "모레 날씨는", r"weather.*모레"), + C([WX_KO], "다음 주는요?", "weather", "다음 주 날씨는요", r"weather.*다음 ?주"), C([WX_KO], "오늘 밤은?", "search|weather", "오늘 밤 날씨는"), C([WX_KO], "우산 챙겨야 돼?", "weather", "비 와? 우산 필요해?"), C([WX_KO], "헐 덥겠다", "talk", "날씨에 대한 반응"), C([WX_KO], "와 좋다", "talk", "반응"), C([WX_KO], "and Busan?", "weather", "mixed: 부산 날씨는", r"Busan"), - C([WX_KO], "tomorrow는?", "search", "code-switch: 내일 날씨는", r"weather.*tomorrow"), - C([WX_KO, T("오 좋네", "")], "내일은요?", "search", "반응 뒤에도 날씨 얘기: 내일 날씨", r"weather.*내일"), + C([WX_KO], "tomorrow는?", "search|weather", "code-switch: 내일 날씨는", r"weather.*tomorrow"), + C([WX_KO, T("오 좋네", "")], "내일은요?", "weather", "반응 뒤에도 날씨 얘기: 내일 날씨", r"weather.*내일"), C([WX_KO, KO_AGE], "그럼 내일은?", "llm", "사이에 다른 주제: 날씨 아님"), C([TOKYO], "and Kyoto?", "weather", "the weather in Kyoto", r"Kyoto"), C([TOKYO], "what about tomorrow there?", "search|weather", "tomorrow's weather in Tokyo", r"tomorrow|Tokyo"), diff --git a/tests/test_web.py b/tests/test_web.py index 18ca43b..da3605c 100644 --- a/tests/test_web.py +++ b/tests/test_web.py @@ -104,6 +104,25 @@ outs = iter([["search NY weather\n", "It is 15 degrees. Clear."]]) assert list(brain.Brain().respond_from("Weather?", "x")) == ["It is 15 degrees.", "Clear."] outs = iter([["Search results say 330 m."]]) assert list(brain.Brain().respond_from("Eiffel?", "x")) == ["Search results say 330 m."] +# "Let me check." is said by lumi before the lookup: never again inside the answer +outs = iter([["Let me check. It is 15 degrees. 확인해 볼게요."]]) +assert list(brain.Brain().respond_from("Weather?", "x")) == ["It is 15 degrees."] + +# the days a weather question asks (today: Friday 23 October 2026), and only their lines of the forecast +fri = datetime.date(2026, 10, 23) +for q, days in {"오늘 날씨": [0], "and tomorrow?": [1], "모레는요?": [2], "내일모레": [2], "next Monday?": [3], + "다음 주 월요일 어때요?": [3], "this weekend": [1, 2], "이번 주말": [1, 2], "and Friday?": [0], + "next Friday": [7], "yesterday": [-1], "어제": [-1], "in 2 days": [2], "사흘 뒤": [3], "what about Busan?": None, + "the day after": [2]}.items(): + assert web.asked_day(q, [1], fri) == days, (q, web.asked_day(q, [1], fri)) +week = "Weather in Seoul now: clear sky, 15 degrees.\n" + "\n".join( + f"{(fri + datetime.timedelta(days=i)).strftime('%A %d %B')}: rain." for i in range(7)) +assert web.weather_on(week, [3], fri) == "Forecast for Seoul:\nMonday 26 October: rain." +assert web.weather_on(week, [0], fri).startswith("Weather in Seoul now") and web.weather_on(week, None, fri) == week +assert "too far ahead, the forecast reaches only to Thursday 29 October" in web.weather_on(week, [9], fri) +assert "already past" in web.weather_on(week, [-1], fri) +from tests import dialogues_weather # 30+ weather talks, EN / KO / mixed +dialogues_weather.check() # respond_stream: "search ..." under "ACTION: none" or without "ACTION:" is the lookup, never said; its words are not # the query outs = iter([["ACTION: none -\n", "search Lotte World", " ticket price"]]) @@ -187,8 +206,8 @@ def fake_say_stream(pieces, lang=None, abort=None): class FakeBrain: - weather_place = brain.Brain.weather_place - weather_at = "" + weather_place, weather_on = brain.Brain.weather_place, brain.Brain.weather_on + weather_at, weather_days, history = "", None, [] def respond_from(self, asked, facts): events.append(("llm", asked, time.time()))