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:
@@ -2,8 +2,11 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import math
|
||||
import re
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
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]:
|
||||
try:
|
||||
import yake
|
||||
except Exception:
|
||||
except ImportError:
|
||||
logger.warning("yake not installed, skipping relevance scoring")
|
||||
return {}
|
||||
|
||||
text_token_count = len(text.split())
|
||||
@@ -208,7 +212,8 @@ class KeywordExtractor:
|
||||
try:
|
||||
extractor = yake.KeywordExtractor(lan="en", n=2, dedupLim=0.9, top=top_n)
|
||||
phrase_scores = extractor.extract_keywords(text)
|
||||
except Exception:
|
||||
except (ValueError, TypeError) as e:
|
||||
logger.warning("YAKE extraction failed: %s", e)
|
||||
return {}
|
||||
|
||||
token_scores: Dict[str, float] = {}
|
||||
|
||||
Reference in New Issue
Block a user