-
Notifications
You must be signed in to change notification settings - Fork 277
Expand file tree
/
Copy pathapply_cuts.py
More file actions
269 lines (226 loc) · 8.6 KB
/
Copy pathapply_cuts.py
File metadata and controls
269 lines (226 loc) · 8.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/usr/bin/env python3
"""Apply adversarial edit cuts to chapter files.
Usage:
python apply_cuts.py 12 # apply cuts to ch 12
python apply_cuts.py all # apply cuts to all chapters
python apply_cuts.py all --types OVER-EXPLAIN REDUNDANT # filter by type
python apply_cuts.py all --min-fat 17 # only chapters with >=17% fat
python apply_cuts.py all --dry-run # show what would be cut
"""
import argparse
import json
import re
import sys
from pathlib import Path
BASE = Path(__file__).resolve().parent
CHAPTERS_DIR = BASE / "chapters"
EDIT_LOGS_DIR = BASE / "edit_logs"
VALID_TYPES = {"OVER-EXPLAIN", "REDUNDANT", "FAT", "TELL", "STRUCTURAL", "GENERIC"}
MIN_QUOTE_LEN = 25
def load_cuts(chapter_num: int) -> dict | None:
"""Load the cuts JSON for a given chapter number. Returns None if missing."""
cuts_file = EDIT_LOGS_DIR / f"ch{chapter_num:02d}_cuts.json"
if not cuts_file.exists():
return None
try:
data = json.loads(cuts_file.read_text(encoding="utf-8"))
return data
except (json.JSONDecodeError, OSError) as exc:
print(f" WARNING: failed to parse {cuts_file.name}: {exc}")
return None
def chapter_path(chapter_num: int) -> Path:
return CHAPTERS_DIR / f"ch_{chapter_num:02d}.md"
def find_and_remove(text: str, quote: str) -> tuple[str, bool, str]:
"""Try to find and remove quote from text.
Returns (new_text, success, failure_reason).
"""
# Exact match first
count = text.count(quote)
if count == 1:
text = text.replace(quote, "", 1)
return text, True, ""
if count > 1:
return text, False, f"ambiguous ({count} matches)"
# Normalised whitespace match: collapse runs of whitespace in both the
# text and the quote to single spaces, search, then map back to the
# original span.
ws = re.compile(r"\s+")
norm_quote = ws.sub(" ", quote).strip()
if len(norm_quote) < MIN_QUOTE_LEN:
return text, False, "quote too short after normalisation"
# Build a regex that matches the quote with flexible whitespace
# Escape each token and join with \s+
tokens = norm_quote.split(" ")
pattern = r"\s+".join(re.escape(t) for t in tokens)
matches = list(re.finditer(pattern, text))
if len(matches) == 1:
m = matches[0]
text = text[:m.start()] + text[m.end():]
return text, True, ""
if len(matches) > 1:
return text, False, f"ambiguous after ws-norm ({len(matches)} matches)"
return text, False, "not found"
def collapse_blank_lines(text: str) -> str:
"""Collapse runs of 3+ newlines down to 2 (one blank line)."""
return re.sub(r"\n{3,}", "\n\n", text)
def discover_chapters() -> list[int]:
"""Return sorted list of chapter numbers that have both a chapter file and a cuts file."""
nums = set()
for p in EDIT_LOGS_DIR.glob("ch*_cuts.json"):
m = re.match(r"ch(\d+)_cuts\.json", p.name)
if m:
nums.add(int(m.group(1)))
return sorted(nums)
def process_chapter(
chapter_num: int,
type_filter: set[str] | None,
min_fat: int,
dry_run: bool,
) -> dict:
"""Process cuts for one chapter. Returns stats dict."""
stats = {"applied": 0, "failed": 0, "skipped": 0, "words_removed": 0, "error": None}
label = f"ch{chapter_num:02d}"
# Load cuts
data = load_cuts(chapter_num)
if data is None:
stats["error"] = "no cuts file"
return stats
fat_pct = data.get("overall_fat_percentage", 0)
if fat_pct < min_fat:
stats["skipped"] = len(data.get("cuts", []))
stats["error"] = f"fat {fat_pct}% < threshold {min_fat}%"
return stats
cuts = data.get("cuts", [])
if not cuts:
stats["error"] = "no cuts in file"
return stats
# Load chapter text
ch_path = chapter_path(chapter_num)
if not ch_path.exists():
stats["error"] = f"{ch_path.name} not found"
return stats
text = ch_path.read_text(encoding="utf-8")
original_words = len(text.split())
for cut in cuts:
quote = cut.get("quote", "")
cut_type = cut.get("type", "UNKNOWN")
reason = cut.get("reason", "")
# Filter by type
if type_filter and cut_type not in type_filter:
stats["skipped"] += 1
continue
# Skip short quotes
if len(quote.strip()) < MIN_QUOTE_LEN:
stats["skipped"] += 1
if not dry_run:
print(f" SKIP [{cut_type}] quote too short ({len(quote.strip())} chars)")
continue
if dry_run:
preview = quote[:80].replace("\n", "\\n")
if len(quote) > 80:
preview += "..."
words = len(quote.split())
print(f" CUT [{cut_type}] ~{words}w: {preview}")
print(f" reason: {reason}")
stats["applied"] += 1
stats["words_removed"] += words
continue
# Apply the cut
new_text, success, fail_reason = find_and_remove(text, quote)
if success:
words_cut = len(quote.split())
stats["applied"] += 1
stats["words_removed"] += words_cut
text = new_text
preview = quote[:60].replace("\n", "\\n")
if len(quote) > 60:
preview += "..."
print(f" CUT [{cut_type}] ~{words_cut}w: {preview}")
else:
stats["failed"] += 1
preview = quote[:60].replace("\n", "\\n")
if len(quote) > 60:
preview += "..."
print(f" FAIL [{cut_type}] {fail_reason}: {preview}")
# Write back
if not dry_run and stats["applied"] > 0:
text = collapse_blank_lines(text)
ch_path.write_text(text, encoding="utf-8")
new_words = len(text.split())
print(f" SAVED {ch_path.name}: {original_words} -> {new_words} words")
return stats
def main():
parser = argparse.ArgumentParser(
description="Apply adversarial edit cuts to chapter files.",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=(
"Examples:\n"
" python apply_cuts.py 12\n"
" python apply_cuts.py all --types OVER-EXPLAIN REDUNDANT\n"
" python apply_cuts.py all --min-fat 17\n"
" python apply_cuts.py all --dry-run\n"
),
)
parser.add_argument(
"chapter",
help="Chapter number (e.g. 12) or 'all' to process every chapter.",
)
parser.add_argument(
"--types",
nargs="+",
metavar="TYPE",
choices=sorted(VALID_TYPES),
help=f"Only apply cuts of these types. Choices: {', '.join(sorted(VALID_TYPES))}",
)
parser.add_argument(
"--min-fat",
type=int,
default=0,
metavar="PCT",
help="Only process chapters with overall_fat_percentage >= this value.",
)
parser.add_argument(
"--dry-run",
action="store_true",
help="Show what would be cut without modifying files.",
)
args = parser.parse_args()
type_filter = set(args.types) if args.types else None
# Determine which chapters to process
if args.chapter.lower() == "all":
chapters = discover_chapters()
if not chapters:
print("No cuts files found in edit_logs/")
sys.exit(1)
else:
try:
chapters = [int(args.chapter)]
except ValueError:
parser.error(f"Invalid chapter: {args.chapter!r} (use a number or 'all')")
# Banner
mode = "DRY RUN" if args.dry_run else "APPLY"
type_info = f", types={','.join(sorted(type_filter))}" if type_filter else ""
fat_info = f", min-fat={args.min_fat}%" if args.min_fat > 0 else ""
print(f"=== apply_cuts [{mode}] chapters={len(chapters)}{type_info}{fat_info} ===\n")
# Aggregate stats
totals = {"applied": 0, "failed": 0, "skipped": 0, "words_removed": 0}
for ch_num in chapters:
label = f"ch{ch_num:02d}"
print(f"--- {label} ---")
stats = process_chapter(ch_num, type_filter, args.min_fat, args.dry_run)
if stats["error"]:
print(f" {stats['error']}")
for k in totals:
totals[k] += stats[k]
print()
# Summary
print("=" * 50)
print(f"Applied: {totals['applied']} | Failed: {totals['failed']} | Skipped: {totals['skipped']}")
print(f"Words removed: ~{totals['words_removed']}")
if args.dry_run:
print("(dry run — no files were modified)")
print("=" * 50)
if totals["failed"] > 0:
sys.exit(2)
if __name__ == "__main__":
main()