importance.py 901 B

12345678910111213141516171819202122232425262728
  1. from __future__ import annotations
  2. from typing import Any, Dict
  3. def compute_importance(cluster: Dict[str, Any]) -> float:
  4. """Compute an importance score for an already-enriched cluster.
  5. Preference: use LLM-derived signals when available.
  6. Heuristic blend:
  7. - consensus/coverage: sources + number of articles
  8. - signal strength: |sentimentScore| (LLM-derived)
  9. """
  10. sources = len(set(cluster.get("sources", [])))
  11. article_count = len(cluster.get("articles", []))
  12. sentiment_score = cluster.get("sentimentScore")
  13. if sentiment_score is None:
  14. sentiment_score = 0.0
  15. # Coverage term (kept conservative)
  16. coverage = 0.10 * sources + 0.01 * article_count
  17. # LLM signal term: higher magnitude sentiment => higher importance.
  18. signal = 0.60 * min(1.0, abs(float(sentiment_score)))
  19. score = coverage + signal
  20. return min(0.99, round(score, 2))