antihallucination 0.2.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.
- antihallucination/__init__.py +58 -0
- antihallucination/__main__.py +3 -0
- antihallucination/checks.py +509 -0
- antihallucination/cli.py +201 -0
- antihallucination/core.py +334 -0
- antihallucination/data.py +199 -0
- antihallucination/errors.py +5 -0
- antihallucination/facts.py +227 -0
- antihallucination/lexicon.py +113 -0
- antihallucination/mcp_server.py +266 -0
- antihallucination/numbers.py +312 -0
- antihallucination/prompt.py +176 -0
- antihallucination/py.typed +0 -0
- antihallucination/receipt.py +145 -0
- antihallucination/text.py +116 -0
- antihallucination/writers/__init__.py +12 -0
- antihallucination/writers/claude.py +90 -0
- antihallucination/writers/openai_compatible.py +128 -0
- antihallucination/writing.py +116 -0
- antihallucination-0.2.0.dist-info/METADATA +249 -0
- antihallucination-0.2.0.dist-info/RECORD +25 -0
- antihallucination-0.2.0.dist-info/WHEEL +5 -0
- antihallucination-0.2.0.dist-info/entry_points.txt +3 -0
- antihallucination-0.2.0.dist-info/licenses/LICENSE +21 -0
- antihallucination-0.2.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"""Numbers decide; the AI only writes.
|
|
2
|
+
|
|
3
|
+
Your code computes the facts and decides the verdicts. A writer (any LLM, or a
|
|
4
|
+
person) puts them into words. This package fills in every figure from your
|
|
5
|
+
facts, checks every sentence against your facts, labels and sources, publishes
|
|
6
|
+
only what passes and keeps a receipt of each decision.
|
|
7
|
+
|
|
8
|
+
from antihallucination import Fact, compose
|
|
9
|
+
|
|
10
|
+
facts = [Fact("ventas", 1250, unit="unidades", subject="ventas"),
|
|
11
|
+
Fact("variacion", -3.2, unit="%", change=True, subject="ventas")]
|
|
12
|
+
result = compose("Las ventas cayeron {variacion:abs} hasta {ventas}.", facts, locale="es")
|
|
13
|
+
result.text # 'Las ventas cayeron 3,2 % hasta 1.250 unidades.'
|
|
14
|
+
|
|
15
|
+
write() asks a model for the draft (antihallucination.writers has Claude and
|
|
16
|
+
OpenAI-compatible writers; any function (system, prompt) -> str works) and
|
|
17
|
+
sends it through compose(). The model is told the placeholders, never a value.
|
|
18
|
+
The command line is python -m antihallucination.
|
|
19
|
+
|
|
20
|
+
What it can show: every figure in the text came from your facts (compose) or
|
|
21
|
+
matches them (verify), and every name is in your evidence. What it cannot:
|
|
22
|
+
that your facts are right, or that no sentence claims more than the lists in
|
|
23
|
+
lexicon.py know how to catch.
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
__version__ = "0.2.0"
|
|
27
|
+
|
|
28
|
+
from .checks import Finding, Policy
|
|
29
|
+
from .errors import WriterError
|
|
30
|
+
from .core import Decision, ReplayCheck, ReplayStatus, Result, compose, replay, verify
|
|
31
|
+
from .facts import Fact, Label, Source, classify
|
|
32
|
+
from .receipt import RECIPE, Receipt, ReceiptCheck, ReceiptStatus, verify_receipt
|
|
33
|
+
from .writing import Written, write
|
|
34
|
+
|
|
35
|
+
__all__ = [
|
|
36
|
+
"RECIPE",
|
|
37
|
+
"__version__",
|
|
38
|
+
"Decision",
|
|
39
|
+
"Fact",
|
|
40
|
+
"Finding",
|
|
41
|
+
"Label",
|
|
42
|
+
"Policy",
|
|
43
|
+
"Receipt",
|
|
44
|
+
"ReceiptCheck",
|
|
45
|
+
"ReceiptStatus",
|
|
46
|
+
"ReplayCheck",
|
|
47
|
+
"ReplayStatus",
|
|
48
|
+
"Result",
|
|
49
|
+
"Source",
|
|
50
|
+
"Written",
|
|
51
|
+
"WriterError",
|
|
52
|
+
"classify",
|
|
53
|
+
"compose",
|
|
54
|
+
"replay",
|
|
55
|
+
"verify",
|
|
56
|
+
"verify_receipt",
|
|
57
|
+
"write",
|
|
58
|
+
]
|
|
@@ -0,0 +1,509 @@
|
|
|
1
|
+
"""The checks one sentence goes through, and what each one found.
|
|
2
|
+
|
|
3
|
+
A sentence passes only if every check passes; one finding is enough to keep it
|
|
4
|
+
out. The checks, in the order they run:
|
|
5
|
+
|
|
6
|
+
1. Figures. In compose mode the writer may not type a figure at all: it writes
|
|
7
|
+
{placeholders} and Python writes the numbers. A sentence that cites a source
|
|
8
|
+
may copy a figure from that source. In verify mode every figure must be a
|
|
9
|
+
fact rounded to the precision written, a figure inside a label, or a figure
|
|
10
|
+
in the sources the sentence may use.
|
|
11
|
+
2. Signs. A negative value written without its minus sign needs a word that
|
|
12
|
+
says it fell («cayeron 3,2 %»).
|
|
13
|
+
3. Directions. A word that says something went up or down must agree with the
|
|
14
|
+
sign of the change it talks about, and a move nothing backs is a claim.
|
|
15
|
+
4. Names. A proper name must be in your facts, labels, sources or known names.
|
|
16
|
+
5. Claims. A forecast, a claim of certainty or a cause is a claim the numbers
|
|
17
|
+
do not make (lexicon.py says how far those lists go).
|
|
18
|
+
6. Anchoring. A sentence that passed everything else must still carry
|
|
19
|
+
something from your data, or cite a source. It needs no word list: a
|
|
20
|
+
sentence with no data is the writer's opinion, however it is phrased.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
import re
|
|
24
|
+
from dataclasses import dataclass, field
|
|
25
|
+
from decimal import Decimal
|
|
26
|
+
|
|
27
|
+
from . import lexicon
|
|
28
|
+
from .facts import Fact, Label, Source
|
|
29
|
+
from .numbers import Mention, backed_by_values, evidence_values, extract, match_fact
|
|
30
|
+
from .text import MASK, clause_of, fold, lower, mask, words_before
|
|
31
|
+
|
|
32
|
+
PLACEHOLDER_RE = re.compile(r"\{([A-Za-z_][A-Za-z0-9_]*)(?::([A-Za-z_]+))?\}")
|
|
33
|
+
CITATION_RE = re.compile(r"\[([A-Za-z0-9_.\-]+(?:\s*,\s*[A-Za-z0-9_.\-]+)*)\]")
|
|
34
|
+
MODIFIERS = frozenset({"abs", "signed"})
|
|
35
|
+
_WORD_RE = re.compile(r"[^\W\d_][\w'’.\-&]*")
|
|
36
|
+
_EDGE = ".,;:-'’\"«»“”()¿?¡!"
|
|
37
|
+
_OPENERS = ":¿¡«\"“(—–-"
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
@dataclass(frozen=True)
|
|
41
|
+
class Policy:
|
|
42
|
+
"""Which checks run, and the names you allow anywhere.
|
|
43
|
+
|
|
44
|
+
Every check is on by default. known_names holds names no source has to
|
|
45
|
+
carry: your company, your products, the people who sign the report.
|
|
46
|
+
"""
|
|
47
|
+
|
|
48
|
+
numbers: bool = True
|
|
49
|
+
names: bool = True
|
|
50
|
+
directions: bool = True
|
|
51
|
+
forecasts: bool = True
|
|
52
|
+
certainty: bool = True
|
|
53
|
+
causes: bool = True
|
|
54
|
+
# Every sentence must carry something from your data (a figure, a verdict,
|
|
55
|
+
# a direction checked against a change) or cite a source. Not a word list:
|
|
56
|
+
# a sentence with no data is cut however it is phrased.
|
|
57
|
+
anchored: bool = True
|
|
58
|
+
known_names: tuple[str, ...] = ()
|
|
59
|
+
|
|
60
|
+
def __post_init__(self) -> None:
|
|
61
|
+
names = (self.known_names,) if isinstance(self.known_names, str) else tuple(self.known_names)
|
|
62
|
+
if not all(isinstance(name, str) and name.strip() for name in names):
|
|
63
|
+
raise ValueError("known_names must be non-empty text")
|
|
64
|
+
object.__setattr__(self, "known_names", tuple(name.strip() for name in names))
|
|
65
|
+
|
|
66
|
+
def to_dict(self) -> dict[str, object]:
|
|
67
|
+
return {
|
|
68
|
+
"numbers": self.numbers,
|
|
69
|
+
"names": self.names,
|
|
70
|
+
"directions": self.directions,
|
|
71
|
+
"forecasts": self.forecasts,
|
|
72
|
+
"certainty": self.certainty,
|
|
73
|
+
"causes": self.causes,
|
|
74
|
+
"anchored": self.anchored,
|
|
75
|
+
"known_names": list(self.known_names),
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
@dataclass(frozen=True)
|
|
80
|
+
class Finding:
|
|
81
|
+
"""One reason a sentence did not go out.
|
|
82
|
+
|
|
83
|
+
check is a stable code (unbacked_number, typed_number, unknown_placeholder,
|
|
84
|
+
sign_dropped, direction_contradiction, unbacked_direction, unbacked_name,
|
|
85
|
+
forecast, certainty, cause, unanchored); span is the text as written;
|
|
86
|
+
detail says why.
|
|
87
|
+
"""
|
|
88
|
+
|
|
89
|
+
check: str
|
|
90
|
+
span: str
|
|
91
|
+
detail: str
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
@dataclass(frozen=True)
|
|
95
|
+
class _Reference:
|
|
96
|
+
fact: str
|
|
97
|
+
start: int
|
|
98
|
+
span: str # the figure or placeholder as written
|
|
99
|
+
how: str # "exact" or "abs": whether the figure carries the fact's sign
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
@dataclass
|
|
103
|
+
class SentenceCheck:
|
|
104
|
+
findings: list[Finding] = field(default_factory=list)
|
|
105
|
+
facts: list[str] = field(default_factory=list)
|
|
106
|
+
labels: list[str] = field(default_factory=list)
|
|
107
|
+
cited: list[str] = field(default_factory=list)
|
|
108
|
+
subjects: list[str] = field(default_factory=list)
|
|
109
|
+
output: str = ""
|
|
110
|
+
anchors: int = 0 # pieces of your data the sentence carries, for the anchoring check
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def _contains_word(haystack: str, word: str) -> bool:
|
|
114
|
+
"""`word` (folded) in `haystack` (folded) as a whole word, plural or singular."""
|
|
115
|
+
if not word:
|
|
116
|
+
return False
|
|
117
|
+
candidates = {word}
|
|
118
|
+
if word.endswith("es") and len(word) > 4:
|
|
119
|
+
candidates.add(word[:-2])
|
|
120
|
+
if word.endswith("s") and len(word) > 3:
|
|
121
|
+
candidates.add(word[:-1])
|
|
122
|
+
for candidate in candidates:
|
|
123
|
+
if re.search(r"(?<![a-z0-9])" + re.escape(candidate) + r"(?:s|es)?(?![a-z0-9])", haystack):
|
|
124
|
+
return True
|
|
125
|
+
if "." in word:
|
|
126
|
+
return _contains_word(haystack, word.replace(".", ""))
|
|
127
|
+
return False
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
class Context:
|
|
131
|
+
"""Your facts, labels, sources and policy, read once for every sentence."""
|
|
132
|
+
|
|
133
|
+
def __init__(
|
|
134
|
+
self,
|
|
135
|
+
facts: list[Fact],
|
|
136
|
+
labels: list[Label],
|
|
137
|
+
sources: list[Source],
|
|
138
|
+
policy: Policy,
|
|
139
|
+
locale: str,
|
|
140
|
+
) -> None:
|
|
141
|
+
self.facts: dict[str, Fact] = {}
|
|
142
|
+
self.labels: dict[str, Label] = {}
|
|
143
|
+
for fact in facts:
|
|
144
|
+
if not isinstance(fact, Fact):
|
|
145
|
+
raise TypeError("facts must be Fact objects")
|
|
146
|
+
if fact.name in self.facts:
|
|
147
|
+
raise ValueError(f"two facts are named {fact.name!r}")
|
|
148
|
+
self.facts[fact.name] = fact
|
|
149
|
+
for label in labels:
|
|
150
|
+
if not isinstance(label, Label):
|
|
151
|
+
raise TypeError("labels must be Label objects")
|
|
152
|
+
if label.name in self.facts or label.name in self.labels:
|
|
153
|
+
raise ValueError(f"a fact or label is already named {label.name!r}")
|
|
154
|
+
self.labels[label.name] = label
|
|
155
|
+
self.sources: dict[str, Source] = {}
|
|
156
|
+
for source in sources:
|
|
157
|
+
if source.id in self.sources:
|
|
158
|
+
raise ValueError(f"two sources have the id {source.id!r}")
|
|
159
|
+
self.sources[source.id] = source
|
|
160
|
+
self.policy = policy
|
|
161
|
+
self.locale = locale
|
|
162
|
+
|
|
163
|
+
base = [*policy.known_names, *(label.text for label in labels)]
|
|
164
|
+
for fact in facts:
|
|
165
|
+
base.extend([fact.name.replace("_", " "), fact.label, fact.unit, *fact.subjects])
|
|
166
|
+
self.base_evidence = fold("\n".join(base))
|
|
167
|
+
# Evidence is read both ways: a source may be in another language than the text.
|
|
168
|
+
self.label_values = evidence_values("\n".join(label.text for label in labels))
|
|
169
|
+
self.source_evidence = {sid: fold(source.evidence) for sid, source in self.sources.items()}
|
|
170
|
+
self.source_values = {sid: evidence_values(source.evidence) for sid, source in self.sources.items()}
|
|
171
|
+
self.source_lower = {sid: lower(source.evidence) for sid, source in self.sources.items()}
|
|
172
|
+
|
|
173
|
+
def evidence_for(self, cited: list[str]) -> list[str]:
|
|
174
|
+
"""The sources a sentence may draw on: the ones it cites, or all of them."""
|
|
175
|
+
return cited if cited else list(self.sources)
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def _shown(fact: Fact, ctx: Context) -> str:
|
|
179
|
+
"""A fact as Python writes it, for a finding a person will read."""
|
|
180
|
+
return fact.render(ctx.locale if ctx.locale in ("es", "en") else "en")
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def _render_placeholder(match: re.Match[str], ctx: Context) -> str:
|
|
184
|
+
name, modifier = match.group(1), match.group(2) or ""
|
|
185
|
+
if name in ctx.facts:
|
|
186
|
+
return ctx.facts[name].render(ctx.locale, modifier)
|
|
187
|
+
if name in ctx.labels:
|
|
188
|
+
return ctx.labels[name].text
|
|
189
|
+
return match.group(0)
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
def render_template(template: str, ctx: Context) -> str:
|
|
193
|
+
"""A text Python writes, with every placeholder filled. Not checked: it is yours."""
|
|
194
|
+
return PLACEHOLDER_RE.sub(lambda match: _render_placeholder(match, ctx), template)
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
def template_problems(template: str, ctx: Context) -> list[str]:
|
|
198
|
+
"""What is wrong with the placeholders of a template you wrote."""
|
|
199
|
+
problems: list[str] = []
|
|
200
|
+
for match in PLACEHOLDER_RE.finditer(template):
|
|
201
|
+
name, modifier = match.group(1), match.group(2)
|
|
202
|
+
if name not in ctx.facts and name not in ctx.labels:
|
|
203
|
+
problems.append(f"{{{name}}} names no fact or label")
|
|
204
|
+
elif modifier and (name in ctx.labels or modifier not in MODIFIERS):
|
|
205
|
+
problems.append(f"{match.group(0)} has a modifier it cannot take")
|
|
206
|
+
return problems
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
def _identifier_backed(raw: str, evidence: str) -> bool:
|
|
210
|
+
return re.search(r"(?<![\w-])" + re.escape(fold(raw)) + r"(?![\w-])", evidence) is not None
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
def _negated(text: str, clause_start: int, position: int) -> bool:
|
|
214
|
+
"""Whether a negator in the three words before `position` (same clause) turns the word around."""
|
|
215
|
+
words = words_before(text[clause_start:position], position - clause_start, 3)
|
|
216
|
+
for index, word in enumerate(words):
|
|
217
|
+
if word in lexicon.NEGATORS:
|
|
218
|
+
following = words[index + 1] if index + 1 < len(words) else ""
|
|
219
|
+
if following not in lexicon.NOT_A_NEGATION:
|
|
220
|
+
return True
|
|
221
|
+
return False
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
def _agrees(polarity: str, negated: bool, value: Decimal) -> bool:
|
|
225
|
+
if polarity == "up":
|
|
226
|
+
return value <= 0 if negated else value > 0
|
|
227
|
+
return value >= 0 if negated else value < 0
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
def _subject_facts(window: str, ctx: Context, *, change_only: bool) -> list[str]:
|
|
231
|
+
folded = fold(window)
|
|
232
|
+
found: list[str] = []
|
|
233
|
+
for fact in ctx.facts.values():
|
|
234
|
+
if change_only and not fact.change:
|
|
235
|
+
continue
|
|
236
|
+
if any(_contains_word(folded, fold(word)) for word in fact.subjects):
|
|
237
|
+
found.append(fact.name)
|
|
238
|
+
return found
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
def _carries_minus(lowered: str, clause: tuple[int, int]) -> bool:
|
|
242
|
+
"""Whether a word in the clause says the value is down or negative."""
|
|
243
|
+
start, end = clause
|
|
244
|
+
segment = lowered[start:end]
|
|
245
|
+
for match in lexicon.DOWN.finditer(segment):
|
|
246
|
+
if not _negated(lowered, start, start + match.start()):
|
|
247
|
+
return True
|
|
248
|
+
return lexicon.NEGATIVE_NOUNS.search(segment) is not None
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
def _name_candidates(text: str) -> list[str]:
|
|
252
|
+
"""Capitalized words that are not just the first word of a sentence."""
|
|
253
|
+
candidates: list[str] = []
|
|
254
|
+
for match in _WORD_RE.finditer(text):
|
|
255
|
+
token = match.group(0).rstrip(".")
|
|
256
|
+
if any(ch.isdigit() for ch in token) or not any(ch.isupper() for ch in token):
|
|
257
|
+
continue
|
|
258
|
+
before = text[: match.start()].rstrip(" " + MASK)
|
|
259
|
+
initial = not before or before[-1] in _OPENERS
|
|
260
|
+
letters = [ch for ch in token if ch.isalpha()]
|
|
261
|
+
acronym = len(letters) >= 2 and all(ch.isupper() for ch in letters)
|
|
262
|
+
camel = any(ch.isupper() for ch in token[1:]) and not acronym
|
|
263
|
+
if initial and not (acronym or camel):
|
|
264
|
+
continue
|
|
265
|
+
candidates.append(token.strip(_EDGE))
|
|
266
|
+
return candidates
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
def _stray_numerals(masked: str, mentions: list[Mention]) -> list[str]:
|
|
270
|
+
"""Numeral characters no figure accounts for: «²», «½», «Ⅲ», a digit after «_».
|
|
271
|
+
|
|
272
|
+
compose() promises that the writer typed no digit, so anything numeric the
|
|
273
|
+
figure reader did not see still counts as typed.
|
|
274
|
+
"""
|
|
275
|
+
covered = [False] * len(masked)
|
|
276
|
+
for mention in mentions:
|
|
277
|
+
for index in range(mention.start, mention.end):
|
|
278
|
+
covered[index] = True
|
|
279
|
+
runs: list[str] = []
|
|
280
|
+
run = ""
|
|
281
|
+
for index, ch in enumerate(masked):
|
|
282
|
+
if ch.isnumeric() and not covered[index]:
|
|
283
|
+
run += ch
|
|
284
|
+
elif run:
|
|
285
|
+
runs.append(run)
|
|
286
|
+
run = ""
|
|
287
|
+
if run:
|
|
288
|
+
runs.append(run)
|
|
289
|
+
return runs
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
def _check_figures(masked: str, ctx: Context, check: SentenceCheck, refs: list[_Reference], *, compose: bool) -> None:
|
|
293
|
+
sources = ctx.evidence_for(check.cited)
|
|
294
|
+
mentions = extract(masked, ctx.locale)
|
|
295
|
+
if compose:
|
|
296
|
+
for run in _stray_numerals(masked, mentions):
|
|
297
|
+
check.findings.append(
|
|
298
|
+
Finding("typed_number", run, f"«{run}» was typed by the writer; figures come from placeholders")
|
|
299
|
+
)
|
|
300
|
+
for mention in mentions:
|
|
301
|
+
if compose and not check.cited:
|
|
302
|
+
check.findings.append(
|
|
303
|
+
Finding("typed_number", mention.raw, f"«{mention.raw}» was typed by the writer; figures come from placeholders")
|
|
304
|
+
)
|
|
305
|
+
continue
|
|
306
|
+
if not compose and not mention.identifier:
|
|
307
|
+
matched = _match_facts(mention, ctx)
|
|
308
|
+
if matched is not None:
|
|
309
|
+
how, names = matched
|
|
310
|
+
if how == "sign":
|
|
311
|
+
fact = ctx.facts[names[0]]
|
|
312
|
+
check.findings.append(
|
|
313
|
+
Finding(
|
|
314
|
+
"direction_contradiction",
|
|
315
|
+
mention.raw,
|
|
316
|
+
f"«{mention.raw}» has the opposite sign of {fact.name} ({_shown(fact, ctx)})",
|
|
317
|
+
)
|
|
318
|
+
)
|
|
319
|
+
else:
|
|
320
|
+
refs.extend(_Reference(name, mention.start, mention.raw, how) for name in names)
|
|
321
|
+
continue
|
|
322
|
+
if _backed_elsewhere(mention, ctx, sources, use_labels=not compose):
|
|
323
|
+
check.anchors += 1
|
|
324
|
+
continue
|
|
325
|
+
where = "the cited source" if check.cited else "your facts, labels or sources"
|
|
326
|
+
check.findings.append(Finding("unbacked_number", mention.raw, f"«{mention.raw}» is not in {where}"))
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
def _match_facts(mention: Mention, ctx: Context) -> tuple[str, list[str]] | None:
|
|
330
|
+
"""The facts a figure stands for: exact matches first, then sign-dropped, then opposite sign."""
|
|
331
|
+
by_kind: dict[str, list[str]] = {"exact": [], "abs": [], "sign": []}
|
|
332
|
+
for fact in ctx.facts.values():
|
|
333
|
+
how = match_fact(mention, fact.number, fact.is_percent)
|
|
334
|
+
if how is not None:
|
|
335
|
+
by_kind[how].append(fact.name)
|
|
336
|
+
for kind in ("exact", "abs", "sign"):
|
|
337
|
+
if by_kind[kind]:
|
|
338
|
+
return kind, by_kind[kind]
|
|
339
|
+
return None
|
|
340
|
+
|
|
341
|
+
|
|
342
|
+
def _backed_elsewhere(mention: Mention, ctx: Context, sources: list[str], *, use_labels: bool) -> bool:
|
|
343
|
+
if mention.identifier:
|
|
344
|
+
evidence = [ctx.base_evidence, *(ctx.source_evidence[sid] for sid in sources)]
|
|
345
|
+
return any(_identifier_backed(mention.raw, text) for text in evidence)
|
|
346
|
+
if use_labels and backed_by_values(mention, ctx.label_values):
|
|
347
|
+
return True
|
|
348
|
+
return any(backed_by_values(mention, ctx.source_values[sid]) for sid in sources)
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
def _check_directions(masked: str, ctx: Context, check: SentenceCheck, refs: list[_Reference]) -> None:
|
|
352
|
+
lowered = lower(masked)
|
|
353
|
+
for polarity, pattern in (("up", lexicon.UP), ("down", lexicon.DOWN)):
|
|
354
|
+
for match in pattern.finditer(lowered):
|
|
355
|
+
clause = clause_of(masked, match.start())
|
|
356
|
+
negated = _negated(lowered, clause[0], match.start())
|
|
357
|
+
word = masked[match.start() : match.end()]
|
|
358
|
+
near = [r for r in refs if clause[0] <= r.start < clause[1] and ctx.facts[r.fact].change]
|
|
359
|
+
if near:
|
|
360
|
+
check.anchors += 1
|
|
361
|
+
fact = ctx.facts[min(near, key=lambda r: abs(r.start - match.start())).fact]
|
|
362
|
+
if not _agrees(polarity, negated, fact.number):
|
|
363
|
+
check.findings.append(_contradiction(word, polarity, negated, fact, ctx))
|
|
364
|
+
continue
|
|
365
|
+
# Which change the word talks about: one whose subject the clause
|
|
366
|
+
# names; if the clause names a subject with no change («el ticket
|
|
367
|
+
# subió»), nothing backs the move; if it names none, the sentence's.
|
|
368
|
+
clause_text = masked[clause[0] : clause[1]]
|
|
369
|
+
facts = [ctx.facts[name] for name in _subject_facts(clause_text, ctx, change_only=True)]
|
|
370
|
+
if not facts and not _subject_facts(clause_text, ctx, change_only=False):
|
|
371
|
+
facts = [ctx.facts[name] for name in _subject_facts(masked, ctx, change_only=True)]
|
|
372
|
+
if facts:
|
|
373
|
+
check.anchors += 1
|
|
374
|
+
# Two changes of that subject pointing different ways get no verdict.
|
|
375
|
+
directions = {(fact.number > 0) - (fact.number < 0) for fact in facts}
|
|
376
|
+
if len(directions) == 1 and not _agrees(polarity, negated, facts[0].number):
|
|
377
|
+
check.findings.append(_contradiction(word, polarity, negated, facts[0], ctx))
|
|
378
|
+
continue
|
|
379
|
+
if any(pattern.search(ctx.source_lower[sid]) for sid in ctx.evidence_for(check.cited)):
|
|
380
|
+
continue
|
|
381
|
+
check.findings.append(
|
|
382
|
+
Finding("unbacked_direction", word, f"«{word}» claims a move that no change fact or source backs")
|
|
383
|
+
)
|
|
384
|
+
|
|
385
|
+
|
|
386
|
+
def _anchored(check: SentenceCheck, refs: list[_Reference]) -> bool:
|
|
387
|
+
"""Whether the sentence carries something from your data, or cites a source.
|
|
388
|
+
|
|
389
|
+
Your data is a fact (a placeholder, or a figure that matches one), a label
|
|
390
|
+
(its placeholder, or its words as you wrote them), a figure a label or a
|
|
391
|
+
source backs, or a move checked against a change. «El desempeño se
|
|
392
|
+
mantiene dentro de lo esperado» carries none of them: it is the writer's
|
|
393
|
+
opinion, and it is cut whatever its words are.
|
|
394
|
+
"""
|
|
395
|
+
return bool(refs or check.labels or check.cited or check.anchors)
|
|
396
|
+
|
|
397
|
+
|
|
398
|
+
def _contradiction(word: str, polarity: str, negated: bool, fact: Fact, ctx: Context) -> Finding:
|
|
399
|
+
said = ("not " if negated else "") + ("up" if polarity == "up" else "down")
|
|
400
|
+
return Finding("direction_contradiction", word, f"«{word}» says {said}, but {fact.name} is {_shown(fact, ctx)}")
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
def check_sentence(sentence: str, ctx: Context, *, compose: bool) -> SentenceCheck:
|
|
404
|
+
"""Run every check on one sentence and render what would go out."""
|
|
405
|
+
check = SentenceCheck()
|
|
406
|
+
hidden: list[tuple[int, int]] = []
|
|
407
|
+
|
|
408
|
+
for match in CITATION_RE.finditer(sentence):
|
|
409
|
+
ids = [part.strip() for part in match.group(1).split(",")]
|
|
410
|
+
if all(sid in ctx.sources for sid in ids):
|
|
411
|
+
check.cited.extend(sid for sid in ids if sid not in check.cited)
|
|
412
|
+
hidden.append(match.span())
|
|
413
|
+
|
|
414
|
+
refs: list[_Reference] = []
|
|
415
|
+
verdicts: list[tuple[int, str]] = [] # where each of your labels sits: (start, label name)
|
|
416
|
+
if compose:
|
|
417
|
+
for match in PLACEHOLDER_RE.finditer(sentence):
|
|
418
|
+
hidden.append(match.span())
|
|
419
|
+
name, modifier = match.group(1), match.group(2)
|
|
420
|
+
if name in ctx.facts and (modifier is None or modifier in MODIFIERS):
|
|
421
|
+
refs.append(_Reference(name, match.start(), match.group(0), "abs" if modifier == "abs" else "exact"))
|
|
422
|
+
elif name in ctx.labels and modifier is None:
|
|
423
|
+
verdicts.append((match.start(), name))
|
|
424
|
+
if name not in check.labels:
|
|
425
|
+
check.labels.append(name)
|
|
426
|
+
else:
|
|
427
|
+
check.findings.append(
|
|
428
|
+
Finding("unknown_placeholder", match.group(0), f"{match.group(0)} names no fact or label it can fill")
|
|
429
|
+
)
|
|
430
|
+
else:
|
|
431
|
+
# A verified text may quote your verdicts as you wrote them («una caída
|
|
432
|
+
# leve»). Those words are yours, as a {label} is in compose: they carry
|
|
433
|
+
# data and are not checked again as the writer's (the «caída» in them
|
|
434
|
+
# is not a move the writer claimed).
|
|
435
|
+
lowered_sentence = lower(sentence)
|
|
436
|
+
for label in ctx.labels.values():
|
|
437
|
+
phrase = lower(label.text.strip())
|
|
438
|
+
for match in re.finditer(r"(?<!\w)" + re.escape(phrase) + r"(?!\w)", lowered_sentence):
|
|
439
|
+
hidden.append(match.span())
|
|
440
|
+
verdicts.append((match.start(), label.name))
|
|
441
|
+
check.anchors += 1
|
|
442
|
+
if label.name not in check.labels:
|
|
443
|
+
check.labels.append(label.name)
|
|
444
|
+
|
|
445
|
+
masked = mask(sentence, hidden)
|
|
446
|
+
if ctx.policy.numbers:
|
|
447
|
+
_check_figures(masked, ctx, check, refs, compose=compose)
|
|
448
|
+
|
|
449
|
+
lowered = lower(masked)
|
|
450
|
+
for ref in refs:
|
|
451
|
+
fact = ctx.facts[ref.fact]
|
|
452
|
+
clause = clause_of(masked, ref.start)
|
|
453
|
+
# Your verdict can carry the minus: «una {tendencia} de {variacion:abs}»
|
|
454
|
+
# reads «una caída leve de 3,2 %», with no second word for the fall.
|
|
455
|
+
verdict_says_it = any(
|
|
456
|
+
clause[0] <= start < clause[1] and _carries_minus(lower(ctx.labels[name].text), (0, len(ctx.labels[name].text)))
|
|
457
|
+
for start, name in verdicts
|
|
458
|
+
)
|
|
459
|
+
if ref.how == "abs" and fact.number < 0 and not (_carries_minus(lowered, clause) or verdict_says_it):
|
|
460
|
+
check.findings.append(
|
|
461
|
+
Finding(
|
|
462
|
+
"sign_dropped",
|
|
463
|
+
ref.span,
|
|
464
|
+
f"{fact.name} is {_shown(fact, ctx)}, written without its minus sign and no word says it fell",
|
|
465
|
+
)
|
|
466
|
+
)
|
|
467
|
+
|
|
468
|
+
if ctx.policy.directions:
|
|
469
|
+
_check_directions(masked, ctx, check, refs)
|
|
470
|
+
|
|
471
|
+
if ctx.policy.names:
|
|
472
|
+
evidence = [ctx.base_evidence, *(ctx.source_evidence[sid] for sid in ctx.evidence_for(check.cited))]
|
|
473
|
+
for name in _name_candidates(masked):
|
|
474
|
+
core = fold(name).strip(_EDGE)
|
|
475
|
+
if not core or core in lexicon.NOT_NAMES:
|
|
476
|
+
continue
|
|
477
|
+
if not any(_contains_word(text, core) for text in evidence):
|
|
478
|
+
check.findings.append(
|
|
479
|
+
Finding("unbacked_name", name, f"«{name}» is not in your facts, labels, sources or known names")
|
|
480
|
+
)
|
|
481
|
+
|
|
482
|
+
for enabled, code, pattern, why in (
|
|
483
|
+
(ctx.policy.forecasts, "forecast", lexicon.FORECAST, "forecasts; the text may only describe the data"),
|
|
484
|
+
(ctx.policy.certainty, "certainty", lexicon.CERTAINTY, "claims a certainty the numbers do not carry"),
|
|
485
|
+
(ctx.policy.causes, "cause", lexicon.CAUSE, "states a cause your data does not record"),
|
|
486
|
+
):
|
|
487
|
+
if enabled:
|
|
488
|
+
for match in pattern.finditer(lowered):
|
|
489
|
+
span = masked[match.start() : match.end()]
|
|
490
|
+
check.findings.append(Finding(code, span, f"«{span}» {why}"))
|
|
491
|
+
|
|
492
|
+
# Anchoring: only reported for a sentence nothing else failed, where it is
|
|
493
|
+
# the one reason; a sentence already out for a typed figure needs no second.
|
|
494
|
+
if ctx.policy.anchored and not check.findings and not _anchored(check, refs):
|
|
495
|
+
check.findings.append(
|
|
496
|
+
Finding("unanchored", sentence, "it carries none of your facts or labels and cites no source")
|
|
497
|
+
)
|
|
498
|
+
|
|
499
|
+
for ref in refs:
|
|
500
|
+
if ref.fact not in check.facts:
|
|
501
|
+
check.facts.append(ref.fact)
|
|
502
|
+
subjects: list[str] = []
|
|
503
|
+
for name in [*check.facts, *_subject_facts(masked, ctx, change_only=False)]:
|
|
504
|
+
for word in ctx.facts[name].subjects:
|
|
505
|
+
if word not in subjects:
|
|
506
|
+
subjects.append(word)
|
|
507
|
+
check.subjects = subjects
|
|
508
|
+
check.output = render_template(sentence, ctx) if compose else sentence
|
|
509
|
+
return check
|