| """Line-level footer / boilerplate stripping (Stage 0.5). |
| |
| Removes reprint / OCR footer lines that wrap the genuine old text -- URLs, "printed |
| in the United States of America", "all rights reserved", photocopy / print-on-demand |
| colophons, ISBN lines, bare page numbers, library stamps -- WITHOUT touching the |
| book body. |
| |
| Design: |
| - A line is removed only if a footer pattern matches AND the line is "footer-like": |
| either short (< FOOTER_MAX_LINE_CHARS) or the match covers most of the line. This |
| keeps a long prose sentence that merely contains a URL mid-line. |
| - After the line pass, if a document would lose more than FOOTER_MAX_DOC_LINE_FRAC of |
| its non-empty lines, we KEEP it unstripped and flag it (guards against a pattern |
| going haywire on an unusual document). |
| |
| Pure and testable: no HF, no network. Thresholds come from config. |
| """ |
| import re |
|
|
| import config |
| from filter_lib import FORMAT_TELL_PATTERNS |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| FOOTER_LINE_PATTERNS = { |
| |
| "pod_preservation_photocopy": r"\bpreservation\s+photocopy\b", |
| "pod_laser": r"\blaser[-\s]?print", |
| "pod_createspace": r"\bcreatespace\b", |
| "pod_lightning": r"\blightning\s+source\b", |
| "pod_acid_free_archival": r"\bacid[-\s]?free\s+archival\b", |
| "pod_ansi_paper": r"\bansi(?:/niso)?\s+z39\.48\b", |
| "pod_made_in_usa": r"\bmade\s+in\s+the\s+usa\b", |
| |
| "repro_this_was_produced": r"\bthis\s+(?:book|edition|volume)\s+is\s+a\s+preservation\b", |
| "repro_facsimile": r"\bauthori[sz]ed\s+facsimile\b|\buniversity\s+microfilms\b", |
| "digitized_by_google": r"\bdigiti[sz]ed\s+by\s+google\b", |
| |
| "lib_ex_libris": r"^\s*ex\s*libris\b", |
| |
| "page_num_bare": r"^\s*[\[\(\-–—]*\s*(?:p\.?\s*)?\d{1,4}\s*[\]\)\-–—]*\s*$", |
| } |
|
|
| |
| |
| _SAFE_FORMAT_TELLS = {k: v for k, v in FORMAT_TELL_PATTERNS.items() if k != "email_addr"} |
| _ALL_PATTERNS = {**_SAFE_FORMAT_TELLS, **FOOTER_LINE_PATTERNS} |
| _COMPILED = {name: re.compile(pat, re.IGNORECASE) for name, pat in _ALL_PATTERNS.items()} |
|
|
| |
| _NOISE_LINE_RE = re.compile(r"^[\W\d_]+$") |
|
|
|
|
| def _line_is_footer(line): |
| """Return the name of the first footer pattern that flags this line, or None. |
| |
| A pattern only counts if the line is footer-like: short, or the match spans |
| most of the line's non-space characters (so mid-prose URLs are spared). |
| """ |
| stripped = line.strip() |
| if not stripped: |
| return None |
|
|
| short = len(stripped) <= config.FOOTER_MAX_LINE_CHARS |
| |
| if short and _NOISE_LINE_RE.match(stripped) and len(stripped) <= 12: |
| return "ocr_noise" |
|
|
| for name, rx in _COMPILED.items(): |
| m = rx.search(stripped) |
| if not m: |
| continue |
| if short: |
| return name |
| |
| coverage = (m.end() - m.start()) / max(len(stripped), 1) |
| if coverage >= 0.6: |
| return name |
| return None |
|
|
|
|
| def strip_footers(text): |
| """Remove footer/boilerplate lines from a document. |
| |
| Returns (clean_text, removed, flagged) where: |
| removed : list of (pattern_name, line_text) actually removed |
| flagged : True if the doc hit the removed-fraction cap and was KEPT unstripped |
| """ |
| if not isinstance(text, str) or not text: |
| return text, [], False |
|
|
| lines = text.split("\n") |
| n_nonempty = sum(1 for ln in lines if ln.strip()) |
| kept, removed = [], [] |
| for line in lines: |
| name = _line_is_footer(line) |
| if name is None: |
| kept.append(line) |
| else: |
| removed.append((name, line.strip())) |
|
|
| |
| |
| if n_nonempty and (len(removed) / n_nonempty) > config.FOOTER_MAX_DOC_LINE_FRAC: |
| return text, removed, True |
|
|
| clean = "\n".join(kept) |
| |
| clean = re.sub(r"\n{3,}", "\n\n", clean).strip("\n") |
| return clean, removed, False |
|
|