Skip to main content

Library designed as a python wrapper to unleash Rust text processing power combined with Python

Project description

PyTextRust

High-performance text processing library that unleashes the power of Rust's state-of-the-art regex engine for Python. Built as a direct wrapper around Rust's regex crate - the fastest and most feature-complete regex implementation available.

PyTextRust focuses on sparse text patterns and optimized algorithms, delivering exceptional performance through Rust's zero-cost abstractions and Python's ease of use.

Give some happiness

🚀 Performance Features

Non-Unicode Mode

Achieve 6-12x faster processing for ASCII-like text by disabling unicode matching:

  • unicode=False removes unicode overhead from matching
  • substitute_bound=True optimizes word boundaries for maximum DFA utilization
  • substitute_latin_char=True handles accented characters efficiently

Parallel Processing

Built-in parallelization with configurable thread counts and chunk sizes for maximum throughput on large text collections.

🔍 Regex Pattern Matching

Powered by Rust's state-of-the-art regex engine with intelligent pattern compilation and RegexSet optimization.

from pytextrust.regex_operator import apply_patterns_to_texts

# Find multiple patterns across texts with high performance
patterns = [r"\b\d{3}-\d{3}-\d{4}\b", r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b"]
texts = ["Call me at 555-123-4567 or email john@example.com", "My number is 999-888-7777"]

results = apply_patterns_to_texts(
    patterns=patterns,
    texts=texts,
    unicode=False,  # 6-12x faster for ASCII text
    substitute_bound=True,  # Optimize word boundaries
    case_insensitive=True,
    n_threads=4
)

# Results contain match_results, invalid_pattern_indexes, etc.
print(results['match_results'])  # Matched positions by text and pattern
print(f"Total matches: {results['n_total_matches']}")

Regex Compilation Check

from pytextrust.regex_operator import check_regex_compile

# Validate regex patterns before processing
pattern = r"(?P<phone>\b\d{3}-\d{3}-\d{4}\b)"
base_nonunicode, base, fancy, python = check_regex_compile(pattern)
print(f"Base non-unicode: {base_nonunicode}, Base: {base}, Fancy: {fancy}, Python: {python}")

Regex Matching

from pytextrust.regex_operator import match_patterns_to_texts

# Fast pattern-text matching using RegexSet
patterns = [r"\bpython\b", r"\brust\b", r"\bjavascript\b"]
texts = ["I love python programming", "Rust is fast", "JavaScript is versatile"]

matches, invalid_patterns = match_patterns_to_texts(
    patterns=patterns,
    texts=texts,
    unicode=False,
    substitute_bound=True,
    regexset_chunk_size=1000,
    regexset_size_mb_limit=100
)
print(matches)  # Returns matched (text_index, pattern_index) pairs
print(f"Invalid patterns: {invalid_patterns}")  # Pattern indexes that failed compilation

📝 Literal Text Replacement

Ultra-fast literal replacement using Rust's aho-corasick algorithm with boundary checking.

from pytextrust.replacer import replace_literal_patterns

# High-performance literal replacement with word boundaries
patterns = ["uno", "dos", "tres"]
replacements = ["1", "2", "3"]
texts = ["es el numero uno o el dos y el tres"]

replaced_texts, num_replacements = replace_literal_patterns(
    literal_patterns=patterns,
    replacements=replacements,
    text_to_replace=texts,
    is_bounded=True,  # Respect word boundaries
    case_insensitive=True,
    n_jobs=4
)

print(replaced_texts)  # ['es el numero 1 o el 2 y el 3']
print(num_replacements)  # 3

Lookup-Based Replacement

from pytextrust.replacer import LookUpReplacer

# Create lookup-based replacer
lookup_dict = {"hello": "hi", "world": "earth", "python": "snake"}
replacer = LookUpReplacer(provided_lookup_dict=lookup_dict)

# Replace individual tokens
print(replacer.replace_token("hello"))  # "hi"
print(replacer.replace_token("world"))  # "earth"

# Replace texts (for bulk operations, save to file first)
texts = ["hello world, python is great"]
LookUpReplacer.write_lookup(lookup_dict, "/tmp/lookup.bin")
bulk_replacer = LookUpReplacer(bulk_lookup_path="/tmp/lookup.bin")
replaced_texts = bulk_replacer.replace_texts(texts)
print(replaced_texts)  # Modified texts with replacements

🧮 Text Similarity and Distance

Advanced text similarity calculations with multiple algorithms and parallel processing.

from pytextrust.similarity import parallel_calculate_similarity, Similarity

# Calculate similarity between texts and patterns
haystacks = ["The quick brown fox jumps over the lazy dog", "Python is a great language"]
patterns = ["quick brown", "lazy cat", "python language"]

similarities = parallel_calculate_similarity(
    haystack=haystacks,
    patterns=patterns,
    step=1,  # Character step size
    similarity=Similarity.JACCARD.value,  # Choose similarity algorithm
    par_chunk_size=1000,
    n_threads=1
)
print(similarities)  # 2D array: [haystack_index][pattern_index] similarity scores
print(f"Shape: {len(similarities)}x{len(similarities[0])}")

Single Text Similarity Search

from pytextrust.similarity import single_similarity_search, Similarity

# Find similarity in a single text
haystack = "The quick brown fox jumps over the lazy dog"
patterns = ["quick brown", "lazy dog", "jumping cat"]

results = single_similarity_search(
    haystack=haystack,
    patterns=patterns,
    step=1,
    similarity=Similarity.COSINEJACCARDCOMBINED.value
)
print(results)  # Similarity scores for each pattern in the text

🔤 Text Tokenization and Processing

Efficient text tokenization and token-based operations.

from pytextrust.token import tokenize, get_match_from_token_range

# Tokenize text into words
text = "The quick brown fox jumps"
tokens = tokenize(text)
print(tokens)  # ['The', 'quick', 'brown', 'fox', 'jumps']

# Convert token ranges to character positions
start_char, end_char = get_match_from_token_range(
    text=text,
    token_start=1,  # 'quick'
    token_end=3     # 'brown'
)
print(f"Characters {start_char}-{end_char}: '{text[start_char:end_char]}'")

Token Mapping

from pytextrust.token import map_match_by_tokens

# Map matches between original and lemmatized text representations
# Original text with multiple word forms
source = "  Ñ  ha habido eventos multiples  "
# Lemmatized version with standardized forms  
lemmatized = " Ç  hacer haber evento multiple "

# Map a match found in lemmatized text back to original text positions
mapped_start, mapped_end = map_match_by_tokens(
    source=source,          # Original text
    matched=lemmatized,     # Lemmatized text
    match_start=2,          # Start position in lemmatized text
    match_end=7             # End position in lemmatized text
)
print(f"Mapped to original text position: {mapped_start}-{mapped_end}")
print(f"Original text segment: '{source[mapped_start:mapped_end]}'")

Vocabulary Computation

from pytextrust.token import compute_vocabulary

# Compute token frequencies across texts  
texts = ["hello world", "world hello", "hello python world"]

vocabulary = compute_vocabulary(
    text_list=texts,
    n_threads=4
)
print(vocabulary)  # {'hello': 3.0, 'world': 3.0, 'python': 1.0}

🧹 Text Normalization

Clean and normalize text with efficient processing.

from pytextrust.normalization import normalize_text

# Normalize text with multiple options
texts = ["HELLO    WORLD!!", "  Múltiple   SPÂCES   "]

cleaned = normalize_text(
    text_list=texts,
    reduce_accents=True,              # Remove accents: á -> a
    reduce_punctuation=True,          # Remove punctuation  
    reduce_multi_whitespaces=True,    # Clean multiple whitespaces
    transform_to_lower_case=True      # Convert to lowercase
)
print(cleaned)  # ['hello world', 'multiple spaces']

⚡ Performance Tips

  1. Use non-unicode mode (unicode=False) for 6-12x speed boost on ASCII text
  2. Enable boundary substitution (substitute_bound=True) for optimal DFA usage
  3. Configure parallel processing with appropriate n_threads and chunk sizes
  4. Pre-compile lookups for repeated literal replacements
  5. Use RegexSet limits to control memory usage: regexset_size_limit, regexset_dfa_size_limit

🛠️ Installation

pip install pytextrust

📚 Documentation

This library leverages Rust's world-class regex engine and text processing capabilities:

🏆 Performance Benchmarks

PyTextRust consistently outperforms pure Python solutions:

  • 6-12x faster regex matching in non-unicode mode
  • Parallel processing scales linearly with CPU cores
  • Memory efficient RegexSet compilation reduces overhead
  • Zero-copy operations where possible through Rust integration

The underlying Rust regex engine is recognized as one of the fastest available, making PyTextRust ideal for high-throughput text processing applications.

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

pytextrust-0.12.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

pytextrust-0.12.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

pytextrust-0.12.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

pytextrust-0.12.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

pytextrust-0.12.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.7mmanylinux: glibc 2.17+ x86-64

File details

Details for the file pytextrust-0.12.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pytextrust-0.12.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d38f6a776be150466fb15e3311b03b75faad924b2e9aa3c9140da3bb929f5f42
MD5 de67db0998d1af010d98ece91062a6a2
BLAKE2b-256 3556183b27d4817571f1bb8060090db45001f776e257f72bd743a8e2cd309a2a

See more details on using hashes here.

File details

Details for the file pytextrust-0.12.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pytextrust-0.12.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 bf7066afc3ea2c69bebca64e83ce43f96e9b71e549e38403706d2d694abe3090
MD5 ed87d46fa8bbcc3f3dcb0880516a29c2
BLAKE2b-256 4d0507c7b41c7fe3edfed9a3bfeb544ffd6595970e35659a4ce218753084fa02

See more details on using hashes here.

File details

Details for the file pytextrust-0.12.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pytextrust-0.12.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ddf881fec84215b3a5727344fda39cc9f4fd65f315775fb650f6332b096953f9
MD5 30f0320263ae907c19d009ec9cbb938e
BLAKE2b-256 80afda5649abfd650a6675cffd6f47c5c9e08056f166e0f39d57b5c86b6721f2

See more details on using hashes here.

File details

Details for the file pytextrust-0.12.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pytextrust-0.12.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ec9ffe8afeaf0bc2003154bb27583a947bc0f1690412f4ae886be32ffa2bfc4b
MD5 25195edd0b0b6d6a16ab76d5e1210bc7
BLAKE2b-256 f2f60b22d8f22458fa3517da9c0ba442cdeb90bec7d26f83eba1c807e003aa5d

See more details on using hashes here.

File details

Details for the file pytextrust-0.12.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pytextrust-0.12.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f6b742f08ff28ad5827e98f8ceadf1c25dae674900d934032278d4c5357bd8b4
MD5 f899cd0c6856a4c174a7fea5ca588b36
BLAKE2b-256 f8eff360966cf012bcbb0bb770445d3de58b6c7c952384d9d6f56fbc34dadd01

See more details on using hashes here.

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