def pick(*values, default="unknown"): for value in values: if value is None: continue text = str(value).strip() if text == "": continue if text.lower() in [ "unknown", "null", "none", "--", ]: continue return text return default def normalize_analysis(data): analyst = data.get("analyst", {}) psychology = data.get("psychology", {}) strategy = data.get("strategy", {}) return { "interest": pick( analyst.get("interest_level"), psychology.get("emotional_availability"), default="mixed" ), "tone": pick( analyst.get("emotional_tone"), default="neutral" ), "risk": pick( psychology.get("risk_level"), analyst.get("emotional_risk"), default="moderate" ), "trend": pick( analyst.get("trend"), default="forming" ), "confidence": pick( analyst.get("confidence"), psychology.get("confidence"), default="medium" ), "summary": pick( strategy.get("personalized_advice"), strategy.get("truth"), analyst.get("summary"), default="Emotional pattern still developing." ), "hidden_meaning": pick( analyst.get("hidden_meaning"), default="Mixed emotional signals." ), "advice": pick( strategy.get("action_advice"), default="Communicate clearly and observe consistency over time." ), "warning": pick( strategy.get("warning"), default="Emotional inconsistency may create confusion." ), "attachment": pick( psychology.get("attachment_style"), default="unclear" ), "communication_health": pick( analyst.get("communication_health"), default="mixed" ), "love_score": pick( psychology.get("compatibility_score"), generate_love_score(analyst, psychology), default=50 ), "leverage": pick( strategy.get("leverage_assessment"), default="Unclear balance of power." ), "trajectory": pick( strategy.get("trajectory"), default="Pattern still forming." ), } def generate_love_score( analyst, psychology ): score = 50 interest = str( analyst.get("interest_level", "") ).lower() tone = str( analyst.get("emotional_tone", "") ).lower() risk = str( analyst.get("emotional_risk", "") ).lower() if "high" in interest: score += 20 elif "medium" in interest: score += 5 elif "low" in interest: score -= 15 if "warm" in tone: score += 10 if "avoidant" in tone: score -= 10 if "high" in risk: score -= 20 elif "moderate" in risk: score -= 5 score = max(5, min(score, 95)) return score