M2: extraction quality — fallback extractors, thin-content detection, result cache
- extractor: trafilatura (precision) -> trafilatura (recall) -> readability-lxml fallback chain; longest result wins; 'thin' flag below 200 chars - orchestrator: Redis-backed scrape-result cache keyed by URL (cache hit skips render entirely); thin/empty results are never cached - /health reports orchestrator Redis reachability Verified: HN + synthetic thin/empty/boilerplate pages; cache hit path via fakeredis. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
parent
e2b329b2bb
commit
56e7fa16dd
8 changed files with 156 additions and 32 deletions
|
|
@ -1,37 +1,83 @@
|
|||
"""Trafilatura-based extraction. M1: primary extractor only; M2 adds fallbacks."""
|
||||
"""Mechanical extraction: Trafilatura primary, readability-lxml fallback.
|
||||
|
||||
No inference anywhere — deterministic library-based extraction only. A result
|
||||
is "thin" when it's shorter than MIN_TEXT_CHARS; thin/empty primary output
|
||||
triggers the fallback chain, and the longest result wins.
|
||||
"""
|
||||
|
||||
import re
|
||||
|
||||
import trafilatura
|
||||
from readability import Document
|
||||
from lxml import html as lxml_html
|
||||
|
||||
MIN_TEXT_CHARS = 200
|
||||
|
||||
|
||||
def _trafilatura_extract(html: str, url: str, favor_recall: bool) -> str:
|
||||
return (
|
||||
trafilatura.extract(
|
||||
html,
|
||||
url=url,
|
||||
include_tables=True,
|
||||
include_links=False,
|
||||
include_comments=False,
|
||||
favor_recall=favor_recall,
|
||||
)
|
||||
or ""
|
||||
)
|
||||
|
||||
|
||||
def _readability_extract(html: str) -> str:
|
||||
try:
|
||||
doc = Document(html)
|
||||
tree = lxml_html.fromstring(doc.summary())
|
||||
text = tree.text_content()
|
||||
# Collapse the whitespace soup text_content() produces.
|
||||
text = re.sub(r"[ \t]+", " ", text)
|
||||
text = re.sub(r"\n\s*\n\s*", "\n\n", text)
|
||||
return text.strip()
|
||||
except Exception:
|
||||
return ""
|
||||
|
||||
|
||||
def _metadata(html: str, url: str) -> tuple[str | None, dict]:
|
||||
meta = trafilatura.extract_metadata(html, default_url=url)
|
||||
if meta is None:
|
||||
return None, {}
|
||||
fields = {
|
||||
k: v
|
||||
for k, v in {
|
||||
"author": meta.author,
|
||||
"date": meta.date,
|
||||
"sitename": meta.sitename,
|
||||
"description": meta.description,
|
||||
}.items()
|
||||
if v
|
||||
}
|
||||
return meta.title, fields
|
||||
|
||||
|
||||
def extract_content(html: str, url: str) -> dict:
|
||||
text = trafilatura.extract(
|
||||
html,
|
||||
url=url,
|
||||
include_tables=True,
|
||||
include_links=False,
|
||||
include_comments=False,
|
||||
favor_recall=False,
|
||||
)
|
||||
meta = trafilatura.extract_metadata(html, default_url=url)
|
||||
candidates = [
|
||||
("trafilatura", lambda: _trafilatura_extract(html, url, favor_recall=False)),
|
||||
("trafilatura_recall", lambda: _trafilatura_extract(html, url, favor_recall=True)),
|
||||
("readability", lambda: _readability_extract(html)),
|
||||
]
|
||||
|
||||
metadata = {}
|
||||
title = None
|
||||
if meta is not None:
|
||||
title = meta.title
|
||||
metadata = {
|
||||
k: v
|
||||
for k, v in {
|
||||
"author": meta.author,
|
||||
"date": meta.date,
|
||||
"sitename": meta.sitename,
|
||||
"description": meta.description,
|
||||
}.items()
|
||||
if v
|
||||
}
|
||||
best_name, best_text = "none", ""
|
||||
for name, fn in candidates:
|
||||
text = fn()
|
||||
if len(text) > len(best_text):
|
||||
best_name, best_text = name, text
|
||||
if len(best_text) >= MIN_TEXT_CHARS and name == "trafilatura":
|
||||
break # primary extractor produced substantial content; done
|
||||
|
||||
title, metadata = _metadata(html, url)
|
||||
return {
|
||||
"title": title,
|
||||
"text": text or "",
|
||||
"text": best_text,
|
||||
"metadata": metadata,
|
||||
"extractor_used": "trafilatura" if text else "none",
|
||||
"extractor_used": best_name if best_text else "none",
|
||||
"thin": len(best_text) < MIN_TEXT_CHARS,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
fastapi>=0.115
|
||||
uvicorn[standard]>=0.32
|
||||
trafilatura>=1.12
|
||||
readability-lxml>=0.8
|
||||
lxml>=5.0
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue