veriph-ai 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- beacon/__init__.py +32 -0
- beacon/attribution.py +419 -0
- beacon/attribution_check.py +1257 -0
- beacon/attribution_reported.py +1376 -0
- beacon/bias_invariance.py +814 -0
- beacon/bias_names.py +195 -0
- beacon/bias_reviewer.py +240 -0
- beacon/call_log.py +303 -0
- beacon/caption_grade.py +213 -0
- beacon/case_name_abbreviations.py +59 -0
- beacon/checks/__init__.py +1 -0
- beacon/checks/adverse_authority.py +1503 -0
- beacon/checks/attachment.py +304 -0
- beacon/checks/base.py +333 -0
- beacon/checks/citation_date.py +422 -0
- beacon/checks/citation_existence.py +1172 -0
- beacon/checks/description_grounding.py +384 -0
- beacon/checks/holding.py +558 -0
- beacon/checks/quote_integrity.py +2567 -0
- beacon/cli.py +882 -0
- beacon/cloak.py +482 -0
- beacon/console.py +39 -0
- beacon/corpus_text.py +87 -0
- beacon/coverage.py +1426 -0
- beacon/data/contested_key_assignments.json +10 -0
- beacon/data/contested_key_assignments.tsv.gz +0 -0
- beacon/data/e1_per_type.json +26 -0
- beacon/data/wikidata_aliases.json +258 -0
- beacon/datapaths.py +39 -0
- beacon/decided_line.py +46 -0
- beacon/declared_sources.py +291 -0
- beacon/deterministic_eyecite.py +102 -0
- beacon/entities.py +224 -0
- beacon/entity_aliases.py +174 -0
- beacon/extract.py +1649 -0
- beacon/extraction_artefacts.py +134 -0
- beacon/file_lock.py +120 -0
- beacon/gate.py +1415 -0
- beacon/groundtruth/__init__.py +1 -0
- beacon/groundtruth/citation_keys.py +462 -0
- beacon/groundtruth/courtlistener.py +1949 -0
- beacon/groundtruth/courtlistener_quota.py +520 -0
- beacon/groundtruth/documents.py +504 -0
- beacon/groundtruth/local_search.py +423 -0
- beacon/groundtruth/local_store.py +1135 -0
- beacon/groundtruth/lookup_keys.py +111 -0
- beacon/groundtruth/remote_lookup.py +299 -0
- beacon/groundtruth/treatment.py +162 -0
- beacon/hidden_text.py +191 -0
- beacon/impossible_cite_lane.py +313 -0
- beacon/intake.py +210 -0
- beacon/internal_summary.py +272 -0
- beacon/jurisdiction.py +469 -0
- beacon/lookup_client.py +100 -0
- beacon/lookup_service.py +488 -0
- beacon/mcp_auth.py +466 -0
- beacon/mcp_server.py +2182 -0
- beacon/name_binding.py +140 -0
- beacon/names.py +209 -0
- beacon/netpolicy.py +238 -0
- beacon/pdf_sandbox.py +425 -0
- beacon/private_files.py +125 -0
- beacon/promptsafety.py +163 -0
- beacon/provenance.py +334 -0
- beacon/record_log.py +203 -0
- beacon/reporter_universe.py +214 -0
- beacon/schema.py +1522 -0
- beacon/statutes.py +429 -0
- beacon/store_build.py +297 -0
- beacon/text_layer.py +599 -0
- beacon/textutil.py +785 -0
- beacon/token_command.py +85 -0
- beacon/user_settings.py +511 -0
- beacon/verification_record.py +619 -0
- beacon/verifier/__init__.py +1 -0
- beacon/verifier/base.py +457 -0
- beacon/verifier/choose.py +277 -0
- beacon/verifier/claim_ranker.py +157 -0
- beacon/verifier/local.py +613 -0
- beacon/verifier/triage.py +216 -0
- beacon/verifier/typesafe.py +473 -0
- veriph_ai-1.0.0.dist-info/METADATA +86 -0
- veriph_ai-1.0.0.dist-info/RECORD +87 -0
- veriph_ai-1.0.0.dist-info/WHEEL +4 -0
- veriph_ai-1.0.0.dist-info/entry_points.txt +4 -0
- veriph_ai-1.0.0.dist-info/licenses/LICENSE +202 -0
- veriph_ai-1.0.0.dist-info/licenses/NOTICE +11 -0
beacon/__init__.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"""Beacon — independent verification layer for AI-generated legal work product."""
|
|
2
|
+
|
|
3
|
+
from beacon.schema import (
|
|
4
|
+
Authority,
|
|
5
|
+
CheckClass,
|
|
6
|
+
Claim,
|
|
7
|
+
ConflictEntry,
|
|
8
|
+
Determination,
|
|
9
|
+
Finding,
|
|
10
|
+
GateStatus,
|
|
11
|
+
MatterContext,
|
|
12
|
+
Severity,
|
|
13
|
+
VerificationRequest,
|
|
14
|
+
VerificationVerdict,
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
__version__ = "1.0.0"
|
|
18
|
+
|
|
19
|
+
__all__ = [
|
|
20
|
+
"Authority",
|
|
21
|
+
"CheckClass",
|
|
22
|
+
"Claim",
|
|
23
|
+
"ConflictEntry",
|
|
24
|
+
"Determination",
|
|
25
|
+
"Finding",
|
|
26
|
+
"GateStatus",
|
|
27
|
+
"MatterContext",
|
|
28
|
+
"Severity",
|
|
29
|
+
"VerificationRequest",
|
|
30
|
+
"VerificationVerdict",
|
|
31
|
+
"__version__",
|
|
32
|
+
]
|
beacon/attribution.py
ADDED
|
@@ -0,0 +1,419 @@
|
|
|
1
|
+
"""Attribution claims — who a document says produced a passage.
|
|
2
|
+
|
|
3
|
+
Quote integrity asks whether wording exists in a source. Attribution integrity
|
|
4
|
+
asks whether the source says *this person* produced it. This module does the
|
|
5
|
+
first half: it finds the claims. It does not adjudicate them.
|
|
6
|
+
|
|
7
|
+
Enumeration is worth shipping on its own, because an attribution claim is
|
|
8
|
+
currently invisible to every other check here. "X says Y" with no quotation
|
|
9
|
+
marks is not a citation, not a quotation, and not a claim about the law — so a
|
|
10
|
+
document's attribution surface is unmeasured, and unmeasured is where this
|
|
11
|
+
project's failures have consistently lived. Listing the claims converts an
|
|
12
|
+
unbounded risk into a finite one.
|
|
13
|
+
|
|
14
|
+
Five documented failures motivate it, all from this vault, all caught by hand:
|
|
15
|
+
|
|
16
|
+
McCormack undiarized ASR merged two speakers; the text was attributed to
|
|
17
|
+
the wrong one
|
|
18
|
+
Ramanathan a quotation attributed to a named executive that appears
|
|
19
|
+
nowhere in the transcript
|
|
20
|
+
Weinberg an attributed clause carrying the opposite of what was said
|
|
21
|
+
Levy a paraphrase presented as verbatim
|
|
22
|
+
Fox a reporter's characterisation attributed to the subject, with
|
|
23
|
+
the hedge stripped in the move
|
|
24
|
+
|
|
25
|
+
Note the last one has no quotation marks at all. That is why enumeration comes
|
|
26
|
+
before adjudication: the claims that most need checking are the ones no
|
|
27
|
+
quotation-shaped check can see.
|
|
28
|
+
|
|
29
|
+
See docs/design-attribution-integrity.md for the full check-class design.
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
from __future__ import annotations
|
|
33
|
+
|
|
34
|
+
import re
|
|
35
|
+
from dataclasses import dataclass
|
|
36
|
+
from enum import StrEnum
|
|
37
|
+
|
|
38
|
+
from beacon.textutil import quoted_spans
|
|
39
|
+
|
|
40
|
+
#: Verbs and phrases that credit a passage to someone on their own: one of these
|
|
41
|
+
#: counts straight after a name or a pronoun, and after a name with an
|
|
42
|
+
#: appositive; a pronoun takes no appositive. It needs no complement to do so.
|
|
43
|
+
#: Deliberately broad on reporting verbs and narrow on everything else: a missed
|
|
44
|
+
#: cue is a claim that never reaches the inventory, which is the failure mode
|
|
45
|
+
#: that matters here. Verbs that only report with a complement live in
|
|
46
|
+
#: `_COMPLEMENT_CUE` instead.
|
|
47
|
+
_SPEECH_CUE = (
|
|
48
|
+
r"said|says|say|wrote|writes|told|tells|argued|argues|noted|notes|"
|
|
49
|
+
r"observed|observes|claimed|claims|described|describes|added|adds|"
|
|
50
|
+
r"explained|explains|recalled|recalls|recounted|recounts|stated|states|"
|
|
51
|
+
r"reported|reports|testified|testifies|articulated|articulates|"
|
|
52
|
+
r"framed|frames|conceded|concedes|acknowledged|acknowledges|"
|
|
53
|
+
r"warned|warns|insisted|insists|put it|calls it|called it"
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
#: Verbs that only report when a complement follows. `… maintained the bridge`
|
|
57
|
+
#: and `… denied him entry` report upkeep and a refusal, not speech, so one of
|
|
58
|
+
#: these counts only when `_COMPLEMENT_AFTER` follows it. Lower case only.
|
|
59
|
+
_COMPLEMENT_CUE = (
|
|
60
|
+
r"suggested|suggests|proposed|proposes|asserted|asserts|predicted|predicts|"
|
|
61
|
+
r"denied|denies|maintained|maintains|cautioned|cautions|promised|promises"
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
#: What may follow a `_COMPLEMENT_CUE` word for it to count: after any
|
|
65
|
+
#: whitespace, line breaks included, the word `that`, a quotation mark, the word
|
|
66
|
+
#: `not`, or a word of five or more letters ending in "ing"; or a colon straight
|
|
67
|
+
#: after the word, with nothing between. The `-ing` branch also admits nouns
|
|
68
|
+
#: ending in "ing" ("funding"), a known limit of the rule.
|
|
69
|
+
_COMPLEMENT_AFTER = r"(?::|\s*(?:that\b|[\"“]|not\b|[a-z]{2,}ing\b))"
|
|
70
|
+
|
|
71
|
+
#: One to four capitalised tokens. Initials and internal punctuation allowed so
|
|
72
|
+
#: "Raghu Ramanathan", "M. Fox" and "O'Brien" survive.
|
|
73
|
+
_NAME = r"(?:[A-Z][\w'’\-]*\.?(?:\s+[A-Z][\w'’\-]*\.?){0,3})" # noqa: RUF001
|
|
74
|
+
|
|
75
|
+
#: Capitalised words that begin sentences and are not speakers. Without this,
|
|
76
|
+
#: every sentence opening with "The" or "This" before a reporting verb produces
|
|
77
|
+
#: a phantom speaker.
|
|
78
|
+
_NOT_A_SPEAKER = frozenset(
|
|
79
|
+
[
|
|
80
|
+
"the",
|
|
81
|
+
"this",
|
|
82
|
+
"that",
|
|
83
|
+
"these",
|
|
84
|
+
"those",
|
|
85
|
+
"but",
|
|
86
|
+
"and",
|
|
87
|
+
"however",
|
|
88
|
+
"here",
|
|
89
|
+
"there",
|
|
90
|
+
"what",
|
|
91
|
+
"which",
|
|
92
|
+
"who",
|
|
93
|
+
"whose",
|
|
94
|
+
"if",
|
|
95
|
+
"as",
|
|
96
|
+
"in",
|
|
97
|
+
"on",
|
|
98
|
+
"at",
|
|
99
|
+
"a",
|
|
100
|
+
"an",
|
|
101
|
+
"both",
|
|
102
|
+
"neither",
|
|
103
|
+
"either",
|
|
104
|
+
"some",
|
|
105
|
+
"many",
|
|
106
|
+
"most",
|
|
107
|
+
"one",
|
|
108
|
+
"two",
|
|
109
|
+
"three",
|
|
110
|
+
"when",
|
|
111
|
+
"where",
|
|
112
|
+
"while",
|
|
113
|
+
"though",
|
|
114
|
+
"although",
|
|
115
|
+
"because",
|
|
116
|
+
"so",
|
|
117
|
+
"then",
|
|
118
|
+
"now",
|
|
119
|
+
"later",
|
|
120
|
+
"today",
|
|
121
|
+
"yesterday",
|
|
122
|
+
"still",
|
|
123
|
+
"yet",
|
|
124
|
+
"also",
|
|
125
|
+
"thus",
|
|
126
|
+
"hence",
|
|
127
|
+
"therefore",
|
|
128
|
+
"meanwhile",
|
|
129
|
+
"instead",
|
|
130
|
+
"rather",
|
|
131
|
+
]
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
#: Pronoun subjects are real attribution claims whose speaker cannot be resolved
|
|
135
|
+
#: without coreference. They are inventoried and marked, never silently dropped:
|
|
136
|
+
#: an unresolvable attribution is harder for a human to check, not easier.
|
|
137
|
+
_PRONOUNS = frozenset(["he", "she", "they", "it", "we", "i", "you"])
|
|
138
|
+
|
|
139
|
+
#: Possessive determiners are never speakers. "Its described function is
|
|
140
|
+
#: structural" is an adjective, not a report.
|
|
141
|
+
_POSSESSIVES = frozenset(["its", "his", "her", "their", "our", "my", "your"])
|
|
142
|
+
|
|
143
|
+
#: A determiner before a *one-word* capitalised token marks a common noun rather
|
|
144
|
+
#: than a name: "the AI told us" attributes nothing to anyone called AI.
|
|
145
|
+
#: Multi-token names are exempt, because "The Financial Times reported" is one.
|
|
146
|
+
_DETERMINER_BEFORE_RE = re.compile(r"\b(?:the|a|an)\s+$", re.I)
|
|
147
|
+
|
|
148
|
+
#: A gerund is a heading or a noun phrase, not a speaker: "## Drafting notes".
|
|
149
|
+
_GERUND_RE = re.compile(r"ing$", re.I)
|
|
150
|
+
|
|
151
|
+
#: Subject first, cue second: `Maria Okafor said …`. A name or a pronoun may be
|
|
152
|
+
#: the subject, and only a `_SPEECH_CUE` word counts here, because those need no
|
|
153
|
+
#: complement. Two widenings sit below: a `_COMPLEMENT_CUE` word needs a
|
|
154
|
+
#: complement to count, and a name may carry a bracketed appositive between it
|
|
155
|
+
#: and its cue (`Maria Okafor, the director, said …`), which only a name may do.
|
|
156
|
+
_SUBJECT_FIRST_RE = re.compile(
|
|
157
|
+
rf"\b(?P<name>{_NAME}|[Hh]e|[Ss]he|[Tt]hey|[Ii]t|[Ww]e)\s+(?P<cue>{_SPEECH_CUE})\b"
|
|
158
|
+
)
|
|
159
|
+
|
|
160
|
+
#: Subject first, then a complement-requiring cue. The complement lookahead is
|
|
161
|
+
#: zero-width and outside the match, so the span still ends with the verb.
|
|
162
|
+
_SUBJECT_FIRST_COMPLEMENT_RE = re.compile(
|
|
163
|
+
rf"\b(?P<name>{_NAME}|[Hh]e|[Ss]he|[Tt]hey|[Ii]t|[Ww]e)\s+"
|
|
164
|
+
rf"(?P<cue>{_COMPLEMENT_CUE})\b(?={_COMPLEMENT_AFTER})"
|
|
165
|
+
)
|
|
166
|
+
|
|
167
|
+
#: A name, then an appositive, then a cue. The appositive begins with a
|
|
168
|
+
#: lower-case letter, is one to eighty characters, and holds no comma, period,
|
|
169
|
+
#: quotation mark, semicolon, colon or line break; apostrophes are allowed. It
|
|
170
|
+
#: must close with a comma before the cue, so a following clause is not mistaken
|
|
171
|
+
#: for its tail. Pronouns are not names here: `He, the director, said …` is left
|
|
172
|
+
#: out, and so is a pronoun spelled after a sentence opener.
|
|
173
|
+
_APPOSITIVE_BODY = r"[a-z][^,.\"“”;:\n]{0,79}"
|
|
174
|
+
_APPOSITIVE_RE = re.compile(
|
|
175
|
+
rf"\b(?P<name>{_NAME}), "
|
|
176
|
+
rf"(?P<appositive>{_APPOSITIVE_BODY}), "
|
|
177
|
+
rf"(?P<cue>{_SPEECH_CUE}|(?:{_COMPLEMENT_CUE})(?={_COMPLEMENT_AFTER}))\b"
|
|
178
|
+
)
|
|
179
|
+
|
|
180
|
+
#: Inversion — `"…," said Fox.` Anchored to a preceding quotation mark on
|
|
181
|
+
#: purpose: bare `said <Capitalised>` also matches "said Beacon should ship",
|
|
182
|
+
#: where the capitalised token is the subject of the reported clause rather than
|
|
183
|
+
#: the speaker.
|
|
184
|
+
_INVERTED_RE = re.compile(
|
|
185
|
+
rf"[\"”][,.]?\s+(?P<cue>said|says|wrote|told|noted|added|explained|argued|"
|
|
186
|
+
rf"recalled|observed)\s+(?P<name>{_NAME})\b"
|
|
187
|
+
)
|
|
188
|
+
|
|
189
|
+
#: Case-insensitivity cannot be applied to these patterns as a whole: `re.I`
|
|
190
|
+
#: makes the `[A-Z]` in the name class match lowercase, so "according to the
|
|
191
|
+
#: report" yields "report" as a speaker. Sentence-initial capitals are spelled
|
|
192
|
+
#: out instead.
|
|
193
|
+
_ACCORDING_TO_RE = re.compile(rf"\b(?P<cue>[Aa]ccording to)\s+(?P<name>{_NAME})\b")
|
|
194
|
+
|
|
195
|
+
#: The name class admits apostrophes so "O'Brien" survives, which means a greedy
|
|
196
|
+
#: match swallows the possessive it is supposed to leave behind. Matched lazily
|
|
197
|
+
#: here so "Junestrand's" yields the name and the suffix separately.
|
|
198
|
+
_IN_WORDS_RE = re.compile(
|
|
199
|
+
r"\b(?P<cue>[Ii]n)\s+(?P<name>[A-Z][\w'’\-]*?(?:\s+[A-Z][\w'’\-]*?){0,3})" # noqa: RUF001
|
|
200
|
+
r"(?:'s|’s)\s+(?:own\s+)?words\b" # noqa: RUF001
|
|
201
|
+
)
|
|
202
|
+
|
|
203
|
+
_PER_RE = re.compile(rf"(?<![\w-])(?P<cue>per)\s+(?P<name>{_NAME})\b")
|
|
204
|
+
|
|
205
|
+
#: An attribution dash following a quotation: `> "…" — David Fox`. Requires a
|
|
206
|
+
#: quotation mark earlier on the same line so ordinary em-dashes are not read as
|
|
207
|
+
#: attributions.
|
|
208
|
+
_DASH_ATTRIB_RE = re.compile(
|
|
209
|
+
rf"[\"”][^\n]{{0,40}}?(?P<cue>—|–|--)\s*(?P<name>{_NAME})\s*$", # noqa: RUF001
|
|
210
|
+
re.M,
|
|
211
|
+
)
|
|
212
|
+
|
|
213
|
+
#: How far from the cue a quotation may sit and still be the thing attributed.
|
|
214
|
+
ANCHOR_WINDOW_CHARS = 200
|
|
215
|
+
|
|
216
|
+
#: A quotation in a *different sentence* is not what a cue attributes, however
|
|
217
|
+
#: close it sits. Without this, `"…," McCormack said. Its tooling, Fox says, …`
|
|
218
|
+
#: hands McCormack's words to Fox — inventing exactly the kind of misattribution
|
|
219
|
+
#: this module exists to surface. Abbreviations ("v.", "U.S.") make the rule
|
|
220
|
+
#: over-trigger, which downgrades a claim to REPORTED: under-claiming
|
|
221
|
+
#: checkability rather than over-claiming it, which is the right direction.
|
|
222
|
+
_SENTENCE_BREAK_RE = re.compile(r"[.!?][\"'”’)\]]*\s|\n\s*\n") # noqa: RUF001
|
|
223
|
+
|
|
224
|
+
#: Bounds the reported-speech excerpt so the inventory stays readable.
|
|
225
|
+
MAX_EXCERPT_CHARS = 300
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
class AttributionForm(StrEnum):
|
|
229
|
+
"""How the document presents the attributed material."""
|
|
230
|
+
|
|
231
|
+
ANCHORED = "anchored"
|
|
232
|
+
"""A quotation sits beside the cue. The attributed span is unambiguous, so
|
|
233
|
+
this claim is checkable against a source today."""
|
|
234
|
+
|
|
235
|
+
REPORTED = "reported"
|
|
236
|
+
"""Reported speech with no quotation marks. Delimiting what was actually
|
|
237
|
+
claimed needs judgment, so this is inventoried but not yet checkable."""
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
@dataclass(frozen=True, slots=True)
|
|
241
|
+
class AttributionClaim:
|
|
242
|
+
"""One assertion that a named party produced some content."""
|
|
243
|
+
|
|
244
|
+
speaker: str | None
|
|
245
|
+
"""The resolved name, or None when the subject is a pronoun."""
|
|
246
|
+
|
|
247
|
+
speaker_surface: str
|
|
248
|
+
"""What actually appeared in the text."""
|
|
249
|
+
|
|
250
|
+
cue: str
|
|
251
|
+
form: AttributionForm
|
|
252
|
+
content: str
|
|
253
|
+
"""The quotation when anchored; the surrounding excerpt when reported."""
|
|
254
|
+
|
|
255
|
+
line: int
|
|
256
|
+
span: tuple[int, int]
|
|
257
|
+
|
|
258
|
+
@property
|
|
259
|
+
def resolved(self) -> bool:
|
|
260
|
+
"""False when the speaker is a pronoun and needs coreference."""
|
|
261
|
+
return self.speaker is not None
|
|
262
|
+
|
|
263
|
+
@property
|
|
264
|
+
def checkable(self) -> bool:
|
|
265
|
+
"""Whether Tier 1 could adjudicate this claim as it stands."""
|
|
266
|
+
return self.form is AttributionForm.ANCHORED and self.resolved
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
def _line_of(text: str, index: int) -> int:
|
|
270
|
+
return text.count("\n", 0, index) + 1
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
def _classify_speaker(surface: str) -> tuple[str | None, bool]:
|
|
274
|
+
"""Return (resolved name, is_attribution). Rejects sentence-openers."""
|
|
275
|
+
cleaned = surface.strip()
|
|
276
|
+
lowered = cleaned.lower()
|
|
277
|
+
if lowered in _PRONOUNS:
|
|
278
|
+
return None, True
|
|
279
|
+
if lowered in _POSSESSIVES or _GERUND_RE.search(
|
|
280
|
+
cleaned.split()[-1] if cleaned.split() else ""
|
|
281
|
+
):
|
|
282
|
+
return None, False
|
|
283
|
+
first = lowered.split()[0] if lowered.split() else ""
|
|
284
|
+
# A lone sentence-opener is not a speaker. A multi-token phrase beginning
|
|
285
|
+
# with one ("The Financial Times reported") keeps the rest.
|
|
286
|
+
if first in _NOT_A_SPEAKER:
|
|
287
|
+
remainder = " ".join(cleaned.split()[1:])
|
|
288
|
+
if not remainder:
|
|
289
|
+
return None, False
|
|
290
|
+
return remainder, True
|
|
291
|
+
return cleaned, True
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
def _pronoun_takes_no_appositive(speaker: str | None) -> bool:
|
|
295
|
+
"""Whether a classified speaker is a pronoun, which takes no appositive.
|
|
296
|
+
|
|
297
|
+
Takes the speaker `_classify_speaker` already returned rather than the raw
|
|
298
|
+
surface, so the name is not classified a second time. A `None` speaker is a
|
|
299
|
+
bare pronoun; case is ignored, so `He` and `he` both count.
|
|
300
|
+
"""
|
|
301
|
+
return speaker is None or speaker.lower() in _PRONOUNS
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
def _excerpt(text: str, start: int, end: int) -> str:
|
|
305
|
+
"""The sentence-ish neighbourhood of a cue, for reported speech."""
|
|
306
|
+
left = text.rfind(".", max(0, start - MAX_EXCERPT_CHARS), start)
|
|
307
|
+
left = left + 1 if left != -1 else max(0, start - MAX_EXCERPT_CHARS)
|
|
308
|
+
right = text.find(".", end, end + MAX_EXCERPT_CHARS)
|
|
309
|
+
right = right + 1 if right != -1 else min(len(text), end + MAX_EXCERPT_CHARS)
|
|
310
|
+
return " ".join(text[left:right].split())
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
def extract_attributions(text: str) -> list[AttributionClaim]:
|
|
314
|
+
"""Every attribution claim in the document, in order, de-duplicated by span."""
|
|
315
|
+
spans = quoted_spans(text)
|
|
316
|
+
claims: dict[tuple[int, int], AttributionClaim] = {}
|
|
317
|
+
|
|
318
|
+
patterns = (
|
|
319
|
+
_DASH_ATTRIB_RE,
|
|
320
|
+
_INVERTED_RE,
|
|
321
|
+
_IN_WORDS_RE,
|
|
322
|
+
_ACCORDING_TO_RE,
|
|
323
|
+
_PER_RE,
|
|
324
|
+
_SUBJECT_FIRST_RE,
|
|
325
|
+
_SUBJECT_FIRST_COMPLEMENT_RE,
|
|
326
|
+
_APPOSITIVE_RE,
|
|
327
|
+
)
|
|
328
|
+
|
|
329
|
+
for pattern in patterns:
|
|
330
|
+
for match in pattern.finditer(text):
|
|
331
|
+
surface = match.group("name")
|
|
332
|
+
speaker, is_attribution = _classify_speaker(surface)
|
|
333
|
+
if not is_attribution:
|
|
334
|
+
continue
|
|
335
|
+
if pattern is _APPOSITIVE_RE and _pronoun_takes_no_appositive(speaker):
|
|
336
|
+
continue
|
|
337
|
+
if len(surface.split()) == 1 and _DETERMINER_BEFORE_RE.search(
|
|
338
|
+
text[: match.start("name")]
|
|
339
|
+
):
|
|
340
|
+
continue
|
|
341
|
+
|
|
342
|
+
cue_start, cue_end = match.start("cue"), match.end("cue")
|
|
343
|
+
|
|
344
|
+
nearest = _nearest_quotation(text, spans, cue_start, cue_end)
|
|
345
|
+
if nearest is not None:
|
|
346
|
+
form, content = AttributionForm.ANCHORED, nearest
|
|
347
|
+
else:
|
|
348
|
+
form, content = (
|
|
349
|
+
AttributionForm.REPORTED,
|
|
350
|
+
_excerpt(text, match.start(), match.end()),
|
|
351
|
+
)
|
|
352
|
+
|
|
353
|
+
key = (match.start(), match.end())
|
|
354
|
+
if any(s <= key[0] < e for s, e in claims):
|
|
355
|
+
continue
|
|
356
|
+
claims[key] = AttributionClaim(
|
|
357
|
+
speaker=speaker,
|
|
358
|
+
speaker_surface=surface.strip(),
|
|
359
|
+
cue=match.group("cue").lower(),
|
|
360
|
+
form=form,
|
|
361
|
+
content=content,
|
|
362
|
+
line=_line_of(text, match.start()),
|
|
363
|
+
span=key,
|
|
364
|
+
)
|
|
365
|
+
|
|
366
|
+
return [claims[k] for k in sorted(claims)]
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
def _nearest_quotation(
|
|
370
|
+
text: str, spans: list[tuple[int, int, str]], cue_start: int, cue_end: int
|
|
371
|
+
) -> str | None:
|
|
372
|
+
"""The quotation a cue attributes, if one is close enough to be that.
|
|
373
|
+
|
|
374
|
+
Close enough means: within the window, and in the same sentence. Proximity
|
|
375
|
+
alone is not enough — the sentence before ends with someone else's words.
|
|
376
|
+
"""
|
|
377
|
+
best: tuple[int, str] | None = None
|
|
378
|
+
for start, end, inner in spans:
|
|
379
|
+
if start <= cue_start and cue_end <= end:
|
|
380
|
+
continue # the cue is inside the quotation; not its attribution
|
|
381
|
+
if end <= cue_start:
|
|
382
|
+
between, distance = text[end:cue_start], cue_start - end
|
|
383
|
+
elif start >= cue_end:
|
|
384
|
+
between, distance = text[cue_end:start], start - cue_end
|
|
385
|
+
else:
|
|
386
|
+
between, distance = "", 0
|
|
387
|
+
if distance > ANCHOR_WINDOW_CHARS or _SENTENCE_BREAK_RE.search(between):
|
|
388
|
+
continue
|
|
389
|
+
if best is None or distance < best[0]:
|
|
390
|
+
best = (distance, inner)
|
|
391
|
+
return None if best is None else " ".join(best[1].split())
|
|
392
|
+
|
|
393
|
+
|
|
394
|
+
@dataclass(frozen=True, slots=True)
|
|
395
|
+
class AttributionInventory:
|
|
396
|
+
"""What a document claims about who said what."""
|
|
397
|
+
|
|
398
|
+
claims: list[AttributionClaim]
|
|
399
|
+
|
|
400
|
+
@property
|
|
401
|
+
def speakers(self) -> list[str]:
|
|
402
|
+
return sorted({c.speaker for c in self.claims if c.speaker})
|
|
403
|
+
|
|
404
|
+
@property
|
|
405
|
+
def anchored(self) -> list[AttributionClaim]:
|
|
406
|
+
return [c for c in self.claims if c.form is AttributionForm.ANCHORED]
|
|
407
|
+
|
|
408
|
+
@property
|
|
409
|
+
def reported(self) -> list[AttributionClaim]:
|
|
410
|
+
return [c for c in self.claims if c.form is AttributionForm.REPORTED]
|
|
411
|
+
|
|
412
|
+
@property
|
|
413
|
+
def unresolved(self) -> list[AttributionClaim]:
|
|
414
|
+
return [c for c in self.claims if not c.resolved]
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+
def inventory(text: str) -> AttributionInventory:
|
|
418
|
+
"""Enumerate a document's attribution surface."""
|
|
419
|
+
return AttributionInventory(claims=extract_attributions(text))
|