diff --git a/plugin/archivar/keyword_matcher.py b/plugin/archivar/keyword_matcher.py index e5f8200..e356bac 100644 --- a/plugin/archivar/keyword_matcher.py +++ b/plugin/archivar/keyword_matcher.py @@ -298,21 +298,40 @@ def match_keyword(keyword: str, text: str) -> bool: return True # Strategy 3: Fuzzy match (Levenshtein) - # Only for words > MIN_FUZZY_LEN-1 characters to avoid false positives - # Check each word of the keyword against each word of the text + # For multi-word keywords: ALL keyword words must find a fuzzy match + # in the text. A single matching word is not enough — otherwise + # 'hermes-gateway' would falsely match 'hermes deployment pipeline' + # just because 'hermes' == 'hermes'. + # For single-word keywords: that one word must find a fuzzy match. + # Words shorter than MIN_FUZZY_LEN are skipped here (they were already + # checked by Strategy 1/2); if we reach Strategy 3 and a keyword word + # is too short for fuzzy, the keyword as a whole does NOT match. kw_words = kw_norm.split() text_words = text_no_hyphen.split() + all_fuzzy_matched = True for kw_word in kw_words: if len(kw_word) < MIN_FUZZY_LEN: - continue + # Short words can't be reliably fuzzy-matched. + # If we're here, Strategy 1/2 didn't match them either. + # For multi-word keywords, this means the keyword doesn't fully match. + all_fuzzy_matched = False + break + found = False for text_word in text_words: if len(text_word) < MIN_FUZZY_LEN: continue # Scale: distance 1 for 5-7 chars, distance 2 for 8+ chars max_dist = FUZZY_MAX_DIST_SHORT if len(kw_word) <= 7 else FUZZY_MAX_DIST_LONG if levenshtein_distance(kw_word, text_word) <= max_dist: - return True + found = True + break + if not found: + all_fuzzy_matched = False + break + + if all_fuzzy_matched and len(kw_words) > 0: + return True # Strategy 3b: Phrase-level fuzzy match (concatenated, no spaces) # Catches 'raum-map' vs 'Raummap' where individual words are too short @@ -398,18 +417,39 @@ def match_keyword_with_stems( return True # Strategy 3: Fuzzy match (Levenshtein) + # For multi-word keywords: ALL keyword words must find a fuzzy match + # in the text. A single matching word is not enough — otherwise + # 'hermes-gateway' would falsely match 'hermes deployment pipeline' + # just because 'hermes' == 'hermes'. + # For single-word keywords: that one word must find a fuzzy match. + # Words shorter than MIN_FUZZY_LEN are skipped here (they were already + # checked by Strategy 1/2); if we reach Strategy 3 and a keyword word + # is too short for fuzzy, the keyword as a whole does NOT match. kw_words = kw_norm.split() text_words = text_no_hyphen.split() + all_fuzzy_matched = True for kw_word in kw_words: if len(kw_word) < MIN_FUZZY_LEN: - continue + # Short words can't be reliably fuzzy-matched. + # If we're here, Strategy 1/2 didn't match them either. + # For multi-word keywords, this means the keyword doesn't fully match. + all_fuzzy_matched = False + break + found = False for text_word in text_words: if len(text_word) < MIN_FUZZY_LEN: continue max_dist = FUZZY_MAX_DIST_SHORT if len(kw_word) <= 7 else FUZZY_MAX_DIST_LONG if levenshtein_distance(kw_word, text_word) <= max_dist: - return True + found = True + break + if not found: + all_fuzzy_matched = False + break + + if all_fuzzy_matched and len(kw_words) > 0: + return True # Strategy 3b: Phrase-level fuzzy match kw_cat = kw_norm.replace(' ', '') diff --git a/plugin/archivar/tests/test_keyword_matcher.py b/plugin/archivar/tests/test_keyword_matcher.py index 373f6bd..86d3f2d 100644 --- a/plugin/archivar/tests/test_keyword_matcher.py +++ b/plugin/archivar/tests/test_keyword_matcher.py @@ -260,6 +260,38 @@ class TestMatchKeyword: """'ddp' vs 'DDP-Client' → MATCH""" assert match_keyword("ddp", "DDP-Client") is True + # ── Strategy 3: Multi-word fuzzy matching (B-1 fix) ─────────────── + + def test_strategy3_multiword_all_words_match(self): + """Multi-word keyword: ALL words must fuzzy-match for Strategy 3.""" + # 'hermes gateway' (normalized from 'hermes-gateway') vs + # 'hermes gateway' → both words match → MATCH + assert match_keyword("hermes-gateway", "hermes gateway") is True + + def test_strategy3_multiword_only_one_word_matches(self): + """Multi-word keyword: single word match is NOT enough for Strategy 3.""" + # 'hermes gateway' (normalized from 'hermes-gateway') vs + # 'hermes deployment pipeline' → only 'hermes' matches, 'gateway' doesn't + # → NO MATCH (B-1 fix) + assert match_keyword("hermes-gateway", "hermes deployment pipeline") is False + + def test_strategy3_multiword_no_words_match(self): + """Multi-word keyword: no words match → NO MATCH.""" + assert match_keyword("hermes-gateway", "unrelated text here") is False + + def test_strategy3_single_word_still_works(self): + """Single-word keywords still work with Strategy 3 (regression check).""" + # 'hermes' (single word) vs 'hermes' → MATCH + assert match_keyword("hermes", "hermes") is True + # 'hermes' vs 'hermes deployment' → MATCH (exact substring, Strategy 1) + assert match_keyword("hermes", "hermes deployment") is True + + def test_strategy3_multiword_fuzzy_typo(self): + """Multi-word keyword with minor typo in one word still matches.""" + # 'hermes gateway' vs 'hermes gatewy' (typo in 'gateway') + # 'hermes' exact match (Strategy 1), 'gatewy' fuzzy-matches 'gateway' (dist=1) + assert match_keyword("hermes-gateway", "hermes gatewy") is True + # ═══════════════════════════════════════════════════════════════════════════ # match_keyword_exact() Tests