-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevidence.py
More file actions
269 lines (227 loc) · 8.8 KB
/
Copy pathevidence.py
File metadata and controls
269 lines (227 loc) · 8.8 KB
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
"""Traceable evidence references and deterministic confidence scoring."""
from __future__ import annotations
from enum import Enum
from typing import Literal
from pydantic import Field, model_validator
from paperreading.domain.base import DomainModel
class EvidenceType(str, Enum):
"""Supported source surfaces for a research statement."""
TEXT = "TEXT"
TABLE = "TABLE"
FIGURE = "FIGURE"
EQUATION = "EQUATION"
APPENDIX = "APPENDIX"
METADATA = "METADATA"
class ConfidenceLevel(str, Enum):
HIGH = "high"
MEDIUM = "medium"
LOW = "low"
class Confidence(DomainModel):
"""Rule-derived confidence, not an unconstrained model opinion."""
score: float = Field(ge=0, le=1)
level: ConfidenceLevel
reasons: list[str] = Field(min_length=1)
class EvidenceRef(DomainModel):
"""A source-aware locator for one claim or reported result."""
source_id: str = Field(min_length=1)
type: EvidenceType
page: int | None = Field(default=None, ge=1)
section: str | None = None
table: str | None = None
column: str | None = None
figure: str | None = None
equation: str | None = None
appendix: str | None = None
text: str | None = None
confidence: Confidence | None = None
@model_validator(mode="after")
def require_provenance_and_score(self) -> EvidenceRef:
"""Require a reviewable locator and replace arbitrary confidence values."""
locator_values = (
self.page,
self.section,
self.table,
self.column,
self.figure,
self.equation,
self.appendix,
)
if not self.text and not any(value is not None for value in locator_values):
raise ValueError(
"evidence must include quoted/paraphrased text or a source locator"
)
object.__setattr__(self, "confidence", _score_confidence(self))
return self
def _score_confidence(evidence: EvidenceRef) -> Confidence:
"""Apply the repository's transparent locator-specific scoring rule."""
locator_count = sum(
value is not None
for value in (
evidence.page,
evidence.section,
evidence.table,
evidence.column,
evidence.figure,
evidence.equation,
evidence.appendix,
)
)
reasons = ["source_id is present"]
if evidence.page and evidence.table and evidence.column:
specificity = 0.98
reasons.append("page, table, and column identify a precise result")
elif evidence.page and evidence.section:
specificity = 0.92
reasons.append("page and section identify a precise passage")
elif locator_count:
specificity = 0.80
reasons.append("at least one structural locator is present")
else:
specificity = 0.65
reasons.append("text is present without a structural locator")
extraction_consistency = 0.95 if evidence.text and locator_count else 0.90
cross_check = 0.96 if locator_count >= 3 else 0.90
score = round(specificity * extraction_consistency * cross_check, 3)
if score >= 0.75:
level = ConfidenceLevel.HIGH
elif score >= 0.45:
level = ConfidenceLevel.MEDIUM
else:
level = ConfidenceLevel.LOW
reasons.append("score is rule-derived from locator specificity")
return Confidence(score=score, level=level, reasons=reasons)
class TraceabilityScore(DomainModel):
"""Locator-specificity score; it is not a truth or study-quality score."""
score: float = Field(ge=0, le=1)
level: ConfidenceLevel
reasons: list[str] = Field(min_length=1)
scoring_version: Literal["1"] = "1"
class VerificationStatus(str, Enum):
VERIFIED = "verified"
PARTIAL = "partial"
FAILED = "failed"
NOT_RUN = "not_run"
class EvidenceVerification(DomainModel):
"""Result of checking an EvidenceSpan against a supplied PaperDocument."""
status: VerificationStatus
source_match: bool | None = None
page_resolved: bool | None = None
block_resolved: bool | None = None
char_range_resolved: bool | None = None
quote_match: float | None = Field(default=None, ge=0, le=1)
text_hash_match: bool | None = None
issues: list[str] = Field(default_factory=list)
verifier_version: str = "0.3.1"
@model_validator(mode="after")
def validate_run_state(self) -> EvidenceVerification:
checks = (
self.source_match,
self.page_resolved,
self.block_resolved,
self.char_range_resolved,
self.quote_match,
self.text_hash_match,
)
if self.status is VerificationStatus.NOT_RUN:
if any(item is not None for item in checks) or self.issues:
raise ValueError("not-run verification cannot contain check results")
elif self.source_match is None:
raise ValueError("completed verification requires source_match")
return self
class EvidenceSpan(DomainModel):
"""Normalized evidence node referenced by ID from v0.3 research objects."""
evidence_id: str = Field(min_length=1)
source_id: str = Field(min_length=1)
type: EvidenceType
page: int | None = Field(default=None, ge=1)
section_path: list[str] = Field(default_factory=list)
block_id: str | None = None
char_start: int | None = Field(default=None, ge=0)
char_end: int | None = Field(default=None, ge=0)
bbox: tuple[float, float, float, float] | None = None
table: str | None = None
column: str | None = None
figure: str | None = None
equation: str | None = None
appendix: str | None = None
quoted_text: str | None = None
text_hash: str | None = Field(default=None, pattern=r"^[0-9a-f]{64}$")
traceability: TraceabilityScore | None = None
verification: EvidenceVerification | None = None
@model_validator(mode="after")
def validate_span_and_score(self) -> EvidenceSpan:
structural = (
self.page,
self.section_path,
self.block_id,
self.bbox,
self.table,
self.column,
self.figure,
self.equation,
self.appendix,
)
if not self.quoted_text and not any(structural):
raise ValueError(
"evidence span must include quoted text or a structural locator"
)
if (self.char_start is None) != (self.char_end is None):
raise ValueError("char_start and char_end must be supplied together")
if self.char_start is not None and not self.block_id:
raise ValueError("character ranges require a block_id")
if (
self.char_start is not None
and self.char_end is not None
and self.char_end <= self.char_start
):
raise ValueError("char_end must be greater than char_start")
if self.text_hash and not self.quoted_text:
raise ValueError("text_hash requires quoted_text")
if self.bbox:
if self.page is None:
raise ValueError("bbox requires a page locator")
x1, y1, x2, y2 = self.bbox
if x2 <= x1 or y2 <= y1:
raise ValueError("bbox must have increasing coordinates")
object.__setattr__(self, "traceability", score_traceability(self))
return self
def score_traceability(evidence: EvidenceSpan) -> TraceabilityScore:
"""Calculate locator specificity without claiming source-content verification."""
locator_count = sum(
bool(value)
for value in (
evidence.page,
evidence.section_path,
evidence.block_id,
evidence.bbox,
evidence.table,
evidence.column,
evidence.figure,
evidence.equation,
evidence.appendix,
)
)
reasons = ["source_id is present"]
if evidence.page and evidence.block_id and evidence.char_start is not None:
score = 0.98
reasons.append("page, block, and character range are present")
elif evidence.page and evidence.table and evidence.column:
score = 0.95
reasons.append("page, table, and column are present")
elif evidence.page and evidence.section_path:
score = 0.85
reasons.append("page and section path are present")
elif locator_count:
score = 0.70
reasons.append("at least one structural locator is present")
else:
score = 0.50
reasons.append("quoted text is present without a structural locator")
if score >= 0.80:
level = ConfidenceLevel.HIGH
elif score >= 0.55:
level = ConfidenceLevel.MEDIUM
else:
level = ConfidenceLevel.LOW
reasons.append("score measures traceability specificity only")
return TraceabilityScore(score=score, level=level, reasons=reasons)