feat: V8.5+ — Morphologisches Keyword-Matching (Snowball GermanStemmer + Levenshtein Fuzzy + Pre-Compute-Cache)
This commit is contained in:
+23
-14
@@ -31,6 +31,8 @@ from datetime import datetime, timedelta
|
||||
from pathlib import Path
|
||||
from typing import Optional, Dict, List
|
||||
|
||||
from .keyword_matcher import match_keyword, match_keyword_exact, find_matching_keywords
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Use absolute import instead of relative import
|
||||
@@ -87,16 +89,19 @@ class RoomEntry:
|
||||
def match_score(self, text: str) -> int:
|
||||
"""Return number of keyword matches in *text* (case-insensitive).
|
||||
|
||||
Uses morphological matching (stemming, fuzzy) for keywords_100
|
||||
and exact matching for keywords_context.
|
||||
|
||||
Args:
|
||||
text: The message text to match against.
|
||||
|
||||
Returns:
|
||||
Number of matching keywords (0 if none match).
|
||||
"""
|
||||
# Prüfe beide Listen: keywords_100 und keywords_context
|
||||
text_lower = text.lower()
|
||||
score_100 = sum(1 for kw in self.keywords_100 if kw.lower() in text_lower)
|
||||
score_context = sum(1 for kw in self.keywords_context if kw.lower() in text_lower)
|
||||
# 100%-Keywords: morphological matching (stemming, fuzzy)
|
||||
score_100 = sum(1 for kw in self.keywords_100 if match_keyword(kw, text))
|
||||
# Context-Keywords: exact matching only (case-insensitive)
|
||||
score_context = sum(1 for kw in self.keywords_context if match_keyword_exact(kw, text))
|
||||
return score_100 + score_context
|
||||
|
||||
|
||||
@@ -219,6 +224,9 @@ class RoomMap:
|
||||
def find_room_by_keywords(self, text: str, min_score: int = 1, origin_room_id: Optional[str] = None) -> Optional[RoomEntry]:
|
||||
"""Find the room with the most keyword matches for *text*.
|
||||
|
||||
Uses morphological matching (stemming, fuzzy) for 100%-keywords
|
||||
and exact matching for context keywords.
|
||||
|
||||
Args:
|
||||
text: The message text to classify.
|
||||
min_score: Minimum score required to return a room (default: 1).
|
||||
@@ -231,8 +239,6 @@ class RoomMap:
|
||||
if no room has any matching keywords or if the best score
|
||||
is below the minimum threshold.
|
||||
"""
|
||||
text_lower = text.lower()
|
||||
|
||||
# Stufe 1: Prüfe 100% Evidenz Keywords zuerst
|
||||
# Bei mehreren Räumen mit 100%-Matches: den mit den MEISTEN Treffern nehmen
|
||||
# Catch-All-Räume werden in Stufe 1 NICHT berücksichtigt
|
||||
@@ -244,8 +250,8 @@ class RoomMap:
|
||||
if room.catch_all:
|
||||
continue
|
||||
|
||||
# Zähle 100%-Keyword-Treffer
|
||||
score_100 = sum(1 for kw in room.keywords_100 if kw.lower() in text_lower)
|
||||
# Zähle 100%-Keyword-Treffer (morphologisches Matching)
|
||||
score_100 = sum(1 for kw in room.keywords_100 if match_keyword(kw, text))
|
||||
if score_100 > best_100_score:
|
||||
best_100_score = score_100
|
||||
best_100_room = room
|
||||
@@ -261,7 +267,8 @@ class RoomMap:
|
||||
origin_room = self._rooms[origin_room_id]
|
||||
# Prüfe kontextabhängige Keywords im Ursprungsraum
|
||||
# (auch Catch-All-Räume — sie können Kontext-Keywords haben)
|
||||
context_score = sum(1 for kw in origin_room.keywords_context if kw.lower() in text_lower)
|
||||
# Context-Keywords: exaktes Matching (kein Stemming/Fuzzy)
|
||||
context_score = sum(1 for kw in origin_room.keywords_context if match_keyword_exact(kw, text))
|
||||
if context_score >= min_score:
|
||||
return origin_room
|
||||
|
||||
@@ -273,8 +280,8 @@ class RoomMap:
|
||||
best_score: int = 0
|
||||
|
||||
for room in self._rooms.values():
|
||||
# Prüfe kontextabhängige Keywords
|
||||
context_score = sum(1 for kw in room.keywords_context if kw.lower() in text_lower)
|
||||
# Prüfe kontextabhängige Keywords (exaktes Matching)
|
||||
context_score = sum(1 for kw in room.keywords_context if match_keyword_exact(kw, text))
|
||||
if context_score > best_score:
|
||||
best_score = context_score
|
||||
best_room = room
|
||||
@@ -286,12 +293,14 @@ class RoomMap:
|
||||
def is_100_percent_match(self, text: str, room: RoomEntry) -> bool:
|
||||
"""Check if *text* contains any 100%-evidence keywords for *room*.
|
||||
|
||||
Returns True if at least one keywords_100 entry matches (case-insensitive).
|
||||
Uses morphological matching (stemming, fuzzy) for robust matching
|
||||
against plural forms, case declensions, and minor typos.
|
||||
|
||||
Returns True if at least one keywords_100 entry matches.
|
||||
Used by the router to distinguish 100% keyword matches (auto-route, no Butler)
|
||||
from context keyword matches (Butler asks, learning opportunity).
|
||||
"""
|
||||
text_lower = text.lower()
|
||||
return any(kw.lower() in text_lower for kw in room.keywords_100)
|
||||
return any(match_keyword(kw, text) for kw in room.keywords_100)
|
||||
|
||||
def calculate_weighted_score(self, text: str, room: RoomEntry) -> float:
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user