File size: 10,959 Bytes
af61b34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
"""
Taxonomy Trigger Engine.

Matches patient utterances against all 65 clinical domain trigger definitions
using phrase matching, regex patterns, partial substring matching, and
negation handling.

Safety-critical: This is the last line of defense for catching clinical
emergencies that the ML model may miss. False negatives here can be
life-threatening.

Design principles:
  - Fail open: If in doubt, nominate the domain (better to over-escalate)
  - Exhaustive matching: Check ALL domains, not just the first match
  - Negation requires explicit evidence: Only suppress if negation pattern
    clearly matches
  - Pre-compiled regex: All patterns compiled at config load time
  - Case-insensitive matching throughout
"""

from __future__ import annotations

import logging
import re
from typing import Any, Dict, List, Optional, Set, Tuple

from decision.engine.config_loader import DecisionConfigLoader
from decision.engine.models import (
    ConfidenceTier,
    MatchType,
    NegationAction,
    NegationResult,
    TriggerMatch,
)

logger = logging.getLogger("decision.trigger_engine")


class TaxonomyTriggerEngine:
    """
    Matches text against taxonomy trigger definitions.

    Usage:
        engine = TaxonomyTriggerEngine(config)
        matches = engine.match_all(text)
        # Returns: Dict[str, DomainMatchResult] keyed by domain name
    """

    def __init__(self, config: DecisionConfigLoader):
        self._config = config
        self._domains: Dict[str, Dict[str, Any]] = config.taxonomy_triggers
        logger.info(
            "TaxonomyTriggerEngine initialized with %d domains", len(self._domains)
        )

    # ------------------------------------------------------------------
    # Public API
    # ------------------------------------------------------------------

    def match_all(self, text: str) -> Dict[str, DomainMatchResult]:
        """
        Match text against ALL taxonomy domains.

        Returns a dict of domain_name -> DomainMatchResult for every domain
        that has at least one trigger match (before negation).

        Negation is evaluated but does NOT remove the domain from results.
        The caller decides what to do based on negation_result.
        """
        text_lower = text.lower().strip()
        if not text_lower:
            return {}

        results: Dict[str, DomainMatchResult] = {}

        for domain_name, triggers in self._domains.items():
            result = self._match_domain(domain_name, triggers, text, text_lower)
            if result and result.highest_confidence != ConfidenceTier.NONE:
                results[domain_name] = result

        return results

    def match_domain(self, domain_name: str, text: str) -> Optional[DomainMatchResult]:
        """Match text against a single domain's triggers."""
        triggers = self._domains.get(domain_name)
        if not triggers:
            return None
        text_lower = text.lower().strip()
        return self._match_domain(domain_name, triggers, text, text_lower)

    def check_negation(
        self, domain_name: str, text: str
    ) -> NegationResult:
        """Check if text matches negation patterns for a domain."""
        triggers = self._domains.get(domain_name, {})
        text_lower = text.lower().strip()
        return self._evaluate_negation(domain_name, triggers, text_lower)

    # ------------------------------------------------------------------
    # Internal matching
    # ------------------------------------------------------------------

    def _match_domain(
        self,
        domain_name: str,
        triggers: Dict[str, Any],
        text: str,
        text_lower: str,
    ) -> Optional[DomainMatchResult]:
        """Match text against a single domain's trigger definition."""
        lexical = triggers.get("lexical_signals", {})
        priority = triggers.get("priority", 0)

        all_matches: List[TriggerMatch] = []

        # Check each confidence tier (high → medium → low)
        for tier_name, tier_enum in [
            ("high", ConfidenceTier.HIGH),
            ("medium", ConfidenceTier.MEDIUM),
            ("low", ConfidenceTier.LOW),
        ]:
            tier = lexical.get(tier_name, {})
            tier_matches = self._match_tier(
                domain_name, tier, tier_enum, text, text_lower, priority
            )
            all_matches.extend(tier_matches)

        if not all_matches:
            return None

        # Determine highest confidence from matches
        highest = ConfidenceTier.NONE
        for m in all_matches:
            if m.confidence_tier > highest:
                highest = m.confidence_tier

        # Evaluate negation
        negation = self._evaluate_negation(domain_name, triggers, text_lower)

        # Apply negation to adjust confidence
        effective_confidence = highest
        if negation.is_negated:
            if negation.action == NegationAction.SUPPRESS:
                effective_confidence = ConfidenceTier.NONE
            elif negation.action == NegationAction.DOWNGRADE_CONFIDENCE:
                effective_confidence = self._downgrade_tier(highest)

        return DomainMatchResult(
            domain=domain_name,
            priority=priority,
            matches=tuple(all_matches),
            highest_confidence=highest,
            effective_confidence=effective_confidence,
            negation_result=negation,
        )

    def _match_tier(
        self,
        domain_name: str,
        tier: Dict[str, Any],
        tier_enum: ConfidenceTier,
        text: str,
        text_lower: str,
        priority: int,
    ) -> List[TriggerMatch]:
        """Match text against a single confidence tier."""
        matches: List[TriggerMatch] = []

        # 1. Phrase matching (exact substring, case-insensitive)
        for phrase in tier.get("phrases", []):
            phrase_lower = phrase.lower()
            idx = text_lower.find(phrase_lower)
            if idx >= 0:
                matched_span = text[idx : idx + len(phrase)]
                matches.append(
                    TriggerMatch(
                        domain=domain_name,
                        confidence_tier=tier_enum,
                        match_type=MatchType.PHRASE,
                        matched_text=phrase,
                        matched_span=matched_span,
                        priority=priority,
                    )
                )

        # 2. Regex matching (pre-compiled)
        for compiled_re in tier.get("_compiled_regex", []):
            m = compiled_re.search(text)
            if m:
                matches.append(
                    TriggerMatch(
                        domain=domain_name,
                        confidence_tier=tier_enum,
                        match_type=MatchType.REGEX,
                        matched_text=compiled_re.pattern,
                        matched_span=m.group(0),
                        priority=priority,
                    )
                )

        # 3. Partial matching (substring, case-insensitive)
        for partial in tier.get("partials", []):
            partial_lower = partial.lower()
            idx = text_lower.find(partial_lower)
            if idx >= 0:
                matched_span = text[idx : idx + len(partial)]
                matches.append(
                    TriggerMatch(
                        domain=domain_name,
                        confidence_tier=tier_enum,
                        match_type=MatchType.PARTIAL,
                        matched_text=partial,
                        matched_span=matched_span,
                        priority=priority,
                    )
                )

        return matches

    def _evaluate_negation(
        self,
        domain_name: str,
        triggers: Dict[str, Any],
        text_lower: str,
    ) -> NegationResult:
        """
        Evaluate negation patterns for a domain.

        SAFETY DESIGN: Negation requires an EXPLICIT match against a known
        negation pattern. We do NOT use generic "no/not" detection because
        that risks suppressing true emergencies.
        """
        negation_config = triggers.get("negation_handling", {})
        compiled_patterns: List[str] = negation_config.get("_compiled_patterns", [])
        action_str = negation_config.get("action", "downgrade_confidence")

        try:
            action = NegationAction(action_str)
        except ValueError:
            action = NegationAction.DOWNGRADE_CONFIDENCE

        for pattern in compiled_patterns:
            if pattern in text_lower:
                return NegationResult(
                    is_negated=True,
                    action=action,
                    matched_pattern=pattern,
                )

        return NegationResult(
            is_negated=False,
            action=action,
        )

    @staticmethod
    def _downgrade_tier(tier: ConfidenceTier) -> ConfidenceTier:
        """Downgrade confidence by one level."""
        if tier == ConfidenceTier.HIGH:
            return ConfidenceTier.MEDIUM
        elif tier == ConfidenceTier.MEDIUM:
            return ConfidenceTier.LOW
        elif tier == ConfidenceTier.LOW:
            return ConfidenceTier.NONE
        return ConfidenceTier.NONE


