"""记忆重要度打分:决定一条记忆在检索中的基础权重。

v0 用确定性启发式(关键词 + 长度);LLM 版打分在 L2 接入 DeepSeek 后替换
(同一个 Protocol,注入即可换,见 PRD §12 TODO)。
"""

from __future__ import annotations

from typing import Protocol


class ImportanceScorer(Protocol):
    def score(self, content: str) -> float:  # 返回 [0,1]
        ...


class HeuristicImportanceScorer:
    """无 LLM 的确定性打分:含人生里程碑/强情绪关键词或较长的事件更重要。"""

    # 用词干以匹配各种词形(marri→married/marriage,promot→promoted 等)
    _SALIENT = (
        "死", "出生", "结婚", "离婚", "吵架", "爱", "恨", "升职", "失业", "搬家", "背叛",
        "die", "dead", "death", "born", "birth", "love", "hate", "fight", "marri",
        "divorc", "promot", "fired", "betray",
    )

    def score(self, content: str) -> float:
        text = content.strip()
        if not text:
            return 0.0
        base = 0.2
        lowered = text.lower()
        if any(k in lowered for k in self._SALIENT):
            base += 0.5
        base += min(0.3, len(text) / 200.0)  # 长事件略重
        return max(0.0, min(1.0, base))
