File size: 7,800 Bytes
b6f9fa8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
FR-14: src/modules/source_credibility.py β€” Module 3: Source Credibility Scoring
=================================================================================
Scores the credibility of retrieved source documents based on their publication
type / evidence tier.

Tier weights (SRS Section 6.3):
    clinical_guideline   β†’ 1.00  (Tier 1 β€” highest authority)
    systematic_review    β†’ 0.85  (Tier 2)
    research_abstract    β†’ 0.70  (Tier 3 β€” PubMedQA default)
    review_article       β†’ 0.60  (Tier 4)
    clinical_case        β†’ 0.50  (Tier 5)
    unknown / other      β†’ 0.30  (fallback)

Detection:
    1. Use 'tier_type' metadata field if present (set by embedder.py)
    2. Fall back to keyword matching in pub_type / title text

Score = weighted mean of tier weights across all retrieved chunks.

Each chunk must be a dict with at minimum:
    {"text": str, "metadata": {"tier_type": str, "pub_type": str, "title": str}}
or the simpler form accepted by the retriever:
    {"text": str, "source": str, "tier_type": str, "title": str}
"""
from __future__ import annotations

import logging
import re
import time

from src.modules.base import EvalResult

logger = logging.getLogger(__name__)

# ---------------------------------------------------------------------------
# Evidence tier weights
# ---------------------------------------------------------------------------

TIER_WEIGHTS: dict[str, float] = {
    "clinical_guideline":  1.00,
    "systematic_review":   0.85,
    "drug_label":          0.90,  # FDA-approved drug labels β€” authoritative regulatory source
    "research_abstract":   0.70,
    "review_article":      0.60,
    "clinical_case":       0.50,
    "unknown":             0.30,
}

# Keyword β†’ tier_type mapping for fallback text matching
_KEYWORD_MAP: list[tuple[re.Pattern, str]] = [
    (re.compile(r"\b(guideline|clinical practice|recommendation|consensus)\b", re.I), "clinical_guideline"),
    (re.compile(r"\b(systematic review|meta.?analysis)\b", re.I),                    "systematic_review"),
    # RCT / controlled trial β†’ highest single-study evidence tier
    (re.compile(r"\b(randomized|randomised|controlled trial|rct|clinical trial)\b", re.I), "clinical_guideline"),
    # FDA drug labels
    (re.compile(r"\b(fda|drug label|prescribing information|package insert|dailymed)\b", re.I), "drug_label"),
    (re.compile(r"\b(review|overview)\b", re.I),                                     "review_article"),
    (re.compile(r"\b(case report|case study|clinical case)\b", re.I),                "clinical_case"),
    (re.compile(r"\b(abstract|research article|original article|journal)\b", re.I),  "research_abstract"),
]


def _classify_tier(chunk: dict) -> tuple[str, str | None]:
    """
    Return (tier_type, matched_keyword) for a single retrieved chunk dict.

    Priority 1: explicit tier_type field (set by embedder.py)
    Priority 2: pub_type field directly maps to a known tier name
    Priority 3: keyword regex on pub_type + title text
    """
    # Priority 1: explicit tier_type already set (e.g., by embedder.py)
    tier = (
        chunk.get("tier_type")
        or chunk.get("metadata", {}).get("tier_type")
    )
    if tier and tier in TIER_WEIGHTS:
        return tier, None

    # Priority 2: direct pub_type value lookup
    # Handles underscore-separated values like "research_abstract" which
    # won't match word-boundary regex patterns
    pub_type_raw = str(
        chunk.get("pub_type") or chunk.get("metadata", {}).get("pub_type") or ""
    ).strip().lower()

    _PUB_TYPE_DIRECT: dict[str, str] = {
        "research_abstract":  "research_abstract",
        "abstract":           "research_abstract",
        "systematic_review":  "systematic_review",
        "systematic review":  "systematic_review",
        "meta_analysis":      "systematic_review",
        "meta-analysis":      "systematic_review",
        "drug_label":         "drug_label",
        "drug label":         "drug_label",
        "clinical_guideline": "clinical_guideline",
        "clinical guideline": "clinical_guideline",
        "guideline":          "clinical_guideline",
        "review_article":     "review_article",
        "review article":     "review_article",
        "review":             "review_article",
        "clinical_case":      "clinical_case",
        "case_report":        "clinical_case",
        "case report":        "clinical_case",
    }
    if pub_type_raw in _PUB_TYPE_DIRECT:
        return _PUB_TYPE_DIRECT[pub_type_raw], None

    # Priority 3: keyword regex on pub_type + title text
    title = str(chunk.get("title") or chunk.get("metadata", {}).get("title") or "")
    text_to_search = f"{pub_type_raw} {title}"

    for pattern, matched_tier in _KEYWORD_MAP:
        m = pattern.search(text_to_search)
        if m:
            return matched_tier, m.group(0)


    return "unknown", None


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

def score_source_credibility(
    retrieved_chunks: list[dict],
) -> EvalResult:
    """
    Score the credibility of a set of retrieved source documents.

    Args:
        retrieved_chunks : List of chunk dicts as returned by retriever.retrieve().
                          Each must contain at minimum 'text' and ideally
                          'tier_type', 'pub_type', 'title', 'chunk_id' fields.

    Returns:
        EvalResult with module_name="source_credibility", score in [0,1], and
        details matching the shape from src/modules/__init__.py.
    """
    t0 = time.perf_counter()

    if not retrieved_chunks:
        return EvalResult(
            module_name="source_credibility",
            score=0.0,
            details={"chunks": [], "method_used": "none"},
            error="No chunks provided",
            latency_ms=0,
        )

    chunk_details: list[dict] = []
    weights: list[float] = []
    method_used = "metadata"  # assume metadata-first; may switch to keyword

    for i, chunk in enumerate(retrieved_chunks):
        tier_type, matched_kw = _classify_tier(chunk)
        weight = TIER_WEIGHTS.get(tier_type, TIER_WEIGHTS["unknown"])
        weights.append(weight)

        if matched_kw:
            method_used = "keyword"

        # Compute tier number (1-5) for display
        tier_num = {
            "clinical_guideline": 1,
            "systematic_review":  2,
            "research_abstract":  3,
            "review_article":     4,
            "clinical_case":      5,
        }.get(tier_type, 6)  # 6 = unknown/unclassified

        chunk_details.append(
            {
                "chunk_id": chunk.get("chunk_id") or chunk.get("metadata", {}).get("chunk_id") or f"chunk_{i}",
                "tier": tier_num,
                "tier_type": tier_type,
                "tier_weight": round(weight, 2),
                "pub_type": chunk.get("pub_type") or chunk.get("metadata", {}).get("pub_type") or "",
                "title": (chunk.get("title") or chunk.get("metadata", {}).get("title") or "")[:80],
                "matched_keyword": matched_kw,
            }
        )

    score = sum(weights) / len(weights) if weights else 0.0

    details = {
        "method_used": method_used,
        "chunk_count": len(retrieved_chunks),
        "avg_tier_weight": round(score, 4),
        "chunks": chunk_details,
    }

    latency_ms = int((time.perf_counter() - t0) * 1000)
    logger.info(
        "Source credibility: %.3f (avg tier weight over %d chunks) in %d ms",
        score, len(retrieved_chunks), latency_ms,
    )
    return EvalResult(
        module_name="source_credibility",
        score=score,
        details=details,
        latency_ms=latency_ms,
    )