# ---------------------------------------------------------------------------
# Domain Match Result
# ---------------------------------------------------------------------------

class DomainMatchResult:
    """
    Result of matching a single domain against patient text.

    Contains all trigger matches, the highest raw confidence,
    effective confidence (after negation), and negation details.
    """

    __slots__ = (
        "domain",
        "priority",
        "matches",
        "highest_confidence",
        "effective_confidence",
        "negation_result",
    )

    def __init__(
        self,
        domain: str,
        priority: int,
        matches: Tuple[TriggerMatch, ...],
        highest_confidence: ConfidenceTier,
        effective_confidence: ConfidenceTier,
        negation_result: NegationResult,
    ):
        self.domain = domain
        self.priority = priority
        self.matches = matches
        self.highest_confidence = highest_confidence
        self.effective_confidence = effective_confidence
        self.negation_result = negation_result

    @property
    def is_negated(self) -> bool:
        return self.negation_result.is_negated

    @property
    def is_suppressed(self) -> bool:
        return self.effective_confidence == ConfidenceTier.NONE

    @property
    def match_count(self) -> int:
        return len(self.matches)

    def __repr__(self) -> str:
        neg = " [NEGATED]" if self.is_negated else ""
        return (
            f"DomainMatchResult({self.domain}, "
            f"confidence={self.effective_confidence.value}, "
            f"priority={self.priority}, "
            f"matches={self.match_count}{neg})"
        )