P-033: Fix Tiebreak-Bug in find_room_by_keywords
Root Cause: room_map.py:314 verwendete strict '>' statt Tiebreak-Comparator. Bei Score-Tie (z.B. 1:1) entschied Dict-Insertion-Order, nicht Logik. Fix (Option B, Rainer 2026-08-06): - Neue Tiebreak-Helper: _get_matched_keywords(), _keyword_room_count(), _tiebreak_compare() - Bei Score-Tie: Raum mit seltenerem Keyword (geringster room_count) gewinnt - Bei erneutem Tie: laengstes gematchtes Keyword gewinnt - Bei erneutem Tie: first-seen (stabil) - Tiebreak gilt fuer Stufe 1 (100%-Keywords) UND Stufe 3 (Kontext-Keywords) Tests: 6 neue Regressionstests in TestP033Tiebreak - 'Rocketchat' + 'Dokumentation' -> #Rocketchat-Plugin (nicht #LAN-Doku) - Nur 'Dokumentation' -> #LAN-Dokumentation (100%-Keyword schlaegt Kontext) - Nur 'Rocketchat' -> #Rocketchat-Plugin - Raritaet schlaegt Laenge - Stable-Sort bei identischen Keywords - Laenge gewinnt bei gleicher Raritaet Infra: plugin/conftest.py registriert 'plugin' in sys.modules fuer standalone-Tests (P-022 Flat-Modul-Pattern)
This commit is contained in:
+114
-9
@@ -278,6 +278,89 @@ class RoomMap:
|
||||
"""
|
||||
self._fact_store_created_at = timestamp
|
||||
|
||||
# ── Tiebreak Helpers ──────────────────────────────────────────────
|
||||
|
||||
def _get_matched_keywords(self, room: RoomEntry, text: str, keyword_type: str) -> list[str]:
|
||||
"""Return the list of keywords that matched *text* for *room*.
|
||||
|
||||
Args:
|
||||
room: The room to check.
|
||||
text: The message text.
|
||||
keyword_type: ``"100"`` for keywords_100 (morphological matching)
|
||||
or ``"context"`` for keywords_context (exact matching).
|
||||
|
||||
Returns:
|
||||
List of matching keyword strings.
|
||||
"""
|
||||
if keyword_type == "100":
|
||||
return [kw for kw in room.keywords_100 if match_keyword(kw, text)]
|
||||
else:
|
||||
return [kw for kw in room.keywords_context if match_keyword_exact(kw, text)]
|
||||
|
||||
def _keyword_room_count(self, keyword: str) -> int:
|
||||
"""Count how many rooms (excluding catch-all) contain *keyword*.
|
||||
|
||||
Searches both ``keywords_100`` and ``keywords_context`` across all
|
||||
non-catch-all rooms. Lower count = rarer keyword = better tiebreak.
|
||||
|
||||
Args:
|
||||
keyword: The keyword to count (case-sensitive, as stored).
|
||||
|
||||
Returns:
|
||||
Number of rooms that have this keyword.
|
||||
"""
|
||||
count = 0
|
||||
for room in self._rooms.values():
|
||||
if room.catch_all:
|
||||
continue
|
||||
if keyword in room.keywords_100 or keyword in room.keywords_context:
|
||||
count += 1
|
||||
return count
|
||||
|
||||
def _tiebreak_compare(
|
||||
self,
|
||||
room_a: RoomEntry,
|
||||
room_b: RoomEntry,
|
||||
matched_a: list[str],
|
||||
matched_b: list[str],
|
||||
) -> bool:
|
||||
"""Return True if *room_a* beats *room_b* in a score tie.
|
||||
|
||||
Tiebreak rules (Option B, Rainer 2026-08-06):
|
||||
1. Room whose rarest matched keyword has the lowest *room_count* wins.
|
||||
2. If still tied: room whose longest matched keyword is longer wins.
|
||||
3. If still tied: *room_b* stays (stable sort — first seen wins).
|
||||
|
||||
Args:
|
||||
room_a: Challenger room.
|
||||
room_b: Current best room.
|
||||
matched_a: Keywords that matched for room_a.
|
||||
matched_b: Keywords that matched for room_b.
|
||||
|
||||
Returns:
|
||||
True if room_a should replace room_b as the best match.
|
||||
"""
|
||||
# Rule 1: Rarest keyword (lowest room_count) wins
|
||||
rarity_a = min(
|
||||
(self._keyword_room_count(kw) for kw in matched_a),
|
||||
default=999,
|
||||
)
|
||||
rarity_b = min(
|
||||
(self._keyword_room_count(kw) for kw in matched_b),
|
||||
default=999,
|
||||
)
|
||||
if rarity_a != rarity_b:
|
||||
return rarity_a < rarity_b
|
||||
|
||||
# Rule 2: Longest matched keyword wins
|
||||
longest_a = max((len(kw) for kw in matched_a), default=0)
|
||||
longest_b = max((len(kw) for kw in matched_b), default=0)
|
||||
if longest_a != longest_b:
|
||||
return longest_a > longest_b
|
||||
|
||||
# Rule 3: Stable — keep current best
|
||||
return False
|
||||
|
||||
# ── Lookup Methods ────────────────────────────────────────────────
|
||||
|
||||
def find_room_by_keywords(self, text: str, min_score: int = 1, origin_room_id: Optional[str] = None) -> Optional[RoomEntry]:
|
||||
@@ -286,6 +369,10 @@ class RoomMap:
|
||||
Uses morphological matching (stemming, fuzzy) for 100%-keywords
|
||||
and exact matching for context keywords.
|
||||
|
||||
At equal scores, tiebreaking favours the room whose matched keyword
|
||||
is the rarest across the entire room map (lowest room_count), then
|
||||
the longest matched keyword (Option B, P-033 fix).
|
||||
|
||||
Args:
|
||||
text: The message text to classify.
|
||||
min_score: Minimum score required to return a room (default: 1).
|
||||
@@ -299,26 +386,35 @@ class RoomMap:
|
||||
is below the minimum threshold.
|
||||
"""
|
||||
# Stufe 1: Prüfe 100% Evidenz Keywords zuerst
|
||||
# Bei mehreren Räumen mit 100%-Matches: den mit den MEISTEN Treffern nehmen
|
||||
# Bei mehreren Räumen mit 100%-Matches: den mit den MEISTEN Treffern nehmen.
|
||||
# Bei Score-Tie: Tiebreak per Keyword-Rarität (P-033).
|
||||
# Catch-All-Räume werden in Stufe 1 NICHT berücksichtigt
|
||||
# (100%-Keywords auf Catch-All sind für Auto-Route gedacht, nicht für Keyword-Match)
|
||||
best_100_room: Optional[RoomEntry] = None
|
||||
best_100_score: int = 0
|
||||
|
||||
best_100_matched: list[str] = []
|
||||
|
||||
for room in self._rooms.values():
|
||||
if room.catch_all:
|
||||
continue
|
||||
|
||||
# Zähle 100%-Keyword-Treffer (morphologisches Matching)
|
||||
score_100 = sum(1 for kw in room.keywords_100 if match_keyword(kw, text))
|
||||
|
||||
matched = self._get_matched_keywords(room, text, "100")
|
||||
score_100 = len(matched)
|
||||
|
||||
if score_100 > best_100_score:
|
||||
best_100_score = score_100
|
||||
best_100_room = room
|
||||
|
||||
best_100_matched = matched
|
||||
elif score_100 == best_100_score and score_100 > 0:
|
||||
# P-033: Tiebreak per Keyword-Rarität
|
||||
if self._tiebreak_compare(room, best_100_room, matched, best_100_matched):
|
||||
best_100_room = room
|
||||
best_100_matched = matched
|
||||
|
||||
# Wenn mindestens ein 100%-Match existiert → sofort diesen Raum zurückgeben
|
||||
if best_100_room is not None and best_100_score > 0:
|
||||
return best_100_room
|
||||
|
||||
|
||||
# Stufe 2: Origin-Priorität mit kontextabhängigen Keywords
|
||||
# Origin-Priorität gilt NUR für context-Keywords (100% schlagen immer durch)
|
||||
# V8.4.6: Catch-All-Räume sind in Stufe 2 als Butler-Ziele erlaubt
|
||||
@@ -335,15 +431,24 @@ class RoomMap:
|
||||
# V8.4.6: Catch-All-Räume sind in Stufe 3 als Butler-Ziele erlaubt.
|
||||
# Catch-All als Keyword-Match-Ziel war für 100%-Auto-Route blockiert (Stufe 1).
|
||||
# Für Kontext-Keywords (Stufe 3) sind Catch-All-Räume valide Butler-Ziele.
|
||||
# P-033: Tiebreak per Keyword-Rarität bei Score-Tie.
|
||||
best_room: Optional[RoomEntry] = None
|
||||
best_score: int = 0
|
||||
best_matched: list[str] = []
|
||||
|
||||
for room in self._rooms.values():
|
||||
# Prüfe kontextabhängige Keywords (exaktes Matching)
|
||||
context_score = sum(1 for kw in room.keywords_context if match_keyword_exact(kw, text))
|
||||
matched = self._get_matched_keywords(room, text, "context")
|
||||
context_score = len(matched)
|
||||
|
||||
if context_score > best_score:
|
||||
best_score = context_score
|
||||
best_room = room
|
||||
best_matched = matched
|
||||
elif context_score == best_score and context_score > 0:
|
||||
# P-033: Tiebreak per Keyword-Rarität
|
||||
if self._tiebreak_compare(room, best_room, matched, best_matched):
|
||||
best_room = room
|
||||
best_matched = matched
|
||||
|
||||
if best_room is not None and best_score >= min_score:
|
||||
return best_room
|
||||
|
||||
Reference in New Issue
Block a user