Metadata-Version: 2.4
Name: trafilatura-rs
Version: 0.1.0
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Operating System :: MacOS
Classifier: Operating System :: Microsoft :: Windows
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Rust
Classifier: Topic :: Text Processing :: Markup :: HTML
Classifier: Topic :: Internet :: WWW/HTTP :: Indexing/Search
Requires-Dist: pytest ; extra == 'test'
Requires-Dist: trafilatura==1.11.0 ; extra == 'test'
Requires-Dist: lxml==6.1.3 ; extra == 'test'
Requires-Dist: lxml-html-clean==0.4.6 ; extra == 'test'
Requires-Dist: justext==3.0.2 ; extra == 'test'
Requires-Dist: courlan==1.4.0 ; extra == 'test'
Requires-Dist: htmldate==1.11.0 ; extra == 'test'
Requires-Dist: trafilatura==1.11.0 ; extra == 'trafilatura'
Requires-Dist: lxml==6.1.3 ; extra == 'trafilatura'
Provides-Extra: test
Provides-Extra: trafilatura
Summary: Byte-exact drop-in Rust port of trafilatura 1.11's extract()
Keywords: trafilatura,html,text-extraction,boilerplate-removal,web-scraping,datatrove,rust
Author: Hexay
License: Apache-2.0
Requires-Python: >=3.10
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Homepage, https://github.com/Hexay/trafilatura-rs
Project-URL: Issues, https://github.com/Hexay/trafilatura-rs/issues
Project-URL: Repository, https://github.com/Hexay/trafilatura-rs

# trafilatura-rs

Byte-exact drop-in for trafilatura 1.11's `extract()` (txt output, `include_comments=False`), in Rust: same
output, byte for byte, ~9x less CPU. Pinned to trafilatura 1.11.0 + lxml 6.1.3 / libxml2 2.14.6 (vendored and
statically linked, see `crates/libxml2-sys/PATCHES.md`).

Not related to the `rs-trafilatura` package, which is a separate extractor with different output (exact on
171/10,666 pages, see [Benchmark](#benchmark)).

A port of trafilatura 2.2.0's pipeline (`crates/trafilatura-rs/src/trafilatura2`, diff in
`research/trafilatura-2x-diff.md`) is verified by the harness but not exposed in Python yet.

## Verified configs

`trafilatura_rs.VERIFIED`, each byte-identical to Python on the 10,666-doc corpus below
(`(favor_precision, favor_recall, include_tables, no_fallback)`):

| harness cfg | args | 1.11 final | 2.2.0 final |
|---|---|---|---|
| fineweb | `favor_precision=True, deduplicate=True` (datatrove) | 10666/10666 | 10666/10666 |
| balanced | defaults | 10666/10666 | 10666/10666 |
| recall | `favor_recall=True` | 10666/10666 | 10666/10666 |
| fast | `no_fallback=True` | 10666/10666 | 10666/10666 |
| notables | `include_tables=False` | 10666/10666 | 10666/10666 |

Stage hashes: 1.11 (fineweb) 10666/10666; 2.2.0 balanced and fineweb 10666/10666. jusText and readability
components: 10666/10666. Any other argument falls back to Python trafilatura.

## Usage

```bash
pip install trafilatura-rs
```

```python
import trafilatura_rs
trafilatura_rs.install()  # patches trafilatura.extract; covered calls run in Rust, others stay on Python
# e.g. datatrove's Trafilatura(favour_precision=True) now runs trafilatura-rs
trafilatura_rs.extract(html, favor_precision=True, deduplicate=True)   # direct; NotImplementedError if not covered
```

`deduplicate=True` keeps a process-global LRU like trafilatura's, so output depends on doc order;
`trafilatura_rs.reset_caches()` clears it.

## Dev setup (Linux)

Needs Rust, cmake, uv, perf. Corpus and references live outside the repo in `$TRAFILATURA_RS_DATA`
(default `/srv/agents/cache/trafilatura-rs-data`).

```bash
tools/dev.sh setup     # .venv (py3.13: trafilatura 1.11, datatrove via .pth from $DATATROVE_SRC, rs-trafilatura)
                       # + .venv-t22 (trafilatura 2.2.0)
tools/dev.sh build     # cargo build --release (harness bins)
tools/dev.sh py        # maturin develop --release into .venv
tools/dev.sh test      # cargo test
tools/build_wheel.sh   # abi3 wheel in target/wheels (PGO=1 for profile-guided, via tools/pgo.sh)
pytest tests           # parity vs trafilatura on tests/fixtures (needs the wheel + `.[test]` pins; CI runs this)
```

Releases: `.github/workflows/release.yml` builds abi3 wheels (Linux x86_64/aarch64, macOS x86_64/arm64,
Windows x64) and an sdist, smoke-tests each against trafilatura, and publishes to PyPI on a `v*` tag matching
`pyproject.toml`'s version.

## Parity

```bash
tools/make_reference.sh   # exports docs.jsonl (first 10,666 URLFilter-passing HTML records of the WARC) and all
                          # Python references; REF_JOBS parallel, skips existing files (~50 CPU-min)
tools/parity.sh           # every job; or e.g. tools/parity.sh stages final:fineweb final2:recall
LIMIT=500 tools/parity.sh final:fineweb
tools/parity.sh show 123 txt          # diff one doc's stage (show2 for 2.2.0)
TRAFILATURA_RS_VERIFY_XPATH=1 tools/parity.sh final:fineweb   # cross-check fast-path XPaths against libxml2
```

## Benchmark

`tools/bench.sh [N]` — trafilatura 1.11 via datatrove's call, the same call after `trafilatura_rs.install()`, and
`rs_trafilatura.extract(html, favor_precision=True, include_comments=False)`. Fresh process per run, same docs in
the same order, single thread, engines alternated over `ROUNDS` (default 2). Reports per-doc CPU time (min over
rounds) and `perf stat` user-space instructions net of a load-only baseline; never wall time (shared box). It also
checks trafilatura-rs output == trafilatura output on every doc (the end-to-end check) and rs-trafilatura agreement.

Full corpus (10,666 docs), 2026-11-10, loaded 16-core box:

| engine | mean ms/doc | p50 | p99 | Minstr/doc | speedup (CPU / instr) | output == trafilatura |
|---|---|---|---|---|---|---|
| trafilatura 1.11 | 17.93 | 11.19 | 110.83 | 261.7 | 1x | — |
| trafilatura-rs | 1.92 | 1.25 | 11.85 | 23.2 | 9.3x / 11.3x | 10666/10666 |
| rs-trafilatura 0.1.1 | 12.24 | 7.66 | 81.88 | 181.8 | 1.5x / 1.4x | 171/10666 exact, 974 whitespace-normalized |

rs-trafilatura is a different extractor, not a port: it never returns None, separates paragraphs with `\n\n`, and
selects different content (248/10666 equal with None == "", 172 of them both empty; 637 equal after collapsing
newline runs; median difflib similarity ~0.67 on the first 400 docs).

