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 clusterslen(text: str) -> int: Returns the number of grapheme clusters in the stringslice(text: str, start: int, end: int) -> str: Extracts a substring by grapheme cluster indicestruncate(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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file graphemex-0.1.1-cp37-abi3-macosx_11_0_arm64.whl.
File metadata
- Download URL: graphemex-0.1.1-cp37-abi3-macosx_11_0_arm64.whl
- Upload date:
- Size: 350.8 kB
- Tags: CPython 3.7+, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
88d5b24c04892120f1a66fb333c5e8019aff6da7266110ed5bc9026a264f2501
|
|
| MD5 |
fa55d23c7a1d727e8a863e9ec573fdd8
|
|
| BLAKE2b-256 |
2b956f2263a5277a951b1f70fa854b045195250c8f2a1cda5294ce0d77ea5717
|