Skip to main content

Python bindings for rehuman: Unicode-safe text cleaning & normalization

Project description

rehuman (Python)

Unicode-safe text cleaning & normalization for Python, backed by the native Rust rehuman core (PyO3 + maturin). Strip invisible characters, normalize typography, and enforce ASCII/keyboard-safe output for text sourced from web scraping, user input, or LLMs.

Install

The package is published on PyPI as rehuman:

pip install rehuman
  • Requires Python 3.9+ (abi3 wheels: one wheel per platform covers every supported CPython).
  • Prebuilt wheels: Linux (x86_64, aarch64), macOS (arm64), Windows (x86_64).
  • Other platforms install from the sdist, which compiles the native module and needs a Rust toolchain. Wheel coverage details live in Release Automation.
  • Published wheels are built with the default Rust features: rehuman.HAS_STATS is True and rehuman.HAS_SECURITY is False.

Quickstart

clean() and humanize() both return str but target different outputs: clean produces ASCII/keyboard-safe text, humanize keeps Unicode and tidies typography and whitespace (exact policy differences).

import rehuman

text = "A   B 👍 Café"
cleaned = rehuman.clean(text)       # "A   B Cafe"
humanized = rehuman.humanize(text)  # "A B 👍 Café"

assert cleaned == "A   B Cafe"
assert humanized == "A B 👍 Café"

# Use Cleaner for change counts and stats
cleaner = rehuman.Cleaner()
result = cleaner.clean("Hi\u200bthere \U0001f44d")
print(result.text)         # "Hithere"
print(result.changes_made) # e.g. 3
print(result.stats)        # dict with per-operation counters

Option-level control (non_ascii_policy, presets, line endings, ...) goes through Options and Cleaner.

Advanced usage: cleaning a text corpus

A concrete case study: normalizing model-generated text in a training corpus (SFT conversations, synthetic data, scraped documents). Such text tends to carry invisible codepoints (zero-width spaces, soft hyphens), typographic quotes/dashes/ellipses, and exotic whitespace. Normalizing it removes stylistic tells and collapses spurious token variants without touching content.

Two decisions matter up front:

  • Do not use default clean() on corpora you care about. It targets ASCII/keyboard-safe output and deletes what it cannot transliterate, so non-Latin scripts vanish ("日本語テキスト hello" becomes "hello").
  • Derive from the code_safe preset instead. It keeps every script, emoji, and diagram glyph, strips hidden/control characters, and normalizes typographic quotes and dashes. Use replace to adjust it:
import rehuman

OPTIONS = rehuman.Options.code_safe_preset().replace(
    normalize_other=True,        # also fold ellipsis -> "..." etc.
    unicode_normalization="nfc", # canonical form, no visual change
)

def clean_batch(batch):
    cleaner = rehuman.Cleaner(OPTIONS)  # construction is ~1 microsecond
    results = [cleaner.clean(t) for t in batch["text"]]
    return {
        "text": [r.text for r in results],
        "rehuman_changes": [r.changes_made for r in results],
    }

This shape drops straight into map-style pipelines, for example Hugging Face datasets: ds.map(clean_batch, batched=True, num_proc=8). Options and Cleaner pickle, so worker processes and cache fingerprinting work; clean releases the GIL, so thread pools scale too. Throughput is roughly a thousand few-KB documents per second per core, linear in processes.

Keeping changes_made as a column is deliberate: sort by it descending and eyeball the most-rewritten samples before training on the output.

One caveat: transforms apply to the whole string with no markdown or code awareness. A fenced code block whose literals contain typographic characters gets rewritten like any other text (text.replace("“", '"') breaks). If your corpus is code-heavy, split out fenced blocks and clean only the prose.

Documentation

Development

Build the extension into a local venv and run the test suite:

cd python
python -m venv .venv
source .venv/bin/activate
pip install -U pip maturin pytest
maturin develop
pytest -q

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rehuman-0.2.0.tar.gz (72.6 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

rehuman-0.2.0-cp39-abi3-win_amd64.whl (448.9 kB view details)

Uploaded CPython 3.9+Windows x86-64

rehuman-0.2.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (541.6 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ x86-64

rehuman-0.2.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (535.4 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64

rehuman-0.2.0-cp39-abi3-macosx_11_0_arm64.whl (520.2 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

File details

Details for the file rehuman-0.2.0.tar.gz.

File metadata

  • Download URL: rehuman-0.2.0.tar.gz
  • Upload date:
  • Size: 72.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for rehuman-0.2.0.tar.gz
Algorithm Hash digest
SHA256 fb27688fe97b819675461562dd64a6b54712e1d6520d925f53f2de64c1fbfada
MD5 5c92f07ee060a7582ddc644e45b85571
BLAKE2b-256 9524105ab060b37f6e78d88af417d27a5aad3b9a9301aeb1069f3774c1b9e0cb

See more details on using hashes here.

Provenance

The following attestation bundles were made for rehuman-0.2.0.tar.gz:

Publisher: python-pypi-publish.yml on pszemraj/rehuman

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rehuman-0.2.0-cp39-abi3-win_amd64.whl.

File metadata

  • Download URL: rehuman-0.2.0-cp39-abi3-win_amd64.whl
  • Upload date:
  • Size: 448.9 kB
  • Tags: CPython 3.9+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for rehuman-0.2.0-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 1f69385108d8d252c2b44383dc7376b3be88367ff723d8e6b256bbf2a3a447fe
MD5 45c08b6b901b135b823f7fef46ab5c29
BLAKE2b-256 223c8f03ab9f88101ed1eeca0c60c00a03591d13ee334da01ecee0572436737a

See more details on using hashes here.

Provenance

The following attestation bundles were made for rehuman-0.2.0-cp39-abi3-win_amd64.whl:

Publisher: python-pypi-publish.yml on pszemraj/rehuman

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rehuman-0.2.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for rehuman-0.2.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ab5e00a321b8749daead0f05aa10f81c15ada9a3affe2c37a99670236df45721
MD5 256a94b81b1234eaa85b6bffcc6a5dd6
BLAKE2b-256 11292072422f7e8295492a33fa79d43f349153d2b31d7cf4caf53cb8cd7dd63f

See more details on using hashes here.

Provenance

The following attestation bundles were made for rehuman-0.2.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: python-pypi-publish.yml on pszemraj/rehuman

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rehuman-0.2.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for rehuman-0.2.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 4c92a47d1f516d9339a61e98a45cd77b629d1de300fa6465097e3a4236e0f78a
MD5 47af4b93c112d438dcec47e43996bfcb
BLAKE2b-256 dacae8cbdaba1a748ee6e785381580635ae755cbcc9c0bbf6b8f0ffc631f54b0

See more details on using hashes here.

Provenance

The following attestation bundles were made for rehuman-0.2.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: python-pypi-publish.yml on pszemraj/rehuman

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rehuman-0.2.0-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for rehuman-0.2.0-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 cc07c5a9929ea35c2f8796dd46a100f37d32e3064a7378cd1a6e68f922c380f3
MD5 aced7e74eaf97d3cbe5d4f1745623df4
BLAKE2b-256 0ccefd0de6212aaf96ca62a4e8612bc287905b1523d95c857f0ad84b5f02601e

See more details on using hashes here.

Provenance

The following attestation bundles were made for rehuman-0.2.0-cp39-abi3-macosx_11_0_arm64.whl:

Publisher: python-pypi-publish.yml on pszemraj/rehuman

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page