# Weights are module-level constants — easy to tune without touching logic. _W_NEGATIVE_SENTIMENT = 30 # scaled by sentiment confidence _W_URGENCY_HIGH = 25 _W_URGENCY_MEDIUM = 10 _W_COMPLAINT_INTENT = 15 _W_ANGER_OR_FEAR = 20 # scaled by emotion score _LEVEL_THRESHOLDS = [ (75, "CRITICAL"), (50, "HIGH"), (25, "MEDIUM"), (0, "LOW"), ] def compute_priority(sentiment: dict, signals: dict, emotion: dict) -> dict: factors: dict[str, int] = {} raw = 0 # Negative sentiment — scaled by how confident the model is if sentiment["label"] == "NEGATIVE": pts = round(_W_NEGATIVE_SENTIMENT * sentiment["confidence"]) factors["negative_sentiment"] = pts raw += pts # Urgency urgency = signals["urgency"] if urgency == "HIGH": factors["high_urgency"] = _W_URGENCY_HIGH raw += _W_URGENCY_HIGH elif urgency == "MEDIUM": factors["medium_urgency"] = _W_URGENCY_MEDIUM raw += _W_URGENCY_MEDIUM # Complaint intent if signals["intent"] == "COMPLAINT": factors["complaint_intent"] = _W_COMPLAINT_INTENT raw += _W_COMPLAINT_INTENT # High-arousal negative emotions — scaled by emotion score emo = emotion["emotion"] if emo in {"anger", "fear"}: pts = round(_W_ANGER_OR_FEAR * emotion["score"]) factors[emo] = pts raw += pts priority_score = max(0, min(100, raw)) priority_level = "LOW" for threshold, level in _LEVEL_THRESHOLDS: if priority_score >= threshold: priority_level = level break return { "priority_score": priority_score, "priority_level": priority_level, "factors": factors, }