traceground 0.1.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.
- traceground/__init__.py +26 -0
- traceground/__main__.py +6 -0
- traceground/api.py +187 -0
- traceground/binding.py +700 -0
- traceground/citations.py +39 -0
- traceground/cli.py +93 -0
- traceground/numbers.py +596 -0
- traceground/py.typed +0 -0
- traceground/quotes.py +238 -0
- traceground/result.py +55 -0
- traceground/selftalk.py +124 -0
- traceground/textnorm.py +139 -0
- traceground/toolchecks.py +130 -0
- traceground/traces.py +252 -0
- traceground-0.1.0.dist-info/METADATA +271 -0
- traceground-0.1.0.dist-info/RECORD +19 -0
- traceground-0.1.0.dist-info/WHEEL +4 -0
- traceground-0.1.0.dist-info/entry_points.txt +2 -0
- traceground-0.1.0.dist-info/licenses/LICENSE +21 -0
traceground/binding.py
ADDED
|
@@ -0,0 +1,700 @@
|
|
|
1
|
+
"""Binding checks: is each value attached to the right thing?
|
|
2
|
+
|
|
3
|
+
value-to-field A number the answer attaches to a field name ("concurrence = 0.7", "7 rebounds",
|
|
4
|
+
"a 4% chance of rain") must equal the value some tool result gives for a field of that
|
|
5
|
+
name. Field names come from the JSON keys: "rain_chance_pct" is the words "rain chance"
|
|
6
|
+
(unit suffixes such as _pct, _c, _kmh dropped), matched in any order; "monthly" matches
|
|
7
|
+
"a month". Extra names: field_synonyms={"rain_chance_pct": ["precipitation"]}.
|
|
8
|
+
Only results are checked; arguments with the same name count as support.
|
|
9
|
+
|
|
10
|
+
argument The values the question gives must be the values the tools were called with:
|
|
11
|
+
* a question number no call used, where a call used the same digits shifted by a power
|
|
12
|
+
of ten (asked p = 0.3, called with 0.03: the Kreout case);
|
|
13
|
+
* a question number whose neighbouring word names an argument ("5-year" and "years")
|
|
14
|
+
that was called with another value (asked 5 years, called with years=3);
|
|
15
|
+
* two values swapped between two calls (asked 120 USD and 80 GBP, called with
|
|
16
|
+
amount=80 and "USD", amount=120 and "GBP").
|
|
17
|
+
|
|
18
|
+
When a binding is unsure, nothing is reported: no field word right next to the number (a preposition
|
|
19
|
+
such as "for" or "on" starts a new phrase), a number that matches any of the fields its words name, a sentence
|
|
20
|
+
with "respectively", or a number written in another unit than the field's ("91.4 °F" for high_c).
|
|
21
|
+
A unit can also name the field: "weighs 249 kg" is compared with the results' only kg field, and
|
|
22
|
+
reported when 249 is another field of the same call. Also bound: a lone word that only one multi-word
|
|
23
|
+
field name has ("€789.52 in interest" for total_interest), the subject of "... is N" ("The total
|
|
24
|
+
interest you'll pay on the loan is $3,034.15"), and the two ends of a range ("between $69.80 and
|
|
25
|
+
$72.90" for day_low and day_high). A value another call worked out from the field (a calculator's
|
|
26
|
+
39.9 * 3) is not compared with the field, nor is a number whose clause says it is worked out ("the
|
|
27
|
+
difference in monthly payment is $166.03"). A field's exact value written in another currency, with no
|
|
28
|
+
call that converted it, is reported ("€221.4 million" for net_income_musd). Values are compared as
|
|
29
|
+
magnitudes, like numbers ("down 1.2%" for a change of -1.2).
|
|
30
|
+
"""
|
|
31
|
+
import re
|
|
32
|
+
|
|
33
|
+
from traceground import numbers as num
|
|
34
|
+
from traceground.result import Finding
|
|
35
|
+
from traceground.traces import numeric_fields
|
|
36
|
+
|
|
37
|
+
_UNIT_WORDS = {"c", "f", "k", "pct", "percent", "perc", "kmh", "kph", "mph", "mps", "ms", "s", "sec", "secs", "min",
|
|
38
|
+
"mins", "h", "hr", "hrs", "kg", "g", "lb", "lbs", "km", "m", "cm", "mm", "mi", "usd", "eur", "gbp",
|
|
39
|
+
"inr", "jpy", "cny", "aud", "cad", "chf", "l", "ml", "w", "kw", "kwh", "v", "mb", "gb", "tb", "deg",
|
|
40
|
+
"degc", "degf", "celsius", "fahrenheit", "hz", "khz", "mhz", "ghz", "ppm", "ppb", "hpa", "kpa", "pa",
|
|
41
|
+
"mbar", "bar", "psi", "kcal", "cal", "kj", "mg", "mmhg", "db", "lux", "rpm", "mah", "wh", "musd",
|
|
42
|
+
"meur", "mgbp", "kusd", "keur", "mn", "mln", "bn"}
|
|
43
|
+
_GENERIC = {"value", "values", "val", "result", "results", "data", "output", "input", "id", "ids", "index",
|
|
44
|
+
"idx", "status", "code", "type", "name", "key", "n", "x", "y", "z", "arg", "args", "param", "params",
|
|
45
|
+
"number", "num", "query", "text", "item", "items", "state", "count"}
|
|
46
|
+
# result fields that say when, not how much: a number is not compared with them ("year‑over‑year")
|
|
47
|
+
_WHEN_KEYS = {"year", "date", "timestamp", "time", "datetime"}
|
|
48
|
+
_IRREGULAR = {"monthly": "month", "weekly": "week", "daily": "day", "yearly": "year", "hourly": "hour",
|
|
49
|
+
"annually": "year", "annual": "year"}
|
|
50
|
+
_LINKS = {"=", ":", "≈", "~", "is", "was", "are", "were", "of", "at", "to", "about", "around", "approximately",
|
|
51
|
+
"approx", "roughly", "nearly", "falls", "fell", "rises", "rose", "drops", "dropped", "reaches", "reached",
|
|
52
|
+
"equals", "equal", "stands", "comes", "becomes", "became", "remains", "be", "will", "would", "now", "by",
|
|
53
|
+
"the", "a", "an", "its", "their", "has", "have", "had", "gives", "gave", "shows", "showed", "goes", "went",
|
|
54
|
+
"increases", "increased", "decreases", "decreased", "climbs", "climbed", "totals", "totaled", "totalled"}
|
|
55
|
+
_FILLERS = {"of", "in", "a", "an", "the", "per", "for"}
|
|
56
|
+
_STOP_WORDS = {"and", "but", "while", "with", "against", "under", "over", "versus", "vs", "than", "or", "compared",
|
|
57
|
+
"whereas", "so", "then"}
|
|
58
|
+
# going back from a number, these start a different phrase: "the monthly payment for a $30,000 loan"
|
|
59
|
+
# ("in" is not here: "the total paid in all was 32,126" names the field before it)
|
|
60
|
+
_BEFORE_STOP = {"for", "on", "from", "per", "after", "before", "during", "within", "into", "onto", "across",
|
|
61
|
+
"via", "toward", "towards", "versus"}
|
|
62
|
+
_QUESTION_STOP = {"of", "the", "a", "an", "at", "for", "with", "and", "to", "in", "on", "is", "by", "my", "me", "i",
|
|
63
|
+
"what", "how", "much", "many", "if", "be", "will", "it", "its"}
|
|
64
|
+
|
|
65
|
+
_TIME_NOUNS = {"day", "days", "week", "weeks", "month", "months", "year", "years", "hour", "hours"}
|
|
66
|
+
_MIN_MAX = {"min": "minimum", "max": "maximum"}
|
|
67
|
+
_LOW_WORDS, _HIGH_WORDS = {"low", "min", "minimum", "lowest"}, {"high", "max", "maximum", "highest"}
|
|
68
|
+
|
|
69
|
+
_TOKEN = re.compile(r"[^\W\d_]+|[,;.!?()\n]|[=:≈~]")
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def stem(word: str) -> str:
|
|
73
|
+
w = word.lower()
|
|
74
|
+
if w in _IRREGULAR:
|
|
75
|
+
return _IRREGULAR[w]
|
|
76
|
+
if w.endswith("ies") and len(w) > 4:
|
|
77
|
+
return w[:-3] + "y"
|
|
78
|
+
if w.endswith("ed") and len(w) >= 6 and not w.endswith("eed"): # converted, earned: convert, earn
|
|
79
|
+
return w[:-2]
|
|
80
|
+
if w.endswith("s") and not w.endswith("ss") and len(w) > 3:
|
|
81
|
+
return w[:-1]
|
|
82
|
+
return w
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def key_words(key: str) -> tuple:
|
|
86
|
+
"""'rain_chance_pct' -> ('rain', 'chance'); 'werner_weight' -> ('werner', 'weight'); generic keys -> ()."""
|
|
87
|
+
parts = re.findall(r"[A-Z]?[a-z]+|[A-Z]+(?![a-z])|\d+", key)
|
|
88
|
+
if any(p.lower() in _TIME_NOUNS for p in parts): # days_min, days_max: minimum and maximum, not minutes
|
|
89
|
+
parts = [_MIN_MAX.get(p.lower(), p) for p in parts]
|
|
90
|
+
words = [stem(p) for p in parts if p.lower() not in _UNIT_WORDS and not p.isdigit()]
|
|
91
|
+
if not words or all(w in _GENERIC for w in words):
|
|
92
|
+
return ()
|
|
93
|
+
return tuple(w for w in words if w not in _GENERIC) or tuple(words)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
# ---------------------------------------------------------------- tokens of a text
|
|
97
|
+
def _items(text: str) -> list:
|
|
98
|
+
"""The text as an ordered list of ("num", Number) and ("tok", word-or-mark, start, end)."""
|
|
99
|
+
nums = num.find_numbers(text)
|
|
100
|
+
items = [("num", n, n.start, n.end) for n in nums]
|
|
101
|
+
for m in _TOKEN.finditer(text):
|
|
102
|
+
if not any(n.start <= m.start() < n.end for n in nums):
|
|
103
|
+
items.append(("tok", m.group(0), m.start(), m.end()))
|
|
104
|
+
return sorted(items, key=lambda it: it[2])
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def _is_stop(tok: str) -> bool:
|
|
108
|
+
return tok in ",;.!?()\n" or tok.lower() in _STOP_WORDS
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def _words_after(items: list, i: int, k: int = 3) -> list:
|
|
112
|
+
"""Up to k content words after item i, stopping at punctuation, a conjunction or a number:
|
|
113
|
+
[(stem, start, end)]."""
|
|
114
|
+
out = []
|
|
115
|
+
for kind, tok, a, b in items[i + 1:]:
|
|
116
|
+
if kind == "num" or _is_stop(tok):
|
|
117
|
+
break
|
|
118
|
+
low = tok.lower()
|
|
119
|
+
if low in _FILLERS or low in _UNIT_WORDS or not tok[0].isalpha():
|
|
120
|
+
continue
|
|
121
|
+
out.append((stem(tok), a, b))
|
|
122
|
+
if len(out) == k:
|
|
123
|
+
break
|
|
124
|
+
return out
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def _words_before(items: list, i: int, k: int = 3) -> list:
|
|
128
|
+
"""Up to k content words before item i (closest first), skipping link words ("is", "of", "=") and
|
|
129
|
+
stopping at punctuation, a conjunction or a number."""
|
|
130
|
+
out = []
|
|
131
|
+
for j, (kind, tok, a, b) in enumerate(reversed(items[:i])):
|
|
132
|
+
if j == 0 and tok == "(": # "more points (31)": the bracket opens on the number itself
|
|
133
|
+
continue
|
|
134
|
+
if kind == "num" or _is_stop(tok) or tok.lower() in _BEFORE_STOP:
|
|
135
|
+
break
|
|
136
|
+
if tok.lower() in _LINKS or not (tok[0].isalpha() or tok in "=:≈~"):
|
|
137
|
+
continue
|
|
138
|
+
out.append((stem(tok), a, b))
|
|
139
|
+
if len(out) == k:
|
|
140
|
+
break
|
|
141
|
+
return out
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def _matching(words: list, fields: list, slack: int = 1) -> list:
|
|
145
|
+
"""Fields all of whose words are among the first len(words) + slack of `words`."""
|
|
146
|
+
hits = []
|
|
147
|
+
for f in fields:
|
|
148
|
+
fw = f["words"]
|
|
149
|
+
window = words[:len(fw) + slack]
|
|
150
|
+
if fw and set(fw) <= {w for w, _a, _b in window}:
|
|
151
|
+
span = [(a, b) for w, a, b in window if w in fw]
|
|
152
|
+
hits.append((f, min(a for a, _b in span), max(b for _a, b in span)))
|
|
153
|
+
return hits
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
# ---------------------------------------------------------------- value-to-field
|
|
157
|
+
def _fields(calls: list, synonyms: dict, which: str) -> list:
|
|
158
|
+
out = []
|
|
159
|
+
for c in calls:
|
|
160
|
+
if c.error:
|
|
161
|
+
continue
|
|
162
|
+
obj = c.result if which == "result" else c.args
|
|
163
|
+
for path, key, value in numeric_fields(obj):
|
|
164
|
+
if which == "result" and key.lower() in _WHEN_KEYS:
|
|
165
|
+
continue
|
|
166
|
+
names = [key_words(key)] + [tuple(stem(w) for w in re.findall(r"[^\W\d_]+", s))
|
|
167
|
+
for s in synonyms.get(key, [])]
|
|
168
|
+
for words in names:
|
|
169
|
+
if words:
|
|
170
|
+
out.append({"call": c, "path": path, "key": key, "value": value, "words": words, "kind": which})
|
|
171
|
+
return out
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
_UNITS = { # word or symbol -> (kind, unit)
|
|
175
|
+
**{w: ("temperature", "C") for w in ("c", "celsius", "degc")}, **{w: ("temperature", "F") for w in ("f", "fahrenheit", "degf")},
|
|
176
|
+
**{w: ("length", "km") for w in ("km", "kilometre", "kilometres", "kilometer", "kilometers")},
|
|
177
|
+
**{w: ("length", "mi") for w in ("mi", "mile", "miles")}, **{w: ("length", "m") for w in ("m", "metre", "metres", "meter", "meters")},
|
|
178
|
+
**{w: ("length", "ft") for w in ("ft", "foot", "feet")},
|
|
179
|
+
**{w: ("mass", "kg") for w in ("kg", "kilogram", "kilograms")}, **{w: ("mass", "lb") for w in ("lb", "lbs", "pound", "pounds")},
|
|
180
|
+
**{w: ("mass", "g") for w in ("g", "gram", "grams")},
|
|
181
|
+
**{w: ("money", "USD") for w in ("usd", "$", "dollar", "dollars")}, **{w: ("money", "EUR") for w in ("eur", "€", "euro", "euros")},
|
|
182
|
+
**{w: ("money", "GBP") for w in ("gbp", "£", "pound sterling")}, **{w: ("money", "INR") for w in ("inr", "₹", "rupee", "rupees")},
|
|
183
|
+
**{w: ("money", "JPY") for w in ("jpy", "¥", "yen")}, **{w: ("money", "CHF") for w in ("chf", "franc", "francs")},
|
|
184
|
+
**{w: ("speed", "kmh") for w in ("kmh", "kph", "km/h")}, **{w: ("speed", "mph") for w in ("mph",)},
|
|
185
|
+
**{w: ("time", "hour") for w in ("hour", "hours", "hr", "hrs")},
|
|
186
|
+
**{w: ("time", "minute") for w in ("minute", "minutes", "mins")}, **{w: ("time", "day") for w in ("day", "days")},
|
|
187
|
+
**{w: ("time", "week") for w in ("week", "weeks")}, **{w: ("time", "month") for w in ("month", "months")},
|
|
188
|
+
**{w: ("time", "year") for w in ("year", "years")},
|
|
189
|
+
}
|
|
190
|
+
_AFTER_UNIT = re.compile(r"\s?(?:°\s?(?P<deg>[CF])\b|(?P<word>km/h|[A-Za-z]+))")
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
def answer_unit(answer: str, n: num.Number):
|
|
194
|
+
"""(kind, unit) written right after the number ("91.4 °F", "42.16 km") or right before it ("$249")."""
|
|
195
|
+
m = _AFTER_UNIT.match(answer, n.end)
|
|
196
|
+
if m:
|
|
197
|
+
w = (m.group("deg") or m.group("word")).lower()
|
|
198
|
+
if w in _UNITS:
|
|
199
|
+
return _UNITS[w]
|
|
200
|
+
before = answer[max(0, n.start - 1):n.start]
|
|
201
|
+
return _UNITS.get(before)
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def key_unit(key: str):
|
|
205
|
+
"""(kind, unit) of a JSON key's unit suffix: high_c -> temperature C, price_usd -> money USD."""
|
|
206
|
+
parts = re.findall(r"[A-Za-z]+", key.replace("_", " "))
|
|
207
|
+
if len(parts) < 2:
|
|
208
|
+
return None
|
|
209
|
+
last = parts[-1].lower()
|
|
210
|
+
if last not in _UNITS and len(last) == 4 and last[0] in "mkb": # musd, keur, bgbp: scale + currency
|
|
211
|
+
last = last[1:]
|
|
212
|
+
return _UNITS.get(last)
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
def _hit(n: num.Number, f: dict) -> bool:
|
|
216
|
+
"""n states field f's value, also at the scale its key names (revenue_musd 1840.5 as "1.84 billion")."""
|
|
217
|
+
scale = num.key_scale(f["key"])
|
|
218
|
+
return _matches(n, f["value"]) or (scale != 1 and _matches(n, f["value"] * scale))
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
def _matches(n: num.Number, value: float) -> bool:
|
|
222
|
+
"""Magnitudes, as in the number check: "down 1.2%" for a change of -1.2."""
|
|
223
|
+
return any(num._close(c, t, abs(value)) for c, t in n.candidates())
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
_COPULA = {"is", "was", "are", "were", "be"}
|
|
227
|
+
_APPROX_WORDS = {"about", "around", "approximately", "approx", "roughly", "nearly", "some", "just", "only",
|
|
228
|
+
"exactly", "almost", "the", "a", "an"}
|
|
229
|
+
# a subject with one of these names a quantity worked out from a field, not the field itself
|
|
230
|
+
_COMPUTED = {"difference", "gap", "increase", "decrease", "growth", "saving", "extra", "ratio", "average", "mean",
|
|
231
|
+
"combined", "sum", "half", "double", "twice", "remaining", "per", "each"}
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
_COMPARATIVE = {"higher", "lower", "more", "less", "fewer", "warmer", "cooler", "colder", "hotter", "cheaper",
|
|
235
|
+
"dearer", "longer", "shorter", "bigger", "smaller", "greater", "larger", "apart", "than", "extra",
|
|
236
|
+
"difference", "faster", "slower", "heavier", "lighter"}
|
|
237
|
+
_YEAR_PREPS = {"in", "of", "for", "since", "from", "until", "till", "by", "during", "to", "before", "after"}
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
def _computed_clause(items: list, i: int) -> bool:
|
|
241
|
+
"""The number is worked out, not a field's value: the clause before it says so ("the difference in
|
|
242
|
+
monthly payment is $166") or a comparative follows it ("3.3 hPa higher than", "7 °C warmer")."""
|
|
243
|
+
for kind, tok, _a, _b in reversed(items[:i]):
|
|
244
|
+
if kind == "tok" and tok in ",;.!?\n":
|
|
245
|
+
break
|
|
246
|
+
if kind == "tok" and stem(tok) in _COMPUTED:
|
|
247
|
+
return True
|
|
248
|
+
for kind, tok, _a, _b in items[i + 1:i + 4]:
|
|
249
|
+
if kind == "num" or (kind == "tok" and tok in ",;.!?\n"):
|
|
250
|
+
break
|
|
251
|
+
if tok.lower() in _COMPARATIVE:
|
|
252
|
+
return True
|
|
253
|
+
return False
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
def _a_year(items: list, i: int, when_values=()) -> bool:
|
|
257
|
+
""" "in 2024", "from 2024 to 2025", "a 2025 revenue": a plain whole number from 1900 to 2100 after such a
|
|
258
|
+
word, or equal to a year the calls give, is a year."""
|
|
259
|
+
n = items[i][1]
|
|
260
|
+
if not (n.integer and 1900 <= n.value <= 2100):
|
|
261
|
+
return False
|
|
262
|
+
if any(abs(n.value - v) < 1e-9 for v in when_values):
|
|
263
|
+
return True
|
|
264
|
+
prev = next((t for t in reversed(items[:i]) if t[0] == "num" or t[1] not in "*_"), None)
|
|
265
|
+
return prev is not None and prev[0] == "tok" and prev[1].lower() in _YEAR_PREPS
|
|
266
|
+
_SUBJECT_END = _BEFORE_STOP | {"of", "at", "in", "over", "with", "under", "by", "if", "when", "after", "than"}
|
|
267
|
+
_RATE_WORDS = ("pct", "percent", "rate", "margin", "ratio", "share")
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
def _key_ok(n: num.Number, key: str) -> bool:
|
|
271
|
+
"""A percentage is tied by a lone word only to a field that may hold one ("margin_pct", "rate")."""
|
|
272
|
+
return not n.percent or any(w in key.lower() for w in _RATE_WORDS)
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
def _one_field(cands: list):
|
|
276
|
+
"""The field the candidates name, if they name one: the key with the most words, when no other key has as
|
|
277
|
+
many. [(field, start, end)] for every call that has that key, or []."""
|
|
278
|
+
if not cands:
|
|
279
|
+
return []
|
|
280
|
+
most = max(len(f["words"]) for f, _a, _b in cands)
|
|
281
|
+
keys = {f["key"] for f, _a, _b in cands if len(f["words"]) == most}
|
|
282
|
+
return [c for c in cands if c[0]["key"] in keys] if len(keys) == 1 else []
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
# measure nouns name what is counted, not which field: "after 3 years" is not warranty_years
|
|
286
|
+
_MEASURE = {"year", "month", "week", "day", "hour", "minute", "second", "time", "unit", "item", "piece", "people",
|
|
287
|
+
"person", "point", "dollar", "euro", "rupee", "pound", "cent", "night", "seat", "share", "km", "mile",
|
|
288
|
+
"meter", "metre", "gram", "kilo", "litre", "liter", "degree", "percent", "percentage", "number", "total",
|
|
289
|
+
"amount", "value", "level", "count"}
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
def _distinctive(word, results: list, n, a: int, b: int) -> list:
|
|
293
|
+
"""A lone word next to the number that only one multi-word field name has: "€789.52 in interest" when
|
|
294
|
+
total_interest is the only field with "interest" ("total" alone fits total_paid as well; measure nouns
|
|
295
|
+
such as "years" never name a field on their own)."""
|
|
296
|
+
if not word or word in _MEASURE:
|
|
297
|
+
return []
|
|
298
|
+
cands = [(f, a, b) for f in results if word in f["words"]]
|
|
299
|
+
keys = {f["key"] for f, _a, _b in cands}
|
|
300
|
+
if len(keys) != 1 or len(cands[0][0]["words"]) < 2 or not _key_ok(n, cands[0][0]["key"]):
|
|
301
|
+
return []
|
|
302
|
+
return cands
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
def _after_copula(items: list, i: int) -> bool:
|
|
306
|
+
"""The number comes right after "is", "was", ... and the word before that verb ends a phrase that starts
|
|
307
|
+
with a preposition: "the rating for this business according to customer reviews is 3.5", where
|
|
308
|
+
"reviews" is not what the number is."""
|
|
309
|
+
j = i - 1
|
|
310
|
+
while j >= 0 and items[j][0] == "tok" and (items[j][1].lower() in _APPROX_WORDS or items[j][1] in "($*"):
|
|
311
|
+
j -= 1
|
|
312
|
+
if not (j >= 0 and items[j][0] == "tok" and items[j][1].lower() in _COPULA):
|
|
313
|
+
return False
|
|
314
|
+
for kind, tok, _a, _b in reversed(items[:j]):
|
|
315
|
+
if kind == "num" or not (tok[0].isalpha() or tok[0] in "'’"):
|
|
316
|
+
return False
|
|
317
|
+
if tok.lower() in _SUBJECT_END or tok.lower() in ("to", "according"):
|
|
318
|
+
return True
|
|
319
|
+
return False
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
def _subject(items: list, i: int, results: list, n) -> list:
|
|
323
|
+
""""The total interest you'll pay on a $15,000 loan ... is $300.57": a number right after "is" or "was"
|
|
324
|
+
is the value of the sentence's subject, the words from the sentence start up to a preposition."""
|
|
325
|
+
j = i - 1
|
|
326
|
+
while j >= 0 and items[j][0] == "tok" and (items[j][1].lower() in _APPROX_WORDS or items[j][1] in "($*"):
|
|
327
|
+
j -= 1
|
|
328
|
+
if j < 0 or items[j][0] != "tok" or items[j][1].lower() not in _COPULA:
|
|
329
|
+
return []
|
|
330
|
+
k = j
|
|
331
|
+
while k > 0 and not (items[k - 1][0] == "tok" and items[k - 1][1] in ".!?\n:;"):
|
|
332
|
+
k -= 1
|
|
333
|
+
words = []
|
|
334
|
+
for kind, tok, a, b in items[k:j]:
|
|
335
|
+
if kind == "num" or _is_stop(tok) or tok.lower() in _SUBJECT_END:
|
|
336
|
+
break
|
|
337
|
+
if tok[0].isalpha():
|
|
338
|
+
words.append((stem(tok), a, b))
|
|
339
|
+
if any(w in _COMPUTED for w, _a, _b in words):
|
|
340
|
+
return []
|
|
341
|
+
cands = [c for c in _matching(words, results, slack=len(words)) if _key_ok(n, c[0]["key"])]
|
|
342
|
+
return _one_field(cands)
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
_RANGE_CUE = {"between", "from", "range", "ranging", "ranges", "spans"}
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
def _range_pairs(results: list) -> list:
|
|
349
|
+
"""[(low fields, high fields)] of result fields whose names differ only by low/high or min/max:
|
|
350
|
+
day_low and day_high, days_min and days_max (for every call that has them)."""
|
|
351
|
+
pairs = []
|
|
352
|
+
for f in results:
|
|
353
|
+
lo = set(f["words"]) & _LOW_WORDS
|
|
354
|
+
if not lo or f["words"] == tuple(lo):
|
|
355
|
+
continue
|
|
356
|
+
rest = set(f["words"]) - lo
|
|
357
|
+
for g in results:
|
|
358
|
+
hi = set(g["words"]) & _HIGH_WORDS
|
|
359
|
+
if g["call"] is f["call"] and hi and set(g["words"]) - hi == rest:
|
|
360
|
+
lows = [x for x in results if x["key"] == f["key"]]
|
|
361
|
+
highs = [x for x in results if x["key"] == g["key"]]
|
|
362
|
+
if (lows, highs) not in pairs:
|
|
363
|
+
pairs.append((lows, highs))
|
|
364
|
+
return pairs
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
def _ranges(items: list, answer: str, results: list) -> dict:
|
|
368
|
+
"""{item index: [(field, start, end)]} for the two numbers of a range ("between $69.80 and $72.90",
|
|
369
|
+
"a trading range of $69.80 to $72.90", "6-10 days") when the results have exactly one low/high pair."""
|
|
370
|
+
pairs = _range_pairs(results)
|
|
371
|
+
if len(pairs) != 1:
|
|
372
|
+
return {}
|
|
373
|
+
lows, highs = pairs[0]
|
|
374
|
+
out = {}
|
|
375
|
+
for i, (kind, n, a, b) in enumerate(items):
|
|
376
|
+
if kind != "num":
|
|
377
|
+
continue
|
|
378
|
+
j = i + 1
|
|
379
|
+
dash = j < len(items) and items[j][0] == "num" and re.fullmatch(r"\s*[-\u2010-\u2014]\s*", answer[b:items[j][2]])
|
|
380
|
+
word = (j + 1 < len(items) and items[j][0] == "tok" and items[j][1].lower() in ("to", "and")
|
|
381
|
+
and items[j + 1][0] == "num")
|
|
382
|
+
if not (dash or word):
|
|
383
|
+
continue
|
|
384
|
+
k = j if dash else j + 1
|
|
385
|
+
before = [t[1].lower() for t in items[max(0, i - 6):i] if t[0] == "tok"]
|
|
386
|
+
if not dash and not (_RANGE_CUE & set(before)):
|
|
387
|
+
continue
|
|
388
|
+
hi_n = items[k][1]
|
|
389
|
+
out[i] = [(f, n.start, hi_n.end) for f in lows]
|
|
390
|
+
out[k] = [(f, n.start, hi_n.end) for f in highs]
|
|
391
|
+
return out
|
|
392
|
+
|
|
393
|
+
|
|
394
|
+
def list_counts(calls: list) -> list:
|
|
395
|
+
"""[(word, length)] for every list in the calls' results: {"reviews": [..3 items..]} gives ("review", 3)."""
|
|
396
|
+
out = []
|
|
397
|
+
|
|
398
|
+
def walk(obj):
|
|
399
|
+
if isinstance(obj, dict):
|
|
400
|
+
for k, v in obj.items():
|
|
401
|
+
if isinstance(v, list):
|
|
402
|
+
for w in key_words(str(k)) or ():
|
|
403
|
+
out.append((w, len(v)))
|
|
404
|
+
walk(v)
|
|
405
|
+
elif isinstance(obj, list):
|
|
406
|
+
for v in obj:
|
|
407
|
+
walk(v)
|
|
408
|
+
for c in calls:
|
|
409
|
+
walk(c.result)
|
|
410
|
+
return out
|
|
411
|
+
|
|
412
|
+
|
|
413
|
+
def check_value_to_field(answer: str, calls: list, *, field_synonyms=None, sentence_of=None) -> tuple:
|
|
414
|
+
synonyms = field_synonyms or {}
|
|
415
|
+
results = _fields(calls, synonyms, "result")
|
|
416
|
+
args = _fields(calls, synonyms, "args")
|
|
417
|
+
if not results:
|
|
418
|
+
return [], {"bindings_checked": 0, "bindings_wrong": 0}
|
|
419
|
+
items = _items(answer)
|
|
420
|
+
question_values = [(c, v) for c in calls for _k, v in _arg_values(c)]
|
|
421
|
+
in_range = _ranges(items, answer, results)
|
|
422
|
+
counted = {w for w, _n in list_counts(calls)}
|
|
423
|
+
when_values = [float(v) for c in calls for obj in (c.args, c.result) if isinstance(obj, dict)
|
|
424
|
+
for k, v in obj.items() if k.lower() in _WHEN_KEYS and isinstance(v, (int, float))
|
|
425
|
+
and not isinstance(v, bool)]
|
|
426
|
+
when_values += [float(y) for c in calls for y in re.findall(r"(?<!\d)((?:19|20)\d\d)-\d\d-\d\d", c.render())]
|
|
427
|
+
skip = num.excluded_spans(answer)
|
|
428
|
+
# "high and low are 9 and 4 respectively": the pairing is by position, not by neighbouring words
|
|
429
|
+
skip += [(m.start(), m.end()) for m in re.finditer(r"[^.!?\n]*\brespectively\b[^.!?\n]*", answer, re.I)]
|
|
430
|
+
used_words = set()
|
|
431
|
+
findings, checked = [], 0
|
|
432
|
+
rendered = " ".join(re.sub(r"\s+", " ", c.render().lower()) for c in calls)
|
|
433
|
+
for i, (kind, n, _a, _b) in enumerate(items):
|
|
434
|
+
if kind != "num" or any(a <= n.start < b for a, b in skip):
|
|
435
|
+
continue
|
|
436
|
+
# "4 Eggs & Pizza", "223 Anacapa St": the number with its next or previous word is text in a result
|
|
437
|
+
# (a name, an address), not a value of a field
|
|
438
|
+
nxt = re.match(r"[°'’]?\s+[A-Za-z][\w'’]*", answer[n.end:])
|
|
439
|
+
prv = re.search(r"[A-Za-z][\w'’]*\s+$", answer[max(0, n.start - 40):n.start])
|
|
440
|
+
if (nxt and (n.text.strip() + nxt.group()).lower() in rendered) or \
|
|
441
|
+
(prv and (prv.group() + n.text.strip()).lower() in rendered):
|
|
442
|
+
continue
|
|
443
|
+
after_words = _words_after(items, i)
|
|
444
|
+
after = _matching(after_words, results, slack=0) # "7 rebounds": the name follows at once
|
|
445
|
+
if after: # "3 years at 7%": the words a number takes after it are not the next number's
|
|
446
|
+
used_words.update((a2, b2) for _w, a2, b2 in after_words)
|
|
447
|
+
before_words = [w for w in _words_before(items, i) if (w[1], w[2]) not in used_words]
|
|
448
|
+
if (_computed_clause(items, i) and i not in in_range) or _a_year(items, i, when_values):
|
|
449
|
+
continue
|
|
450
|
+
if after_words and after_words[0][0] in counted:
|
|
451
|
+
continue # "based on 3 reviews": how many items a list has, not a field in it
|
|
452
|
+
bound = in_range.get(i) or after or _matching(before_words, results)
|
|
453
|
+
unit = answer_unit(answer, n)
|
|
454
|
+
if unit and unit[0] == "money":
|
|
455
|
+
# "€221.4 million" for net_income_musd = 221.4: the right number in another currency, and no call
|
|
456
|
+
# converted it
|
|
457
|
+
for f, a2, b2 in bound:
|
|
458
|
+
fu = key_unit(f["key"])
|
|
459
|
+
if fu and fu[0] == "money" and fu != unit and _hit(n, f) and not any(
|
|
460
|
+
g is not f["call"] and any(abs(v - f["value"]) < 1e-9 for _k, v in _arg_values(g))
|
|
461
|
+
for g in calls):
|
|
462
|
+
start, end = min(n.start, a2), max(n.end, b2)
|
|
463
|
+
findings.append(Finding(
|
|
464
|
+
check="value-to-field", severity="high", start=start, end=end, text=answer[start:end],
|
|
465
|
+
sentence=sentence_of(start) if sentence_of else answer,
|
|
466
|
+
message=(f"\"{answer[start:end]}\": the tool results give {f['key']} = {_fmt(f['value'])} "
|
|
467
|
+
f"({f['call'].id}) in {fu[1]}; the answer writes {unit[1]}, and no call converted it."),
|
|
468
|
+
evidence=[{"source": f["call"].id, "value": f"{f['path']} = {_fmt(f['value'])}",
|
|
469
|
+
"context": f["call"].render()}]))
|
|
470
|
+
checked += 1
|
|
471
|
+
break
|
|
472
|
+
if findings and findings[-1].start <= n.start < findings[-1].end:
|
|
473
|
+
continue
|
|
474
|
+
if unit: # "91.4 °F" against high_c: another unit, so the values cannot be compared
|
|
475
|
+
bound = [(f, a, b) for f, a, b in bound
|
|
476
|
+
if not (key_unit(f["key"]) and key_unit(f["key"])[0] == unit[0] and key_unit(f["key"]) != unit)]
|
|
477
|
+
if not bound and unit: # "weighs 249 kg": the only kg field of the results names the value
|
|
478
|
+
same_unit = [f for f in results if key_unit(f["key"]) == unit]
|
|
479
|
+
if len({(id(f["call"]), f["key"]) for f in same_unit}) == 1:
|
|
480
|
+
f = same_unit[0]
|
|
481
|
+
# only a value of another field of that same call is reported: "$1.35" when weight_kg is 1.35
|
|
482
|
+
swapped = any(g["call"] is f["call"] and g["key"] != f["key"] and _hit(n, g)
|
|
483
|
+
for g in results)
|
|
484
|
+
if _hit(n, f) or swapped:
|
|
485
|
+
bound = [(f, n.start, n.end)]
|
|
486
|
+
if not bound and not any(_matches(n, v) for _c, v in question_values):
|
|
487
|
+
# a lone distinctive word right after or right before the number, or the subject of "... is N"
|
|
488
|
+
after1 = after_words[0] if after_words else None
|
|
489
|
+
before1 = before_words[0] if before_words else None
|
|
490
|
+
# "the rating for this business according to customer reviews is 3.5": after "is", the subject
|
|
491
|
+
# names the field, not the word just before "is"
|
|
492
|
+
if _after_copula(items, i):
|
|
493
|
+
before1 = None
|
|
494
|
+
bound = (_distinctive(after1 and after1[0], results, n, *(after1[1:] if after1 else (0, 0)))
|
|
495
|
+
or _distinctive(before1 and before1[0], results, n, *(before1[1:] if before1 else (0, 0)))
|
|
496
|
+
or _subject(items, i, results, n))
|
|
497
|
+
if bound and unit:
|
|
498
|
+
bound = [(f, a, b) for f, a, b in bound if not (key_unit(f["key"]) and key_unit(f["key"]) != unit)]
|
|
499
|
+
if not bound:
|
|
500
|
+
continue
|
|
501
|
+
same_name = [f for f, _a, _b in bound]
|
|
502
|
+
# "the price is $119.70" when a calculator turned price_usd 39.9 into 119.7: a value worked out by
|
|
503
|
+
# another call from the field is not the field's
|
|
504
|
+
if any(g["call"] is not f["call"] and _hit(n, g) and
|
|
505
|
+
any(abs(v - f["value"]) < 1e-9 for _k, v in _arg_values(g["call"]))
|
|
506
|
+
for f in same_name for g in results):
|
|
507
|
+
continue
|
|
508
|
+
checked += 1
|
|
509
|
+
names = {f["words"] for f in same_name}
|
|
510
|
+
support = same_name + [f for f in args if f["words"] in names]
|
|
511
|
+
if any(_hit(n, f) for f in support):
|
|
512
|
+
continue
|
|
513
|
+
# "One review gave it 1 star": a more specific field (review_stars) whose other words are close to the
|
|
514
|
+
# number, and which holds it, is the one meant
|
|
515
|
+
near = set()
|
|
516
|
+
for step, stop in ((-1, max(-1, i - 7)), (1, min(len(items), i + 4))):
|
|
517
|
+
for kind, tok, _a, _b in (items[j] for j in range(i + step, stop, step)):
|
|
518
|
+
if kind == "tok" and tok in ".!?\n;":
|
|
519
|
+
break
|
|
520
|
+
if kind == "tok" and tok[0].isalpha():
|
|
521
|
+
near.add(stem(tok))
|
|
522
|
+
if any(set(f["words"]) < set(g["words"]) and set(g["words"]) - set(f["words"]) <= near and _hit(n, g)
|
|
523
|
+
for f in same_name for g in results):
|
|
524
|
+
continue
|
|
525
|
+
start = min(n.start, min(a for _f, a, _b in bound))
|
|
526
|
+
end = max(n.end, max(b for _f, _a, b in bound))
|
|
527
|
+
gave = "; ".join(f"{f['key']} = {_fmt(f['value'])} ({f['call'].id})" for f in same_name)
|
|
528
|
+
other = next((f for f in results + args if f["words"] not in names and _hit(n, f)), None)
|
|
529
|
+
shown = n.text.strip()
|
|
530
|
+
message = f"\"{answer[start:end]}\": the tool results give {gave}."
|
|
531
|
+
if other:
|
|
532
|
+
message += f" {shown} is the {other['key']} ({other['call'].id})."
|
|
533
|
+
findings.append(Finding(
|
|
534
|
+
check="value-to-field", severity="high", start=start, end=end, text=answer[start:end],
|
|
535
|
+
sentence=sentence_of(start) if sentence_of else answer, message=message,
|
|
536
|
+
evidence=[{"source": f["call"].id, "value": f"{f['path']} = {_fmt(f['value'])}", "context": f["call"].render()}
|
|
537
|
+
for f in same_name]))
|
|
538
|
+
return findings, {"bindings_checked": checked, "bindings_wrong": len(findings)}
|
|
539
|
+
|
|
540
|
+
|
|
541
|
+
def _fmt(v: float) -> str:
|
|
542
|
+
return str(int(v)) if v == int(v) and abs(v) < 1e15 else repr(v)
|
|
543
|
+
|
|
544
|
+
|
|
545
|
+
# ---------------------------------------------------------------- argument binding
|
|
546
|
+
def _arg_values(call) -> list:
|
|
547
|
+
"""[(arg name, value as float)] of a call: numeric args and the numbers written inside string args."""
|
|
548
|
+
out = []
|
|
549
|
+
|
|
550
|
+
def walk(name, v):
|
|
551
|
+
if isinstance(v, bool) or v is None:
|
|
552
|
+
return
|
|
553
|
+
if isinstance(v, (int, float)):
|
|
554
|
+
out.append((name, float(v)))
|
|
555
|
+
elif isinstance(v, str):
|
|
556
|
+
for n in num.find_numbers(v):
|
|
557
|
+
out.extend((name, c) for c, _t in n.candidates())
|
|
558
|
+
out.extend((name, float(m.group(0))) for m in num._ANY_DIGITS.finditer(v))
|
|
559
|
+
elif isinstance(v, dict):
|
|
560
|
+
for k, x in v.items():
|
|
561
|
+
walk(k, x)
|
|
562
|
+
elif isinstance(v, list):
|
|
563
|
+
for x in v:
|
|
564
|
+
walk(name, x)
|
|
565
|
+
for k, v in call.args.items():
|
|
566
|
+
walk(k, v)
|
|
567
|
+
return out
|
|
568
|
+
|
|
569
|
+
|
|
570
|
+
def _strings(call) -> list:
|
|
571
|
+
out = []
|
|
572
|
+
|
|
573
|
+
def walk(v):
|
|
574
|
+
if isinstance(v, str):
|
|
575
|
+
out.append(v.casefold())
|
|
576
|
+
elif isinstance(v, dict):
|
|
577
|
+
for x in v.values():
|
|
578
|
+
walk(x)
|
|
579
|
+
elif isinstance(v, list):
|
|
580
|
+
for x in v:
|
|
581
|
+
walk(x)
|
|
582
|
+
walk(call.args)
|
|
583
|
+
return out
|
|
584
|
+
|
|
585
|
+
|
|
586
|
+
def _neighbours(items: list, i: int, claimed: set) -> tuple:
|
|
587
|
+
"""(words right after the number, words right before it): up to 2 each, small words left out, and
|
|
588
|
+
before it only words that the number before has not taken ("3-year loan at 7%": "year" is the 3's)."""
|
|
589
|
+
def pick(seq):
|
|
590
|
+
out = []
|
|
591
|
+
for kind, tok, a, b in seq:
|
|
592
|
+
if kind == "num" or tok in ",;.!?()\n":
|
|
593
|
+
break
|
|
594
|
+
if tok.lower() in _QUESTION_STOP or not tok[0].isalpha() or (a, b) in claimed:
|
|
595
|
+
continue
|
|
596
|
+
out.append((tok, a, b))
|
|
597
|
+
if len(out) == 2:
|
|
598
|
+
break
|
|
599
|
+
return out
|
|
600
|
+
after = pick(items[i + 1:])
|
|
601
|
+
before = pick(reversed(items[:i]))
|
|
602
|
+
claimed.update((a, b) for _t, a, b in after)
|
|
603
|
+
return [t for t, _a, _b in after], [t for t, _a, _b in before]
|
|
604
|
+
|
|
605
|
+
|
|
606
|
+
def _shifted(q: float, v: float) -> bool:
|
|
607
|
+
if q == 0 or v == 0 or q == v:
|
|
608
|
+
return False
|
|
609
|
+
r = v / q
|
|
610
|
+
return any(abs(r - f) <= 1e-9 * f for f in (10.0, 100.0, 1000.0, 0.1, 0.01, 0.001))
|
|
611
|
+
|
|
612
|
+
|
|
613
|
+
def _answer_span(answer: str, q: num.Number, spans_of_sentences) -> tuple:
|
|
614
|
+
"""Where in the answer to point: the first number equal to the question's, else the first sentence."""
|
|
615
|
+
for n in num.find_numbers(answer):
|
|
616
|
+
if any(num._close(c, t, q.value) for c, t in n.candidates()):
|
|
617
|
+
return n.start, n.end
|
|
618
|
+
a, b = spans_of_sentences[0] if spans_of_sentences else (0, len(answer))
|
|
619
|
+
return a, b
|
|
620
|
+
|
|
621
|
+
|
|
622
|
+
def check_arguments(answer: str, question: str, calls: list, *, sentence_spans=(), sentence_of=None) -> tuple:
|
|
623
|
+
calls = [c for c in calls if c.args]
|
|
624
|
+
if not question.strip() or not calls:
|
|
625
|
+
return [], {"question_values": 0, "arguments_wrong": 0}
|
|
626
|
+
items = _items(question)
|
|
627
|
+
skip = num.excluded_spans(question)
|
|
628
|
+
qnums = [(i, it[1]) for i, it in enumerate(items) if it[0] == "num" and not any(a <= it[1].start < b for a, b in skip)]
|
|
629
|
+
argvals = {c.id: _arg_values(c) for c in calls}
|
|
630
|
+
findings = []
|
|
631
|
+
|
|
632
|
+
def used(q, call=None):
|
|
633
|
+
pool = [argvals[call.id]] if call else argvals.values()
|
|
634
|
+
return any(any(num._close(c, t, abs(v)) for c, t in q.candidates()) for vals in pool for _name, v in vals)
|
|
635
|
+
|
|
636
|
+
def report(q, message, evidence_calls):
|
|
637
|
+
a, b = _answer_span(answer, q, list(sentence_spans))
|
|
638
|
+
findings.append(Finding(
|
|
639
|
+
check="argument", severity="high", start=a, end=b, text=answer[a:b],
|
|
640
|
+
sentence=sentence_of(a) if sentence_of else answer, message=message,
|
|
641
|
+
evidence=[{"source": c.id, "value": "", "context": c.render()} for c in evidence_calls]))
|
|
642
|
+
|
|
643
|
+
reported, claimed, neighbours = set(), set(), {}
|
|
644
|
+
for i, _q in qnums:
|
|
645
|
+
neighbours[i] = _neighbours(items, i, claimed)
|
|
646
|
+
for i, q in qnums:
|
|
647
|
+
after, before = neighbours[i]
|
|
648
|
+
near = {stem(w) for w in after + before}
|
|
649
|
+
# (b) a neighbouring word names an argument that was called with another value
|
|
650
|
+
named = [(c, name, v) for c in calls for name, v in argvals[c.id]
|
|
651
|
+
if near & set(key_words(name)) and _key_ok(q, name)] # "4% compounded" is not compounds_per_year
|
|
652
|
+
if named and not any(any(num._close(c2, t, abs(v)) for c2, t in q.candidates()) for _c, _n, v in named):
|
|
653
|
+
# the argument the words name best: "5-year" names years before annual_rate ("annual" is yearly too)
|
|
654
|
+
c, name, v = min(named, key=lambda x: (-len(near & set(key_words(x[1]))) / len(key_words(x[1])),
|
|
655
|
+
len(key_words(x[1]))))
|
|
656
|
+
word = next(w for w in after + before if stem(w) in set(key_words(name)))
|
|
657
|
+
a, b = q.start, q.end
|
|
658
|
+
for m in re.finditer(rf"(?<!\w){re.escape(word)}(?!\w)", question):
|
|
659
|
+
if abs(m.start() - q.end) < 15 or abs(q.start - m.end()) < 15:
|
|
660
|
+
a, b = min(a, m.start()), max(b, m.end())
|
|
661
|
+
break
|
|
662
|
+
report(q, f"The question asks for {question[a:b]}, but {c.id} was called with {name}={_fmt(v)}.", [c])
|
|
663
|
+
reported.add(i)
|
|
664
|
+
continue
|
|
665
|
+
if used(q):
|
|
666
|
+
continue
|
|
667
|
+
# (a) the same digits shifted by a power of ten
|
|
668
|
+
shifted = [(c, name, v) for c in calls for name, v in argvals[c.id] if _shifted(q.value, abs(v))]
|
|
669
|
+
if shifted:
|
|
670
|
+
c, name, v = shifted[0]
|
|
671
|
+
report(q, f"The question asks for {q.text.strip()}, but {c.id} was called with {name}={_fmt(v)}.", [c])
|
|
672
|
+
reported.add(i)
|
|
673
|
+
# (c) two values swapped between two calls
|
|
674
|
+
tags = {}
|
|
675
|
+
for i, q in qnums:
|
|
676
|
+
after, before = neighbours[i]
|
|
677
|
+
tag = (after or before or [None])[0]
|
|
678
|
+
if tag:
|
|
679
|
+
tags[i] = (q, tag.casefold())
|
|
680
|
+
pairs = list(tags.items())
|
|
681
|
+
for x in range(len(pairs)):
|
|
682
|
+
for y in range(x + 1, len(pairs)):
|
|
683
|
+
(i, (q1, t1)), (j, (q2, t2)) = pairs[x], pairs[y]
|
|
684
|
+
if t1 == t2 or i in reported or j in reported:
|
|
685
|
+
continue
|
|
686
|
+
with_q1 = [c for c in calls if used(q1, c)]
|
|
687
|
+
with_q2 = [c for c in calls if used(q2, c)]
|
|
688
|
+
|
|
689
|
+
def has(c, tag):
|
|
690
|
+
return any(re.search(rf"(?<!\w){re.escape(tag)}(?!\w)", s) for s in _strings(c))
|
|
691
|
+
right = any(has(c, t1) for c in with_q1) or any(has(c, t2) for c in with_q2)
|
|
692
|
+
swapped = any(has(c, t2) for c in with_q1) and any(has(c, t1) for c in with_q2)
|
|
693
|
+
if swapped and not right:
|
|
694
|
+
c1 = next(c for c in with_q1 if has(c, t2))
|
|
695
|
+
c2 = next(c for c in with_q2 if has(c, t1))
|
|
696
|
+
report(q1, f"The question pairs {q1.text.strip()} with {t1.upper() if len(t1) <= 4 else t1} and "
|
|
697
|
+
f"{q2.text.strip()} with {t2.upper() if len(t2) <= 4 else t2}, but the calls swap them: "
|
|
698
|
+
f"{c1.render().split(' returned')[0]} and {c2.render().split(' returned')[0]}.", [c1, c2])
|
|
699
|
+
reported.update((i, j))
|
|
700
|
+
return findings, {"question_values": len(qnums), "arguments_wrong": len(findings)}
|