evidgate 0.9.2__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.
- evidgate/__init__.py +6 -0
- evidgate/cli.py +599 -0
- evidgate/core/__init__.py +10 -0
- evidgate/core/builtin.py +105 -0
- evidgate/core/check.py +175 -0
- evidgate/core/evidence.py +243 -0
- evidgate/core/gate.py +154 -0
- evidgate/core/packs.py +90 -0
- evidgate/core/suggest.py +185 -0
- evidgate/ledger/__init__.py +4 -0
- evidgate/ledger/table.py +135 -0
- evidgate/packs/build.yaml +25 -0
- evidgate/packs/figures.yaml +29 -0
- evidgate/packs/repo.yaml +17 -0
- evidgate/plugins/__init__.py +1 -0
- evidgate/plugins/artifact.py +303 -0
- evidgate/plugins/prose.py +181 -0
- evidgate-0.9.2.dist-info/METADATA +386 -0
- evidgate-0.9.2.dist-info/RECORD +23 -0
- evidgate-0.9.2.dist-info/WHEEL +5 -0
- evidgate-0.9.2.dist-info/entry_points.txt +2 -0
- evidgate-0.9.2.dist-info/licenses/LICENSE +21 -0
- evidgate-0.9.2.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
"""artifact plugin: is the delivered thing still the thing you produced?
|
|
2
|
+
|
|
3
|
+
Derived from three rules that already govern this user's content assets:
|
|
4
|
+
|
|
5
|
+
1. one asset has exactly one source; a copy is either deleted or declared a derivative
|
|
6
|
+
2. a derivative is regenerated by a script, never hand-edited
|
|
7
|
+
3. registering at creation time is cheap; discovering later is expensive
|
|
8
|
+
|
|
9
|
+
Each rule is decidable from a manifest, so no guessing is involved.
|
|
10
|
+
|
|
11
|
+
Manifest (`artifacts.json`):
|
|
12
|
+
|
|
13
|
+
{
|
|
14
|
+
"root": ".",
|
|
15
|
+
"ignore": ["*.log", "tmp/", "*.tmp"],
|
|
16
|
+
"items": [
|
|
17
|
+
{"path": "src/report.md", "role": "source"},
|
|
18
|
+
{"path": "build/report.html", "role": "derivative", "source": "src/report.md",
|
|
19
|
+
"md5": "<hash of the artifact itself>",
|
|
20
|
+
"source_md5": "<hash of the source when this derivative was generated>"},
|
|
21
|
+
{"path": "draft/copy.md", "role": "derivative", "source": "src/report.md",
|
|
22
|
+
"same_as_source": true}
|
|
23
|
+
]
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
Rules and exit codes: 0 clean, 2 blocking, 3 advisory.
|
|
27
|
+
manifest every registered path exists; recorded md5 still matches (red)
|
|
28
|
+
stale the source changed after this derivative was generated (yellow)
|
|
29
|
+
orphan a file on disk that the manifest never registered (yellow)
|
|
30
|
+
sync same_as_source derivative differs from its source (red)
|
|
31
|
+
|
|
32
|
+
`stale` compares the recorded `source_md5` with the source's hash now. It does not use
|
|
33
|
+
mtime: every writer (git clone, unzip, rsync) stamps "now", so comparing times says
|
|
34
|
+
nothing about whether the source actually changed. Measured on this repo's own example,
|
|
35
|
+
the mtime version flipped between 1 PASS / 2 DIFF in a working tree and 2 PASS / 1 DIFF
|
|
36
|
+
after a clone; the hash version is identical in both.
|
|
37
|
+
"""
|
|
38
|
+
from __future__ import annotations
|
|
39
|
+
|
|
40
|
+
import fnmatch
|
|
41
|
+
import hashlib
|
|
42
|
+
import json
|
|
43
|
+
import os
|
|
44
|
+
import re
|
|
45
|
+
from typing import Dict, List, Tuple
|
|
46
|
+
|
|
47
|
+
SKIP_DIRS = {".git", "__pycache__", "node_modules", ".artifacts", ".venv", ".idea"}
|
|
48
|
+
# the gate's own config/output is not a deliverable, so it never counts as "unregistered"
|
|
49
|
+
SKIP_FILES = {"evidgate.yaml", "evidgate_ledger.md", "gate_report.md"}
|
|
50
|
+
|
|
51
|
+
RULES = ("manifest", "stale", "orphan", "sync", "derivatives")
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def _md5(path: str, normalize_newlines: bool = False) -> str:
|
|
55
|
+
h = hashlib.md5()
|
|
56
|
+
with open(path, "rb") as fh:
|
|
57
|
+
for chunk in iter(lambda: fh.read(1 << 20), b""):
|
|
58
|
+
if normalize_newlines:
|
|
59
|
+
chunk = chunk.replace(b"\\r\\n", b"\\n")
|
|
60
|
+
h.update(chunk)
|
|
61
|
+
return h.hexdigest()
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _root_of(data: dict, path: str) -> str:
|
|
65
|
+
root = data.get("root", ".")
|
|
66
|
+
if not os.path.isabs(root):
|
|
67
|
+
root = os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(path)), root))
|
|
68
|
+
return root
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def _load(path: str) -> Tuple[str, List[dict], List[str]]:
|
|
72
|
+
with open(path, encoding="utf-8") as fh:
|
|
73
|
+
data = json.load(fh)
|
|
74
|
+
return _root_of(data, path), data.get("items") or [], data.get("ignore") or []
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def _abs(root: str, rel: str) -> str:
|
|
78
|
+
return rel if os.path.isabs(rel) else os.path.normpath(os.path.join(root, rel))
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def rule_manifest(path: str) -> List[str]:
|
|
82
|
+
root, items, _ = _load(path)
|
|
83
|
+
hits = []
|
|
84
|
+
for it in items:
|
|
85
|
+
rel = it.get("path")
|
|
86
|
+
if not rel:
|
|
87
|
+
hits.append("清单里有一项缺少 path")
|
|
88
|
+
continue
|
|
89
|
+
full = _abs(root, rel)
|
|
90
|
+
if not os.path.exists(full):
|
|
91
|
+
hits.append(f"登记了但不存在: {rel}")
|
|
92
|
+
continue
|
|
93
|
+
want = it.get("md5")
|
|
94
|
+
# structural test, not a label: anything that declares a source, or is flagged
|
|
95
|
+
# same_as_source, is a derivative. Relying on role= meant a manifest entry that
|
|
96
|
+
# wrote `source` but forgot `role` was silently skipped (measured: empty result
|
|
97
|
+
# with a deliberately wrong md5).
|
|
98
|
+
is_derivative = bool(it.get("source") or it.get("same_as_source")
|
|
99
|
+
or it.get("role") == "derivative")
|
|
100
|
+
if want and is_derivative:
|
|
101
|
+
got = _md5(full)
|
|
102
|
+
if got != want:
|
|
103
|
+
# a pure line-ending difference is a checkout artefact, not an edit
|
|
104
|
+
if _md5(full, normalize_newlines=True) == want:
|
|
105
|
+
continue
|
|
106
|
+
hits.append(f"内容变了(手改或重跑过): {rel} md5 {got[:8]} != 登记 {want[:8]}")
|
|
107
|
+
return hits
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def rule_stale(path: str) -> List[str]:
|
|
111
|
+
"""The source moved on and the derivative was not regenerated.
|
|
112
|
+
|
|
113
|
+
Decided by hash, never by mtime -- see the module docstring for why.
|
|
114
|
+
"""
|
|
115
|
+
root, items, _ = _load(path)
|
|
116
|
+
hits = []
|
|
117
|
+
for it in items:
|
|
118
|
+
src, rel = it.get("source"), it.get("path")
|
|
119
|
+
if not (src and rel):
|
|
120
|
+
continue
|
|
121
|
+
s = _abs(root, src)
|
|
122
|
+
if not os.path.exists(s):
|
|
123
|
+
hits.append(f"源不存在,派生无从核对: {rel} <- {src}")
|
|
124
|
+
continue
|
|
125
|
+
recorded = it.get("source_md5")
|
|
126
|
+
if not recorded:
|
|
127
|
+
hits.append(
|
|
128
|
+
f"未记录 source_md5,无法判断是否过期: {rel}"
|
|
129
|
+
"(重新生成后跑 artifact-stamp 记录)")
|
|
130
|
+
continue
|
|
131
|
+
if _md5(s) != recorded:
|
|
132
|
+
hits.append(f"源已变,派生未重新生成: {rel} <- {src}")
|
|
133
|
+
return hits
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def rule_sync(path: str) -> List[str]:
|
|
137
|
+
root, items, _ = _load(path)
|
|
138
|
+
hits = []
|
|
139
|
+
for it in items:
|
|
140
|
+
if not it.get("same_as_source"):
|
|
141
|
+
continue
|
|
142
|
+
src, rel = it.get("source"), it.get("path")
|
|
143
|
+
if not (src and rel):
|
|
144
|
+
hits.append(f"same_as_source 缺 source/path: {it}")
|
|
145
|
+
continue
|
|
146
|
+
s, d = _abs(root, src), _abs(root, rel)
|
|
147
|
+
if not (os.path.exists(s) and os.path.exists(d)):
|
|
148
|
+
continue
|
|
149
|
+
if _md5(s) != _md5(d):
|
|
150
|
+
hits.append(f"派生与源不一致(违反「派生不手改」): {rel} != {src}")
|
|
151
|
+
return hits
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def rule_orphan(path: str, scan_dir: str = ".") -> List[str]:
|
|
155
|
+
root, items, ignore = _load(path)
|
|
156
|
+
patterns = [re.compile(fnmatch.translate(p)) for p in ignore]
|
|
157
|
+
known = {os.path.normpath(_abs(root, it["path"])) for it in items if it.get("path")}
|
|
158
|
+
known.add(os.path.normpath(os.path.abspath(path)))
|
|
159
|
+
base = _abs(root, scan_dir)
|
|
160
|
+
hits = []
|
|
161
|
+
for dp, dn, fn in os.walk(base):
|
|
162
|
+
dn[:] = [d for d in dn if d not in SKIP_DIRS]
|
|
163
|
+
for f in fn:
|
|
164
|
+
if f in SKIP_FILES:
|
|
165
|
+
continue
|
|
166
|
+
full = os.path.normpath(os.path.join(dp, f))
|
|
167
|
+
rel = os.path.relpath(full, root).replace(os.sep, "/")
|
|
168
|
+
if patterns and (any(r.match(rel) for r in patterns)
|
|
169
|
+
or any(r.match(f) for r in patterns)):
|
|
170
|
+
continue
|
|
171
|
+
if full not in known:
|
|
172
|
+
hits.append(f"未登记(漏纳管): {rel}")
|
|
173
|
+
return hits
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def refresh(path: str) -> int:
|
|
177
|
+
"""Recompute the artifacts' own recorded hashes. Never touches `source_md5`.
|
|
178
|
+
|
|
179
|
+
An earlier version also refreshed `source_md5`, which silently declared every
|
|
180
|
+
derivative up to date: one `artifact-update` erased every stale item without
|
|
181
|
+
regenerating anything. That is the opposite of a gate, so stamping is now a
|
|
182
|
+
separate, explicit act -- see `stamp()`.
|
|
183
|
+
"""
|
|
184
|
+
with open(path, encoding="utf-8") as fh:
|
|
185
|
+
data = json.load(fh)
|
|
186
|
+
root = _root_of(data, path)
|
|
187
|
+
changed = 0
|
|
188
|
+
for it in data.get("items") or []:
|
|
189
|
+
rel = it.get("path")
|
|
190
|
+
if not rel:
|
|
191
|
+
continue
|
|
192
|
+
# sources carry no md5: their only tracked fact is source_md5 on the derivatives,
|
|
193
|
+
# and only `artifact-stamp` writes that.
|
|
194
|
+
if not (it.get("source") or it.get("same_as_source") or it.get("role") == "derivative"):
|
|
195
|
+
continue
|
|
196
|
+
full = _abs(root, rel)
|
|
197
|
+
if os.path.exists(full):
|
|
198
|
+
got = _md5(full)
|
|
199
|
+
if it.get("md5") != got:
|
|
200
|
+
it["md5"] = got
|
|
201
|
+
changed += 1
|
|
202
|
+
with open(path, "w", encoding="utf-8", newline=chr(10)) as fh:
|
|
203
|
+
json.dump(data, fh, ensure_ascii=False, indent=2)
|
|
204
|
+
fh.write(chr(10))
|
|
205
|
+
return changed
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
def stamp(path: str, targets: List[str]) -> int:
|
|
209
|
+
"""Record "this derivative was generated from the source as it is right now".
|
|
210
|
+
|
|
211
|
+
The only writer of `source_md5`. Call it immediately after regenerating, never in
|
|
212
|
+
bulk: stamping something you did not regenerate is exactly the lie that `stale`
|
|
213
|
+
exists to catch.
|
|
214
|
+
"""
|
|
215
|
+
with open(path, encoding="utf-8") as fh:
|
|
216
|
+
data = json.load(fh)
|
|
217
|
+
root = _root_of(data, path)
|
|
218
|
+
by_path = {it.get("path"): it for it in data.get("items") or []}
|
|
219
|
+
n = 0
|
|
220
|
+
for t in targets:
|
|
221
|
+
it = by_path.get(t)
|
|
222
|
+
if it is None:
|
|
223
|
+
raise SystemExit(f"{t}: 不在清单里")
|
|
224
|
+
src = it.get("source")
|
|
225
|
+
if not src:
|
|
226
|
+
raise SystemExit(f"{t}: 清单里没有 source 字段,它不是一个派生")
|
|
227
|
+
sf, df = _abs(root, src), _abs(root, t)
|
|
228
|
+
if not os.path.exists(df):
|
|
229
|
+
raise SystemExit(f"{t}: 文件不存在,先重新生成再盖章")
|
|
230
|
+
if not os.path.exists(sf):
|
|
231
|
+
raise SystemExit(f"{t}: 源 {src} 不存在")
|
|
232
|
+
it["source_md5"] = _md5(sf)
|
|
233
|
+
it["md5"] = _md5(df)
|
|
234
|
+
n += 1
|
|
235
|
+
with open(path, "w", encoding="utf-8", newline=chr(10)) as fh:
|
|
236
|
+
json.dump(data, fh, ensure_ascii=False, indent=2)
|
|
237
|
+
fh.write(chr(10))
|
|
238
|
+
return n
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
def _split_args(argv: List[str]) -> Tuple[str, Dict[str, str]]:
|
|
242
|
+
positional, opts = [], {}
|
|
243
|
+
i = 0
|
|
244
|
+
while i < len(argv):
|
|
245
|
+
a = argv[i]
|
|
246
|
+
if a.startswith("--"):
|
|
247
|
+
opts[a[2:]] = argv[i + 1] if i + 1 < len(argv) else ""
|
|
248
|
+
i += 2
|
|
249
|
+
else:
|
|
250
|
+
positional.append(a)
|
|
251
|
+
i += 1
|
|
252
|
+
return (positional[0] if positional else ""), opts
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
def rule_derivatives(manifest_path: str) -> List[str]:
|
|
256
|
+
"""A manifest with no derivatives has nothing for the figures pack to guard.
|
|
257
|
+
|
|
258
|
+
Without this, `manifest`/`stale`/`orphan` all report clean over a list of 172
|
|
259
|
+
sources and the user reads "3 PASS" as "my figures are in order".
|
|
260
|
+
"""
|
|
261
|
+
_root, items, _ignore = _load(manifest_path)
|
|
262
|
+
# same structural test as rule_manifest: a declared `source`, or `same_as_source`,
|
|
263
|
+
# makes an entry a derivative. `role` alone is a label and can be missing.
|
|
264
|
+
deriv = [i for i in items if i.get("source") or i.get("same_as_source")]
|
|
265
|
+
if deriv:
|
|
266
|
+
return []
|
|
267
|
+
return [f"没有任何派生项:{len(items)} 条全部是 source。"
|
|
268
|
+
f"把生成的图改成 \"role\": \"derivative\" 并填 \"source\","
|
|
269
|
+
f"否则 manifest/stale/orphan 三项都在空转"]
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
def run(rule: str, argv: List[str]) -> int:
|
|
273
|
+
if rule not in RULES:
|
|
274
|
+
print(f"unknown artifact rule {rule!r} (have: {RULES})")
|
|
275
|
+
return 2
|
|
276
|
+
path, opts = _split_args(argv)
|
|
277
|
+
if not path:
|
|
278
|
+
print("artifact rules need a manifest path, e.g. builtin:artifact:manifest artifacts.json")
|
|
279
|
+
return 2
|
|
280
|
+
if not os.path.exists(path):
|
|
281
|
+
print(f"manifest not found: {path}")
|
|
282
|
+
return 2
|
|
283
|
+
|
|
284
|
+
red = {True: 2, False: 3}
|
|
285
|
+
if rule == "orphan":
|
|
286
|
+
hits = rule_orphan(path, opts.get("dir", "."))
|
|
287
|
+
code = red[False]
|
|
288
|
+
else:
|
|
289
|
+
fn = {"manifest": rule_manifest, "stale": rule_stale, "sync": rule_sync,
|
|
290
|
+
"derivatives": rule_derivatives}[rule]
|
|
291
|
+
hits = fn(path)
|
|
292
|
+
code = red[rule in ("manifest", "sync", "derivatives")]
|
|
293
|
+
|
|
294
|
+
if not hits:
|
|
295
|
+
print(f"[{rule}] clean")
|
|
296
|
+
return 0
|
|
297
|
+
limit = 30
|
|
298
|
+
for h in hits[:limit]:
|
|
299
|
+
print(h)
|
|
300
|
+
if len(hits) > limit:
|
|
301
|
+
print(f"... 另有 {len(hits) - limit} 条未列出(见台账 evidence / 收窄 --dir)")
|
|
302
|
+
print(f"[{rule}] {len(hits)} finding(s) (exit {code})")
|
|
303
|
+
return code
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
"""prose plugin: checks for Chinese technical writing.
|
|
2
|
+
|
|
3
|
+
Each rule is a command with its own exit code, so the gate only has to read
|
|
4
|
+
`exit:0`. That keeps the engine domain-free and lets anyone add a rule without
|
|
5
|
+
touching core.
|
|
6
|
+
|
|
7
|
+
Exit codes: 0 = clean, 2 = blocking finding (red), 3 = advisory finding (yellow).
|
|
8
|
+
"""
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import argparse
|
|
12
|
+
import os
|
|
13
|
+
import re
|
|
14
|
+
import sys
|
|
15
|
+
from typing import List, Tuple
|
|
16
|
+
|
|
17
|
+
SKIP_PREFIX = ("#", ">", "|", "-", "!", "```", "$$", "$", "注:", "图注", "虚线", "实线", "点线")
|
|
18
|
+
SENT_SPLIT = re.compile(r"(?<=[。;!?])")
|
|
19
|
+
|
|
20
|
+
CITE = re.compile(r"\[\d+\]|([^)]*\d{4}[^)]*)")
|
|
21
|
+
CHART = re.compile(r"§\s*\d|第\s*\d+\s*节|图\s*\d|表\s*\d|公式|表\s*注")
|
|
22
|
+
STAT = re.compile(r"p\s*[=<>≈]|p\([A-Za-z]+\)\s*[=<>≈]|\b\d+\s*/\s*\d+\b|n\s*=|±|"
|
|
23
|
+
r"\bHolm\b|\bBH\b|\bpp\b|seed|划分|基线|对照组|"
|
|
24
|
+
r"\bS\d+[a-z]?\b|split\s*\d|\d[\d,]*\s*(窗|块|个|条|张|组)")
|
|
25
|
+
SCOPE_WORD = re.compile(
|
|
26
|
+
r"实测|复算|协议|消融|逐\s*(组|单元|cell)|全池|均值|中位|批次|脚本|路径|本文|我们|口径|汇报"
|
|
27
|
+
r"|惯例|定义|设定|划分|seed|基线")
|
|
28
|
+
# 意味着 is ordinary Chinese prose, not a claim verb
|
|
29
|
+
CLAIM = re.compile(r"(表明|说明|证明|取决于)")
|
|
30
|
+
# "本文/我们 ... 首次/首创" or "首次/首创 + 首创类动词" -- descriptive uses of 首次/唯一
|
|
31
|
+
# (first drop below 0.8, the only negative gap) are not priority claims and must not fire.
|
|
32
|
+
ABS_SELF = re.compile(
|
|
33
|
+
r"(本文|我们|本工作|本研究|本方法)\s*[^,。;]{0,10}"
|
|
34
|
+
r"(首次|首创|最先|率先|前所未有)")
|
|
35
|
+
ABS_VERB = re.compile(
|
|
36
|
+
r"(首次|首创|最先|率先|前所未有)\s*"
|
|
37
|
+
r"(提出|实现|证明|报道|引入|建立|完成|构建|给出)")
|
|
38
|
+
ABS_SCOPE = re.compile(r"(检索日期|query|本协议|范围内|据我们|目前可检索|就本文所见)")
|
|
39
|
+
NUMSTAT = re.compile(r"(ρ|r\s*=|R²|相关系数|\d+\.?\d*\s*%)|"
|
|
40
|
+
r"((提升|降低|下降|增加|减少|高出|低于|降幅|增益|误差)\s*[约]?\s*[+\-−]?\d)")
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _extra() -> "re.Pattern | None":
|
|
44
|
+
"""Domain evidence markers the user adds: EVIDGATE_EVIDENCE='pat1|pat2'."""
|
|
45
|
+
raw = os.environ.get("EVIDGATE_EVIDENCE", "").strip()
|
|
46
|
+
if not raw:
|
|
47
|
+
return None
|
|
48
|
+
try:
|
|
49
|
+
return re.compile(raw)
|
|
50
|
+
except re.error:
|
|
51
|
+
return None
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def _has(pats, s: str) -> bool:
|
|
55
|
+
return any(p.search(s) for p in pats if p is not None)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def evidence_any(sentence: str) -> bool:
|
|
59
|
+
"""Does this sentence show any trace of evidence at all (citation, chart, stat, %)."""
|
|
60
|
+
pats = [CITE, CHART, STAT, SCOPE_WORD, _extra(), re.compile(r"%")]
|
|
61
|
+
return _has(pats, sentence)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def evidence_for_numbers(sentence: str) -> bool:
|
|
65
|
+
"""Same, but a bare % does not count -- otherwise numeral_scope can never fire."""
|
|
66
|
+
return _has([CITE, CHART, STAT, SCOPE_WORD, _extra()], sentence)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def sentences(text: str) -> List[Tuple[int, str]]:
|
|
70
|
+
out = []
|
|
71
|
+
for ln, line in enumerate(text.splitlines(), 1):
|
|
72
|
+
s = line.strip()
|
|
73
|
+
if not s or s.startswith(SKIP_PREFIX):
|
|
74
|
+
continue
|
|
75
|
+
for piece in SENT_SPLIT.split(s):
|
|
76
|
+
piece = piece.strip()
|
|
77
|
+
if len(piece) > 12:
|
|
78
|
+
out.append((ln, piece))
|
|
79
|
+
return out
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def body(text: str) -> str:
|
|
83
|
+
for marker in ("## 参考文献", "# 参考文献"):
|
|
84
|
+
if marker in text:
|
|
85
|
+
return text.split(marker)[0]
|
|
86
|
+
return text
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def rule_denominator(lines: List[Tuple[int, str]]) -> List[str]:
|
|
90
|
+
print(f"正文句 {len(lines)}")
|
|
91
|
+
return []
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def rule_absolutes(lines: List[Tuple[int, str]]) -> List[str]:
|
|
95
|
+
hits = []
|
|
96
|
+
for ln, s in lines:
|
|
97
|
+
m = ABS_SELF.search(s) or ABS_VERB.search(s)
|
|
98
|
+
if m and not ABS_SCOPE.search(s):
|
|
99
|
+
hits.append(f"L{ln} [{m.group(0)[:12]}] {s[:70]}")
|
|
100
|
+
return hits
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def rule_uncited(lines: List[Tuple[int, str]]) -> List[str]:
|
|
104
|
+
hits = []
|
|
105
|
+
for ln, s in lines:
|
|
106
|
+
if CLAIM.search(s) and not evidence_any(s):
|
|
107
|
+
hits.append(f"L{ln} {s[:70]}")
|
|
108
|
+
return hits
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def rule_numeral_scope(lines: List[Tuple[int, str]]) -> List[str]:
|
|
112
|
+
hits = []
|
|
113
|
+
for ln, s in lines:
|
|
114
|
+
# no length threshold: a short sentence carrying a bare percentage is exactly
|
|
115
|
+
# the case that slips through when the rule "looks" like it is working.
|
|
116
|
+
if NUMSTAT.search(s) and not evidence_for_numbers(s):
|
|
117
|
+
hits.append(f"L{ln} {s[:70]}")
|
|
118
|
+
return hits
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def rule_citation_chain(text: str) -> List[str]:
|
|
122
|
+
"""Every [n] in the body must exist in the reference list, and vice versa."""
|
|
123
|
+
b = body(text)
|
|
124
|
+
refs = text[len(b):]
|
|
125
|
+
cited = {int(x) for x in re.findall(r"\[(\d+)\]", b)}
|
|
126
|
+
listed = {int(x) for x in re.findall(r"^\s*\[(\d+)\]", refs, re.M)}
|
|
127
|
+
if not listed:
|
|
128
|
+
listed = {int(x) for x in re.findall(r"\[(\d+)\]", refs)}
|
|
129
|
+
if cited and not listed:
|
|
130
|
+
# the bibliography uses another scheme (author-year, "1." numbering, EndNote
|
|
131
|
+
# paste). Saying 38 times "reference missing" would be a false alarm barrage;
|
|
132
|
+
# say once that the scheme was not recognised.
|
|
133
|
+
return [f"文献表制式未被识别:正文有 {len(cited)} 处 [n] 编号,但文献表里没有 [n]。"
|
|
134
|
+
f"本规则只支持编号制;该文献表可能是作者-年份制或其他格式。"]
|
|
135
|
+
missing = sorted(cited - listed)
|
|
136
|
+
unused = sorted(listed - cited)
|
|
137
|
+
hits = [f"正文引用 [{n}] 在文献表中不存在" for n in missing]
|
|
138
|
+
hits += [f"文献表 [{n}] 正文未引用" for n in unused]
|
|
139
|
+
return hits
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
RULES = {
|
|
143
|
+
"denominator": (rule_denominator, 0),
|
|
144
|
+
"absolutes": (rule_absolutes, 2),
|
|
145
|
+
"uncited": (rule_uncited, 3),
|
|
146
|
+
"numeral_scope": (rule_numeral_scope, 3),
|
|
147
|
+
"citation_chain": (rule_citation_chain, 2),
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def scan(path: str, rule: str, quiet: bool = False) -> int:
|
|
152
|
+
fn, red_code = RULES[rule]
|
|
153
|
+
with open(path, encoding="utf-8") as fh:
|
|
154
|
+
text = fh.read()
|
|
155
|
+
lines = sentences(body(text))
|
|
156
|
+
hits = fn(text) if rule == "citation_chain" else fn(lines)
|
|
157
|
+
if not quiet:
|
|
158
|
+
for h in hits:
|
|
159
|
+
print(h)
|
|
160
|
+
if rule == "denominator":
|
|
161
|
+
return 0
|
|
162
|
+
if not hits:
|
|
163
|
+
print(f"[{rule}] clean ({len(lines)} sentences scanned)")
|
|
164
|
+
return 0
|
|
165
|
+
print(f"[{rule}] {len(hits)} finding(s) (exit {red_code})")
|
|
166
|
+
return red_code
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def main(argv=None) -> int:
|
|
170
|
+
ap = argparse.ArgumentParser(prog="evidgate prose")
|
|
171
|
+
sub = ap.add_subparsers(dest="cmd", required=True)
|
|
172
|
+
s = sub.add_parser("scan", help="run one rule against a markdown draft")
|
|
173
|
+
s.add_argument("--file", required=True)
|
|
174
|
+
s.add_argument("--rule", required=True, choices=sorted(RULES))
|
|
175
|
+
s.add_argument("--quiet", action="store_true")
|
|
176
|
+
a = ap.parse_args(argv)
|
|
177
|
+
return scan(a.file, a.rule, a.quiet)
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
if __name__ == "__main__":
|
|
181
|
+
sys.exit(main())
|