Narrow bare except handlers in keyword_extractor.py

Replace broad `except Exception` with `except ImportError` for the yake
import and `except (ValueError, TypeError)` for YAKE extraction. Add
logging so failures are no longer silently swallowed.

Fixes #4
This commit is contained in:
2026-03-19 19:32:06 +01:00
parent 9b6fd1d95a
commit 3cfb18be11

View File

@@ -2,8 +2,11 @@
from __future__ import annotations from __future__ import annotations
import logging
import math import math
import re import re
logger = logging.getLogger(__name__)
from dataclasses import dataclass, field from dataclasses import dataclass, field
from typing import Any, Dict, Iterable, List, Mapping, Optional, Sequence, Set, Tuple from typing import Any, Dict, Iterable, List, Mapping, Optional, Sequence, Set, Tuple
@@ -199,7 +202,8 @@ class KeywordExtractor:
def _extract_yake_scores(self, text: str) -> Dict[str, float]: def _extract_yake_scores(self, text: str) -> Dict[str, float]:
try: try:
import yake import yake
except Exception: except ImportError:
logger.warning("yake not installed, skipping relevance scoring")
return {} return {}
text_token_count = len(text.split()) text_token_count = len(text.split())
@@ -208,7 +212,8 @@ class KeywordExtractor:
try: try:
extractor = yake.KeywordExtractor(lan="en", n=2, dedupLim=0.9, top=top_n) extractor = yake.KeywordExtractor(lan="en", n=2, dedupLim=0.9, top=top_n)
phrase_scores = extractor.extract_keywords(text) phrase_scores = extractor.extract_keywords(text)
except Exception: except (ValueError, TypeError) as e:
logger.warning("YAKE extraction failed: %s", e)
return {} return {}
token_scores: Dict[str, float] = {} token_scores: Dict[str, float] = {}