booster: replace substring matching with hyphen-insensitive keyword_matcher
Replace naive 'kw in text_lower' substring matching (lines 104-106)
with match_keyword() from keyword_matcher, which provides:
- Strategy 1: Exact substring match (case-insensitive, normalized)
- Strategy 1b: Hyphen-insensitive match ('presence-engine' matches
'Presence Engine' in text)
- Strategy 2: Stemming match (snowballstemmer German)
- Strategy 3: Fuzzy match (Levenshtein)
Changes:
- Import match_keyword from .keyword_matcher
- matched_keywords now uses match_keyword(kw, text) instead of kw in text_lower
- _count_unique_matches: same replacement
- _record_matches: parameter renamed text_lower -> text (no logic change)
- Removed now-unused text_lower = text.lower() line
All 35 booster tests pass.
This commit is contained in:
+11
-10
@@ -27,6 +27,8 @@ import logging
|
|||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from typing import Dict, List, Set, Tuple
|
from typing import Dict, List, Set, Tuple
|
||||||
|
|
||||||
|
from .keyword_matcher import match_keyword
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@@ -100,9 +102,8 @@ class KeywordBooster:
|
|||||||
score_dict: Dict[str, float] = {room_id: score for room_id, score in scores}
|
score_dict: Dict[str, float] = {room_id: score for room_id, score in scores}
|
||||||
|
|
||||||
# ── Keywords im Text finden ───────────────────────────────
|
# ── Keywords im Text finden ───────────────────────────────
|
||||||
text_lower = text.lower()
|
|
||||||
matched_keywords: List[str] = [
|
matched_keywords: List[str] = [
|
||||||
kw for kw in self._keyword_to_rooms if kw in text_lower
|
kw for kw in self._keyword_to_rooms if match_keyword(kw, text)
|
||||||
]
|
]
|
||||||
|
|
||||||
if not matched_keywords:
|
if not matched_keywords:
|
||||||
@@ -110,7 +111,7 @@ class KeywordBooster:
|
|||||||
return sorted(scores, key=lambda x: x[1], reverse=True)
|
return sorted(scores, key=lambda x: x[1], reverse=True)
|
||||||
|
|
||||||
# ── V8.2 A3: Matches für Demotion-Tracking aufzeichnen ──────
|
# ── V8.2 A3: Matches für Demotion-Tracking aufzeichnen ──────
|
||||||
self._record_matches(text_lower, matched_keywords)
|
self._record_matches(text, matched_keywords)
|
||||||
|
|
||||||
# ── Boost-Phase: +0.10 für eindeutige Keywords ────────────
|
# ── Boost-Phase: +0.10 für eindeutige Keywords ────────────
|
||||||
for kw in matched_keywords:
|
for kw in matched_keywords:
|
||||||
@@ -158,8 +159,8 @@ class KeywordBooster:
|
|||||||
|
|
||||||
if diff < 0.05:
|
if diff < 0.05:
|
||||||
# Zähle eindeutige Keyword-Treffer für beide Räume
|
# Zähle eindeutige Keyword-Treffer für beide Räume
|
||||||
unique_count_1 = self._count_unique_matches(text_lower, top1_room)
|
unique_count_1 = self._count_unique_matches(text, top1_room)
|
||||||
unique_count_2 = self._count_unique_matches(text_lower, top2_room)
|
unique_count_2 = self._count_unique_matches(text, top2_room)
|
||||||
|
|
||||||
if unique_count_1 > unique_count_2:
|
if unique_count_1 > unique_count_2:
|
||||||
score_dict[top1_room] += 0.05
|
score_dict[top1_room] += 0.05
|
||||||
@@ -184,11 +185,11 @@ class KeywordBooster:
|
|||||||
|
|
||||||
# ── Helpers ────────────────────────────────────────────────────────
|
# ── Helpers ────────────────────────────────────────────────────────
|
||||||
|
|
||||||
def _count_unique_matches(self, text_lower: str, room_id: str) -> int:
|
def _count_unique_matches(self, text: str, room_id: str) -> int:
|
||||||
"""Zählt wie viele eindeutige Keywords (nur in diesem Raum) im Text vorkommen.
|
"""Zählt wie viele eindeutige Keywords (nur in diesem Raum) im Text vorkommen.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
text_lower: Der Text in lowercase.
|
text: Der Nachrichtentext (RAW, Normalisierung passiert intern).
|
||||||
room_id: Die zu prüfende Raum-ID.
|
room_id: Die zu prüfende Raum-ID.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
@@ -196,13 +197,13 @@ class KeywordBooster:
|
|||||||
"""
|
"""
|
||||||
count = 0
|
count = 0
|
||||||
for kw, rooms in self._keyword_to_rooms.items():
|
for kw, rooms in self._keyword_to_rooms.items():
|
||||||
if len(rooms) == 1 and room_id in rooms and kw in text_lower:
|
if len(rooms) == 1 and room_id in rooms and match_keyword(kw, text):
|
||||||
count += 1
|
count += 1
|
||||||
return count
|
return count
|
||||||
|
|
||||||
# ── V8.2 A3: 90-Tage-Demotion ─────────────────────────────────────
|
# ── V8.2 A3: 90-Tage-Demotion ─────────────────────────────────────
|
||||||
|
|
||||||
def _record_matches(self, text_lower: str, matched_keywords: List[str]) -> None:
|
def _record_matches(self, text: str, matched_keywords: List[str]) -> None:
|
||||||
"""Zeichnet last_matched-Timestamps für alle getroffenen Keywords.
|
"""Zeichnet last_matched-Timestamps für alle getroffenen Keywords.
|
||||||
|
|
||||||
Wird nach jedem boost()-Aufruf aufgerufen, um die RoomMap-Tracking-Felder
|
Wird nach jedem boost()-Aufruf aufgerufen, um die RoomMap-Tracking-Felder
|
||||||
@@ -210,7 +211,7 @@ class KeywordBooster:
|
|||||||
(Demotion betrifft nur 100%-Keywords).
|
(Demotion betrifft nur 100%-Keywords).
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
text_lower: Der Text in lowercase.
|
text: Der Nachrichtentext (RAW, Normalisierung passiert intern).
|
||||||
matched_keywords: Liste der im Text gefundenen Keywords.
|
matched_keywords: Liste der im Text gefundenen Keywords.
|
||||||
"""
|
"""
|
||||||
now = datetime.now().isoformat()
|
now = datetime.now().isoformat()
|
||||||
|
|||||||
Reference in New Issue
Block a user