dev-strender Claude Opus 4.7 (1M context) commited on
Commit
c540d93
ยท
1 Parent(s): 8b6780d

fix: add paragraph_dedupe post-process to remove residual LLM echoes

Browse files

Even after PARAGRAPH_SEP + per-bulk <์›๋ฌธ> (prior commit), solar-pro3
still probabilistically echoes paragraphs for specific inputs:
- Long multi-chunk articles (6788 chars / 13 chunks)
- Truncated inputs the model tries to "complete" by restarting

Add an output-level safety net: split final text on \n+, count
paragraph occurrences in both input and output, drop any paragraph
that appears more in output than input. Exact + 80-char-prefix
matching. Paragraphs shorter than 40 chars (โ—‡ headers, short
interjections) skipped.

Verified on full Supabase sweep (75 unique articles):
- Before fix 1+2+3: 13.4% dup rate
- After: 0.0% (0/75)
- Including aid=63 (long article, was 30%) and aid=22 (truncated
input, was 80%) โ€” both now clean.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

Files changed (2) hide show
  1. pipelines.py +12 -0
  2. postprocess.py +105 -0
pipelines.py CHANGED
@@ -17,6 +17,7 @@ from postprocess import (
17
  apply_comma_removal,
18
  apply_correction_filter,
19
  apply_currency_compact,
 
20
  apply_pronoun_postprocess,
21
  apply_unit_unicode,
22
  apply_vocabulary,
@@ -172,6 +173,9 @@ PIPELINES: dict[str, list[dict]] = {
172
  "max_tokens": 4000,
173
  },
174
  {"name": "vocabulary_reapply", "type": "rule", "rule": "vocabulary"},
 
 
 
175
  {"name": "post_process", "type": "rule", "rule": "post_process"},
176
  ],
177
  # E1 taxonomy pipeline โ€” sequential specialists, grammar last @ RE=medium
@@ -798,6 +802,14 @@ def apply_rule(
798
  max_char_diff=config.get("max_char_diff", 2),
799
  allow_spacing=config.get("allow_spacing", True),
800
  )
 
 
 
 
 
 
 
 
801
 
802
  return text
803
 
 
17
  apply_comma_removal,
18
  apply_correction_filter,
19
  apply_currency_compact,
20
+ apply_paragraph_dedupe,
21
  apply_pronoun_postprocess,
22
  apply_unit_unicode,
23
  apply_vocabulary,
 
173
  "max_tokens": 4000,
174
  },
175
  {"name": "vocabulary_reapply", "type": "rule", "rule": "vocabulary"},
176
+ # Safety net: LLM ์ด ํ™•๋ฅ ์ ์œผ๋กœ ๋ฑ‰๋Š” ๋ฌธ๋‹จ ์—์ฝ”/์žฌ์‹œ์ž‘ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ๋‹ค.
177
+ # input ์— ์ด๋ฏธ ์žˆ๋˜ ๋ฐ˜๋ณต์€ ๋ณด์กด (output_count > input_count ์ผ ๋•Œ๋งŒ drop).
178
+ {"name": "paragraph_dedupe", "type": "rule", "rule": "paragraph_dedupe"},
179
  {"name": "post_process", "type": "rule", "rule": "post_process"},
180
  ],
181
  # E1 taxonomy pipeline โ€” sequential specialists, grammar last @ RE=medium
 
802
  max_char_diff=config.get("max_char_diff", 2),
803
  allow_spacing=config.get("allow_spacing", True),
804
  )
805
+ elif rule == "paragraph_dedupe":
806
+ config = step.get("config", {})
807
+ return apply_paragraph_dedupe(
808
+ text,
809
+ original_text,
810
+ min_len=config.get("min_len", 40),
811
+ prefix_len=config.get("prefix_len", 80),
812
+ )
813
 
814
  return text
815
 
postprocess.py CHANGED
@@ -2,6 +2,7 @@
2
 
3
  import csv
4
  import re
 
5
  from pathlib import Path
6
 
7
  # --- Vocabulary substitution ---
