# ๐Ÿ‡ช๐Ÿ‡น EthioBBPE: Byte-Level BPE Tokenizer for Ethiopian Languages A robust, production-ready Byte-Level BPE (BBPE) tokenizer training framework specifically optimized for Ethiopian languages (Amharic, Oromo, Tigrinya, Somali, etc.) using Hugging Face's `tokenizers` library. ## โœจ Features - **Optimized for Ethiopic Script**: Handles complex Ge'ez script characters and Latin-based orthographies seamlessly. - **Byte-Level Encoding**: Robust against unknown characters and emojis, ensuring no `` tokens. - **End-to-End Pipeline**: From raw text corpus to a ready-to-use `tokenizer.json`. - **Hugging Face Compatible**: Directly usable with `transformers` models. - **Flexible Configuration**: Customize vocabulary size, minimum frequency, and special tokens. - **Multi-Format Support**: Train on `.txt`, `.json`, or `.jsonl` datasets. ## ๐Ÿ“ฆ Installation ```bash pip install -r requirements.txt ``` ## ๐Ÿš€ Quick Start ### 1. Prepare Your Data Place your training corpus (raw text files) in the `data/` directory. ```text data/ โ”œโ”€โ”€ amharic_corpus.txt โ”œโ”€โ”€ oromo_corpus.txt โ””โ”€โ”€ mixed_ethio_text.txt ``` ### 2. Train the Tokenizer **Using CLI:** ```bash python scripts/train_tokenizer.py \ --data_dir ./data \ --model_name EthioBBPE \ --vocab_size 32000 \ --min_frequency 2 \ --special_tokens "[PAD]","[UNK]","[CLS]","[SEP]","[MASK]" ``` **Using Python API:** ```python from scripts.bbpe_trainer import BBPETrainer, BBPEConfig # Configure for Ethiopian Languages config = BBPEConfig( vocab_size=32000, min_frequency=2, show_progress=True, special_tokens=["[PAD]", "[UNK]", "[CLS]", "[SEP]", "[MASK]"] ) trainer = BBPETrainer(config=config, model_name="EthioBBPE") trainer.train_from_directory("./data") trainer.save("./models/EthioBBPE") # Test it text = "แˆฐแˆ‹แˆ แˆแŒ„ แŠฅแŠ•แ‹ดแ‰ต แАแˆฝ? (Hello daughter, how are you?)" tokens = trainer.tokenize(text) print(f"Tokens: {tokens}") ``` ### 3. Load and Use ```python from tokenizers import Tokenizer # Load the trained tokenizer tokenizer = Tokenizer.from_file("models/EthioBBPE/tokenizer.json") # Encode encoded = tokenizer.encode("แ‹จแŠขแ‰ตแ‹ฎแŒตแ‹ซ แˆ…แ‹แ‰ฅ") print(encoded.ids) print(encoded.tokens) # Decode decoded = tokenizer.decode(encoded.ids) print(decoded) ``` ## ๐Ÿ—๏ธ Architecture The `EthioBBPE` architecture follows these steps: 1. **Pre-tokenization**: Splits text into words while preserving byte-level integrity. 2. **Byte Conversion**: Converts all characters (including Ge'ez script) into their byte representations. 3. **BPE Training**: Learns merge operations based on frequency in the corpus. 4. **Vocabulary Creation**: Generates a fixed-size vocabulary of byte-level tokens. ## ๐Ÿ“‚ Project Structure ```text Ethio_BBPE/ โ”œโ”€โ”€ data/ # Raw training data โ”œโ”€โ”€ models/ # Output directory for trained models โ”œโ”€โ”€ scripts/ โ”‚ โ”œโ”€โ”€ bbpe_trainer.py # Core logic (BBPEConfig, BBPETrainer) โ”‚ โ”œโ”€โ”€ train_tokenizer.py # CLI entry point โ”‚ โ””โ”€โ”€ example_usage.py # Usage examples โ”œโ”€โ”€ requirements.txt # Dependencies โ””โ”€โ”€ README.md # This file ``` ## ๐Ÿค— Hugging Face Hub This model is designed to be pushed to the Hugging Face Hub: ```python trainer.push_to_hub("Nexuss0781/Ethio-BBPE", token="YOUR_HF_TOKEN") ``` ## ๐Ÿ“„ License MIT License ## ๐Ÿ™ Acknowledgments Built for the Ethiopian NLP community to foster better language understanding and generation capabilities.