pagepiper/scripts/text_clean.py
pyr0ball f941ebdeeb feat: add ODT and Apple Pages document support, wire DOCX into UI
Extends Pagepiper's document shelving pipeline (renamed from "ingest" —
see below) to cover the formats most likely to appear in a real-world
engineering document corpus, prompted by scoping a STERIS licensing pitch
that needs DOCX/ODT coverage.

- Rename the ingest pipeline to "shelve" throughout (scripts/, app/api,
  tests, docs, frontend). "Glean" (Turnstone's term) was considered and
  rejected — that's a harvest metaphor for log/knowledge extraction,
  not a fit for documents entering a library. Documented as a general
  CF naming principle in the org-level CLAUDE.md.
- Wire DOCX into the upload/scan UI, README, and docs — the extraction
  logic (heading-based chunking, table serialization) already existed
  but wasn't exposed to users or covered by tests.
- Add ODT support via odfpy, mirroring DOCX's chunking strategy.
- Add Apple Pages support via headless LibreOffice conversion to ODT.
  No maintained Python library parses the IWA format directly; libreoffice
  bundles libetonyek, the only real open-source Pages parser. Adds
  libreoffice-writer to the Docker image (~300-400MB) for this.
- 24 new/updated tests across shelve_docx, shelve_odt, and shelve_pages;
  full suite (72 tests) passing.

Known gaps not addressed here: no Windchill/DocPortal connector exists
yet (metadata-only PowerShell recon only), Excel/.xlsx is unsupported,
and circuitforge_core.tasks.dispatch_task does not currently exist in
circuitforge-core — cf-orch dispatch is dead code, always falling
through to local BackgroundTasks. See
circuitforge-plans/pagepiper/superpowers/plans/2026-07-10-steris-licensing-pitch.md
for the full writeup.
2026-07-10 13:58:43 -07:00

72 lines
2.5 KiB
Python

# scripts/text_clean.py
"""
Shared text-cleaning utilities for shelve pipelines.
Removes boilerplate lines injected by ebook converters, piracy watermarks,
and other non-content artifacts before chunks are stored or embedded.
"""
from __future__ import annotations
import re
# Lines that match any of these patterns are dropped entirely.
# Each pattern is matched against the stripped line (case-insensitive).
_LINE_DROP_PATTERNS: list[re.Pattern] = [
# ABC Amber converter family
re.compile(r'generated by abc amber', re.IGNORECASE),
re.compile(r'processtext\.com', re.IGNORECASE),
# Calibre / sigil metadata lines
re.compile(r'calibre \d+\.\d+', re.IGNORECASE),
# Standalone URLs (line is just a URL, no surrounding prose)
re.compile(r'^https?://\S+$'),
# Common piracy / file-sharing watermarks
re.compile(r'www\.\w+\.(com|net|org)/\S*book', re.IGNORECASE),
re.compile(r'downloaded from', re.IGNORECASE),
re.compile(r'scanned by', re.IGNORECASE),
re.compile(r'provided by', re.IGNORECASE),
# Page-number-only lines from PDF extraction (e.g. "- 42 -" or "42")
re.compile(r'^\s*-?\s*\d{1,4}\s*-?\s*$'),
]
# Inline substrings to strip from within a line before further processing.
_INLINE_STRIP_PATTERNS: list[re.Pattern] = [
re.compile(r'generated by abc amber \w+ converter,?\s*https?://\S*', re.IGNORECASE),
re.compile(r'https?://www\.processtext\.com/\S*', re.IGNORECASE),
]
def is_artifact_line(line: str) -> bool:
"""Return True if the line is a known conversion artifact and should be dropped."""
stripped = line.strip()
return any(p.search(stripped) for p in _LINE_DROP_PATTERNS)
def clean_line(line: str) -> str:
"""Strip inline converter artifacts from a line, returning the cleaned version."""
for p in _INLINE_STRIP_PATTERNS:
line = p.sub("", line)
return line.strip()
def clean_paragraph(text: str) -> str:
"""Clean a multi-line paragraph: drop artifact lines, strip inline artifacts."""
lines = []
for line in text.splitlines():
if is_artifact_line(line):
continue
cleaned = clean_line(line)
if cleaned:
lines.append(cleaned)
return "\n".join(lines)
def filter_paragraphs(paragraphs: list[str]) -> list[str]:
"""Remove artifact lines from a list of paragraph strings."""
result = []
for para in paragraphs:
if is_artifact_line(para):
continue
cleaned = clean_line(para)
if cleaned and len(cleaned.split()) >= 4:
result.append(cleaned)
return result