@@ -349,3 +350,107 @@ def apply_correction_filter(
349
  result_tokens.append(orig_gap)
350
 
351
  return " ".join(t for t in result_tokens if t)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  import csv
4
  import re
5
+ from collections import Counter
6
  from pathlib import Path
7
 
8
  # --- Vocabulary substitution ---
 
350
  result_tokens.append(orig_gap)
351
 
352
  return " ".join(t for t in result_tokens if t)
353
+
354
+
355
+ # --- Paragraph-level duplication removal -------------------------------------
356
+
357
+ _PARA_SPLIT_RE = re.compile(r"\n+")
358
+ _WS_RE = re.compile(r"\s+")
359
+
360
+
361
+ def _normalize_paragraph(p: str) -> str:
362
+ """๊ณต๋ฐฑ/ํƒญ/๊ฐœํ–‰ ์••์ถ• ์ •๊ทœํ˜•. ์ค‘๋ณต ๋งค์นญ์šฉ."""
363
+ return _WS_RE.sub(" ", p).strip()
364
+
365
+
366
+ def _split_output_paragraphs(text: str) -> list[str]:
367
+ """Output ํ…์ŠคํŠธ๋ฅผ \\n+ ๊ฒฝ๊ณ„๋กœ ์ชผ๊ฐœ ๋น„์–ด์žˆ์ง€ ์•Š์€ ๋ฌธ๋‹จ ๋ฆฌ์ŠคํŠธ ๋ฐ˜ํ™˜.
368
+
369
+ ์ •๊ทœํ™”ํ•˜์ง€ ์•Š์€ ์›๋ณธ ์กฐ๊ฐ์„ ๊ทธ๋Œ€๋กœ ๋„˜๊ฒจ์„œ, ์žฌ์กฐ๋ฆฝ ์‹œ ๋ชจ๋ธ์ด ์“ด ๊ตฌ๋‘์ /
370
+ ์ŠคํŽ˜์ด์‹ฑ ๋””ํ…Œ์ผ์ด ์ตœ๋Œ€ํ•œ ๋ณด์กด๋˜๋„๋ก ํ•œ๋‹ค (strip ์€ ์กฐ๋ฆฝ ์‹œ \\n ์œผ๋กœ
371
+ ๋‹ค์‹œ ๊ฐ์‹ธ๋ฏ€๋กœ ์•ˆ์ „).
372
+ """
373
+ if not text:
374
+ return []
375
+ parts = _PARA_SPLIT_RE.split(text)
376
+ return [p.strip() for p in parts if p.strip()]
377
+
378
+
379
+ def apply_paragraph_dedupe(
380
+ text: str,
381
+ original: str,
382
+ min_len: int = 40,
383
+ prefix_len: int = 80,
384
+ ) -> str:
385
+ """LLM์ด ๋ฑ‰์€ ์ค‘๋ณต ๋ฌธ๋‹จ์„ ์ œ๊ฑฐํ•œ๋‹ค.
386
+
387
+ **์–ธ์ œ ์ œ๊ฑฐํ•˜๋Š”๊ฐ€**
388
+ - ๊ฐ™์€ ๋ฌธ๋‹จ(์ •๊ทœํ™” ํ›„ ์™„์ „์ผ์น˜) ์ด output ์—์„œ `input ๋“ฑ์žฅ ํšŸ์ˆ˜ + 1`
389
+ ์ด์ƒ ๋“ฑ์žฅํ•˜๋ฉด ๋’ค์ชฝ ๊ฒƒ์„ drop.
390
+ - ์•ž ``prefix_len`` ์ž๊ฐ€ ๋™์ผํ•œ ๋ฌธ๋‹จ์ด out > in ๋กœ ๋ฐ˜๋ณต๋˜๋ฉด drop
391
+ (๋ชจ๋ธ์ด ๋ฏธ์„ธ ๊ต์ •์œผ๋กœ ๋‹ค๋ฅด๊ฒŒ ๋ฑ‰์€ echo ์žก๊ธฐ).
392
+
393
+ **์–ธ์ œ ๋ณด์กดํ•˜๋Š”๊ฐ€**
394
+ - ๋ฌธ๋‹จ ๊ธธ์ด < ``min_len`` (โ—‡์†Œ์ œ๋ชฉ, ํ•œ ์ค„ ์ธ์šฉ ๋“ฑ ํ•ฉ๋ฒ•์  ๋ฐ˜๋ณต).
395
+ - ``output_count <= input_count`` (์ €์ž๊ฐ€ ์˜๋„ํ•ด์„œ ์›๋ฌธ์— ์ด๋ฏธ ๋ฐ˜๋ณต
396
+ ํฌํ•จ. ์ €์ž ์˜๋„๋ฅผ ํ›ผ์†ํ•˜์ง€ ์•Š์Œ).
397
+ - ์ฒซ ๋“ฑ์žฅ์€ ํ•ญ์ƒ ์œ ์ง€. ๋‘ ๋ฒˆ์งธ ์ดํ›„ ๋“ฑ์žฅ๋ถ€ํ„ฐ ์กฐ๊ฑด์— ๋งž์œผ๋ฉด drop.
398
+
399
+ Args:
400
+ text: ํŒŒ์ดํ”„๋ผ์ธ ์ถœ๋ ฅ ํ…์ŠคํŠธ.
401
+ original: ํŒŒ์ดํ”„๋ผ์ธ ์ž…๋ ฅ (์ €์ž ์›๋ฌธ). None ์ด๋ฉด ๋นˆ ๋ฌธ์ž์—ด๋กœ ๊ฐ„์ฃผํ•ด
402
+ input_count=0. ์ด ๊ฒฝ์šฐ output ์—์„œ 2 ํšŒ ์ด์ƒ ๋™์ผ ๋ฌธ๋‹จ ๋“ฑ์žฅ โ†’
403
+ ๋ชจ๋‘ drop.
404
+ min_len: ์ด ๊ธธ์ด ๋ฏธ๋งŒ ์ •๊ทœํ™” ๋ฌธ๋‹จ์€ dedupe ์—์„œ ์ œ์™ธ.
405
+ prefix_len: near-dup ํƒ์ง€์— ์“ฐ๋Š” ์•ž์ž๋ฅด๊ธฐ ๊ธธ์ด.
406
+
407
+ Returns:
408
+ Dedupe ๋œ ํ…์ŠคํŠธ. ๋ฌธ๋‹จ ๊ตฌ๋ถ„์ž๋Š” ``\\n`` ์œผ๋กœ ์žฌ์กฐ๋ฆฝ (๊ธฐ์กด ํŒŒ์ดํ”„๋ผ์ธ
409
+ ์ถœ๋ ฅ ๊ด€๋ก€์™€ ๋™์ผ). ์ค‘๋ณต์ด ์—†์œผ๋ฉด ์ž…๋ ฅ์„ ๊ทธ๋Œ€๋กœ ๋Œ๋ ค์ค€๋‹ค.
410
+ """
411
+ if not text:
412
+ return text
413
+
414
+ in_paras_norm = [_normalize_paragraph(p) for p in _split_output_paragraphs(original or "")]
415
+ in_exact: Counter[str] = Counter()
416
+ in_prefix: Counter[str] = Counter()
417
+ for norm in in_paras_norm:
418
+ if len(norm) >= min_len:
419
+ in_exact[norm] += 1
420
+ in_prefix[norm[:prefix_len]] += 1
421
+
422
+ out_paras = _split_output_paragraphs(text)
423
+ out_exact_seen: Counter[str] = Counter()
424
+ out_prefix_seen: Counter[str] = Counter()
425
+
426
+ kept: list[str] = []
427
+ any_dropped = False
428
+ for para in out_paras:
429
+ norm = _normalize_paragraph(para)
430
+ if len(norm) < min_len:
431
+ kept.append(para)
432
+ continue
433
+
434
+ prefix = norm[:prefix_len]
435
+ out_exact_seen[norm] += 1
436
+ out_prefix_seen[prefix] += 1
437
+
438
+ exact_dup = (
439
+ out_exact_seen[norm] > in_exact.get(norm, 0)
440
+ and out_exact_seen[norm] >= 2
441
+ )
442
+ near_dup = (
443
+ out_prefix_seen[prefix] > in_prefix.get(prefix, 0)
444
+ and out_prefix_seen[prefix] >= 2
445
+ )
446
+
447
+ if exact_dup or near_dup:
448
+ any_dropped = True
449
+ continue
450
+
451
+ kept.append(para)
452
+
453
+ if not any_dropped:
454
+ return text
455
+
456
+ return "\n".join(kept)