|
| 1 | +""" |
| 2 | +Burnout signal analysis aligned to Maslach Burnout Inventory dimensions. |
| 3 | +
|
| 4 | +This is a signal layer over conversational text. It does not diagnose burnout; |
| 5 | +it produces conservative, evidence-anchored features for wellness routing. |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import re |
| 11 | +from dataclasses import dataclass, field |
| 12 | + |
| 13 | + |
| 14 | +@dataclass |
| 15 | +class BurnoutAnalysis: |
| 16 | + ee_hits: int = 0 |
| 17 | + dp_hits: int = 0 |
| 18 | + pa_hits: int = 0 |
| 19 | + ee_tags: list[str] = field(default_factory=list) |
| 20 | + dp_tags: list[str] = field(default_factory=list) |
| 21 | + pa_tags: list[str] = field(default_factory=list) |
| 22 | + ee_score: float = 0.0 |
| 23 | + dp_score: float = 0.0 |
| 24 | + pa_score: float = 0.0 |
| 25 | + composite_score: float = 0.0 |
| 26 | + risk_band: str = "low" |
| 27 | + recommended_action: str = "continue_monitoring" |
| 28 | + high_acuity_flag: bool = False |
| 29 | + |
| 30 | + |
| 31 | +_EE_PATTERNS: list[tuple[str, str]] = [ |
| 32 | + (r"\bexhaust(ed|ing|ion)\b", "exhaustion_explicit"), |
| 33 | + (r"\bdrained\b", "drained"), |
| 34 | + (r"\bburn(ed|t)?[\s-]?out\b", "burnt_out_explicit"), |
| 35 | + (r"\b(can'?t|cannot) (cope|keep up|do this anymore)\b", "cant_cope"), |
| 36 | + (r"\boverwhelm(ed|ing)\b", "overwhelmed"), |
| 37 | + (r"\b(too much|too many) (work|things|deadlines|meetings)\b", "workload_excess"), |
| 38 | + (r"\bworking (late|weekends|all the time|nights)\b", "extended_hours"), |
| 39 | + (r"\bno (time|energy|break)\b", "no_recovery"), |
| 40 | + (r"\bnot sleeping\b", "sleep_disruption"), |
| 41 | + (r"\b(can'?t|cannot) (sleep|relax|switch off|disconnect)\b", "cant_disconnect"), |
| 42 | + (r"\b(constantly|always) (tired|fatigue)\b", "chronic_fatigue"), |
| 43 | + (r"\bno energy\b", "low_energy"), |
| 44 | + (r"\bemotionally (drained|spent|empty)\b", "emotional_depletion"), |
| 45 | +] |
| 46 | + |
| 47 | +_DP_PATTERNS: list[tuple[str, str]] = [ |
| 48 | + (r"\b(don'?t|do not) care anymore\b", "apathy_explicit"), |
| 49 | + (r"\bjust going through the motions\b", "going_through_motions"), |
| 50 | + (r"\bcheck(ing|ed) out\b", "checked_out"), |
| 51 | + (r"\bdisengag(ed|ing)\b", "disengaged"), |
| 52 | + (r"\bcynical\b", "cynicism_explicit"), |
| 53 | + (r"\bpointless\b", "pointless"), |
| 54 | + (r"\b(hate|dread) (work|my job|going in|mondays)\b", "work_aversion"), |
| 55 | + (r"\bwhat'?s the point\b", "questioning_purpose"), |
| 56 | + (r"\b(not|no longer) (engaged|invested|excited)\b", "loss_of_engagement"), |
| 57 | + (r"\b(distant|detached) from (team|colleagues|clients|patients)\b", "interpersonal_detachment"), |
| 58 | + (r"\b(robotic|mechanical|automatic) at work\b", "mechanical_work"), |
| 59 | +] |
| 60 | + |
| 61 | +_PA_PATTERNS: list[tuple[str, str]] = [ |
| 62 | + (r"\b(not|no longer) (effective|making (a )?difference|productive)\b", "ineffective"), |
| 63 | + (r"\bnothing (i do )?matters\b", "futility"), |
| 64 | + (r"\b(failing|failure) at (work|my job)\b", "perceived_failure"), |
| 65 | + (r"\b(can'?t|cannot) (focus|concentrate|finish|get anything done)\b", "performance_decline"), |
| 66 | + (r"\b(useless|worthless) at work\b", "self_devaluation"), |
| 67 | + (r"\bnot good enough\b", "self_doubt"), |
| 68 | + (r"\b(impostor|imposter)\b", "impostor"), |
| 69 | + (r"\bmissing deadlines\b", "missed_deadlines"), |
| 70 | + (r"\b(my )?work quality (has )?(dropped|slipped|declined)\b", "quality_decline"), |
| 71 | + (r"\b(losing|lost) (confidence|motivation)\b", "motivation_loss"), |
| 72 | +] |
| 73 | + |
| 74 | +_HIGH_ACUITY_PATTERNS: list[tuple[str, str]] = [ |
| 75 | + (r"\bsuicid(al|e)\b", "suicidal_ideation"), |
| 76 | + (r"\bself[- ]harm\b", "self_harm"), |
| 77 | + (r"\b(want|wish) to (die|disappear|not (exist|wake up))\b", "wish_to_die"), |
| 78 | + (r"\bend(ing)? it all\b", "end_it_all"), |
| 79 | + (r"\bno (way|reason) to (live|go on)\b", "hopelessness_acute"), |
| 80 | + (r"\b(can'?t|cannot) take (it|this) anymore\b", "breaking_point"), |
| 81 | + (r"\bhaving a breakdown\b", "breakdown"), |
| 82 | +] |
| 83 | + |
| 84 | + |
| 85 | +def _scan(text: str, patterns: list[tuple[str, str]]) -> tuple[int, list[str]]: |
| 86 | + hits = 0 |
| 87 | + tags: list[str] = [] |
| 88 | + seen: set[str] = set() |
| 89 | + for pattern, tag in patterns: |
| 90 | + if re.search(pattern, text, flags=re.I): |
| 91 | + hits += 1 |
| 92 | + if tag not in seen: |
| 93 | + tags.append(tag) |
| 94 | + seen.add(tag) |
| 95 | + return hits, tags |
| 96 | + |
| 97 | + |
| 98 | +def _hits_to_score(hits: int) -> float: |
| 99 | + if hits <= 0: |
| 100 | + return 0.0 |
| 101 | + return round(min(10.0, 10.0 * (1 - 0.55**hits)), 2) |
| 102 | + |
| 103 | + |
| 104 | +def _fuse(regex_score: float, llm_score: float | None) -> float: |
| 105 | + if llm_score is None: |
| 106 | + return regex_score |
| 107 | + return round(min(10.0, max(0.0, 0.75 * float(llm_score) + 0.25 * regex_score)), 2) |
| 108 | + |
| 109 | + |
| 110 | +def _band(score: float, high_acuity: bool) -> tuple[str, str]: |
| 111 | + if high_acuity: |
| 112 | + return "high", "confidential_human_followup_immediate" |
| 113 | + if score >= 7.0: |
| 114 | + return "high", "confidential_human_followup" |
| 115 | + if score >= 4.0: |
| 116 | + return "medium", "manager_check_in_suggested" |
| 117 | + return "low", "continue_monitoring" |
| 118 | + |
| 119 | + |
| 120 | +def analyze_burnout_context( |
| 121 | + *, |
| 122 | + transcript: str | None, |
| 123 | + reason: str | None = None, |
| 124 | + llm_ee: float | None = None, |
| 125 | + llm_dp: float | None = None, |
| 126 | + llm_pa: float | None = None, |
| 127 | +) -> BurnoutAnalysis: |
| 128 | + text = " ".join([reason or "", transcript or ""]).strip().lower() |
| 129 | + if not text: |
| 130 | + return BurnoutAnalysis() |
| 131 | + |
| 132 | + ee_hits, ee_tags = _scan(text, _EE_PATTERNS) |
| 133 | + dp_hits, dp_tags = _scan(text, _DP_PATTERNS) |
| 134 | + pa_hits, pa_tags = _scan(text, _PA_PATTERNS) |
| 135 | + acuity_hits, acuity_tags = _scan(text, _HIGH_ACUITY_PATTERNS) |
| 136 | + |
| 137 | + ee_score = _fuse(_hits_to_score(ee_hits), llm_ee) |
| 138 | + dp_score = _fuse(_hits_to_score(dp_hits), llm_dp) |
| 139 | + pa_score = _fuse(_hits_to_score(pa_hits), llm_pa) |
| 140 | + composite = round(0.45 * ee_score + 0.35 * dp_score + 0.20 * pa_score, 2) |
| 141 | + |
| 142 | + high_acuity = acuity_hits > 0 |
| 143 | + band, action = _band(composite, high_acuity) |
| 144 | + if high_acuity: |
| 145 | + ee_tags = list(dict.fromkeys(ee_tags + acuity_tags)) |
| 146 | + |
| 147 | + return BurnoutAnalysis( |
| 148 | + ee_hits=ee_hits, |
| 149 | + dp_hits=dp_hits, |
| 150 | + pa_hits=pa_hits, |
| 151 | + ee_tags=ee_tags[:6], |
| 152 | + dp_tags=dp_tags[:6], |
| 153 | + pa_tags=pa_tags[:6], |
| 154 | + ee_score=ee_score, |
| 155 | + dp_score=dp_score, |
| 156 | + pa_score=pa_score, |
| 157 | + composite_score=composite, |
| 158 | + risk_band=band, |
| 159 | + recommended_action=action, |
| 160 | + high_acuity_flag=high_acuity, |
| 161 | + ) |
0 commit comments