Skip to main content

Fast Unicode grapheme cluster segmentation with Rust + PyO3

Project description

graphemex

Fast Unicode grapheme cluster segmentation library written in Rust using PyO3.

About

graphemex is a high-performance Python library for Unicode grapheme cluster handling, powered by Rust. It provides correct and efficient text segmentation according to Unicode standards, which is essential for proper text processing in many applications.

Key Benefits

  • ๐Ÿš€ High Performance: Implemented in Rust for maximum speed
  • ๐ŸŒ Unicode Correctness: Properly handles all Unicode grapheme clusters
  • ๐Ÿ”ง Simple API: Just 4 intuitive functions for common text operations
  • ๐Ÿ’ป Cross-Platform: Works on all major operating systems
  • ๐Ÿ Python Friendly: Seamless Python integration via PyO3
  • ๐Ÿ›  Zero Dependencies: Only requires Python standard library at runtime

Features

  • split(text: str) -> List[str]: Splits text into grapheme clusters
  • len(text: str) -> int: Returns the number of grapheme clusters in the string
  • slice(text: str, start: int, end: int) -> str: Extracts a substring by grapheme cluster indices
  • truncate(text: str, max_len: int) -> str: Truncates string to maximum number of grapheme clusters

Installation

Use Cases

  • Text editors and IDEs
  • Input validation and processing
  • Social media character counting
  • Text truncation for UI elements
  • Natural language processing
  • Data cleaning and normalization

Performance

graphemex is significantly faster than pure Python implementations:

  • Up to 100x faster for grapheme splitting
  • Minimal memory overhead
  • Efficient handling of large texts

Requirements

  • Python โ‰ฅ3.7
  • No additional runtime dependencies

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Author

Alexandr Sukhryn (alexandrvirtual@gmail.com)

#[cfg(test)] mod tests { use super::*;

#[test]
fn test_split_basic() {
    let text = "Hello";
    assert_eq!(split(text), vec!["H", "e", "l", "l", "o"]);
}

#[test]
fn test_split_emoji() {
    let text = "๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ๐Ÿ‘‹";
    assert_eq!(split(text), vec!["๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ", "๐Ÿ‘‹"]);
}

#[test]
fn test_len_basic() {
    assert_eq!(len("Hello"), 5);
    assert_eq!(len(""), 0);
}

#[test]
fn test_len_complex() {
    assert_eq!(len("๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ Hello ๐ŸŒ!"), 10);
}

#[test]
fn test_slice_basic() {
    let text = "Hello";
    assert_eq!(slice(text, 0, 2).unwrap(), "He");
    assert_eq!(slice(text, 1, 4).unwrap(), "ell");
}

#[test]
fn test_slice_emoji() {
    let text = "๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ Hello ๐ŸŒ!";
    assert_eq!(slice(text, 0, 1).unwrap(), "๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ");
    assert_eq!(slice(text, 8, 9).unwrap(), "๐ŸŒ");
}

#[test]
fn test_slice_invalid() {
    let text = "Hello";
    assert!(slice(text, 3, 2).is_err()); // start > end
    assert!(slice(text, 0, 6).is_err()); // end > len
}

#[test]
fn test_truncate_basic() {
    assert_eq!(truncate("Hello", 3), "Hel");
    assert_eq!(truncate("Hello", 5), "Hello");
    assert_eq!(truncate("Hello", 10), "Hello");
}

#[test]
fn test_truncate_emoji() {
    assert_eq!(truncate("๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ Hello", 1), "๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ");
    assert_eq!(truncate("๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ Hello", 2), "๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ ");
}

}

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 Distribution

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

graphemex-0.1.1-cp37-abi3-macosx_11_0_arm64.whl (350.8 kB view details)

Uploaded CPython 3.7+macOS 11.0+ ARM64

File details

Details for the file graphemex-0.1.1-cp37-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for graphemex-0.1.1-cp37-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 88d5b24c04892120f1a66fb333c5e8019aff6da7266110ed5bc9026a264f2501
MD5 fa55d23c7a1d727e8a863e9ec573fdd8
BLAKE2b-256 2b956f2263a5277a951b1f70fa854b045195250c8f2a1cda5294ce0d77ea5717

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