Skip to content

Commit 83f41aa

Browse files
vorojarclaude
andcommitted
fix: expand dedup to check all prior lines, not just adjacent
_dedup_lines now checks against all earlier lines instead of only the previous one, catching display math duplicates separated by other content (e.g. "90°" appearing after "D. 90°" with lines between). Also apply dedup at the combined output level across regions. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 8f7685b commit 83f41aa

1 file changed

Lines changed: 23 additions & 7 deletions

File tree

server.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ async def ocr_image_with_layout(image_path: str, merge: bool = True) -> tuple[st
434434
img.close()
435435

436436
combined = "\n\n".join(r["text"] for r in regions if r["text"])
437+
combined = _dedup_lines(combined)
437438
n_calls = len(regions)
438439
logger.info(f"[OCR] TOTAL: {time.time() - t0:.2f}s ({len(raw_regions)} regions, {n_calls} calls, merge={'on' if merge else 'off'})")
439440
return combined, regions
@@ -537,23 +538,38 @@ def _remove_duplicate_display_math(text: str) -> str:
537538

538539

539540
def _dedup_lines(text: str) -> str:
540-
"""Remove lines whose content is a duplicate of an adjacent line (after normalization)."""
541+
"""Remove lines whose normalized content is a substring of any earlier line.
542+
Catches GLM-OCR's duplicate display math even when separated by other lines.
543+
"""
541544
lines = text.split('\n')
542545
if len(lines) <= 1:
543546
return text
544547

545548
result = [lines[0]]
549+
# Keep normalized versions of all accepted lines for fast lookup
550+
seen_norms = [re.sub(r'\s+', '', lines[0])]
551+
546552
for line in lines[1:]:
547-
# Normalize for comparison: strip, remove spaces
548-
prev_norm = re.sub(r'\s+', '', result[-1])
549553
curr_norm = re.sub(r'\s+', '', line)
550-
# Skip if current is empty or exact duplicate of previous
551-
if curr_norm and curr_norm == prev_norm:
554+
if not curr_norm:
555+
result.append(line) # keep blank lines
552556
continue
553-
# Skip if current is a substring of previous (e.g. "15°~20°" within "B. 15°~20°")
554-
if curr_norm and len(curr_norm) > 2 and curr_norm in prev_norm:
557+
558+
# Check if current line is a duplicate/substring of ANY earlier line
559+
is_dup = False
560+
for prev_norm in seen_norms:
561+
if curr_norm == prev_norm:
562+
is_dup = True
563+
break
564+
if len(curr_norm) > 2 and curr_norm in prev_norm:
565+
is_dup = True
566+
break
567+
if is_dup:
555568
continue
569+
556570
result.append(line)
571+
seen_norms.append(curr_norm)
572+
557573
return '\n'.join(result)
558574

559575

0 commit comments

Comments
 (0)