Skip to main content
The tokenizers module provides low-level text splitting utilities for Myanmar text. Unlike Segmenters (which may involve complex logic and dictionary lookups), Tokenizers provide direct access to segmentation algorithms.

Overview

SyllableTokenizer

A fast, regex-based tokenizer that splits Myanmar text into syllables using the Sylbreak algorithm rules.

Initialization

Basic Usage

How It Works

The tokenizer uses regex patterns to identify syllable boundaries based on:
  1. Myanmar consonants (U+1000-U+1021)
  2. Virama/Asat markers (္ and ်) for stacking detection
  3. Negative lookbehind to preserve stacked consonants

Internal Usage

SyllableTokenizer is the building block for:
  • WordTokenizer (inherits from it)
  • FrequencyBuilder (data pipeline)

WordTokenizer

A word tokenizer supporting two segmentation engines:

Initialization

Basic Usage

Engine: myword (Viterbi)

The myword engine uses a Viterbi algorithm with unigram/bigram probabilities stored in a memory-mapped file for fork-safe, high-performance segmentation. Features:
  • Memory-mapped dictionary (Copy-on-Write for multiprocessing)
  • Cython-optimized Viterbi implementation
  • Post-processing for fragment merging and numeral splitting
Initialization Flow:
Post-Processing Steps:
  1. Fragment merging: Merge invalid consonant+asat patterns
  2. Numeral splitting: Split word+numeral concatenations (e.g., လ၁['လ', '၁'])
  3. Re-merge: Handle fragments created by splitting

Engine: CRF

The CRF engine uses a trained Conditional Random Fields model for syllable-based word boundary detection. Features:
  • Uses pycrfsuite library
  • Feature extraction includes bigrams, trigrams, BOS/EOS markers
  • Good accuracy without requiring large dictionary files

Checking Custom Words

For the myword engine, you can check if words exist in the dictionary:
Note: With mmap-only mode, new words cannot be added dynamically at runtime.

Zero/Wa Normalization

The tokenizer automatically normalizes Myanmar numeral zero (၀, U+1040) to letter wa (ဝ, U+101D) when not in numeric context:

Cython Extensions

Performance-critical tokenization code uses Cython extensions:

Checking Cython Status

Error Handling

TransformerWordSegmenter

A model-agnostic word segmenter that uses any HuggingFace token classification model with B/I (Beginning/Inside) labels to identify word boundaries in Myanmar text.

Requirements

Requires the optional transformers dependency:
This installs:
  • transformers>=4.30.0
  • torch>=2.0.0

Initialization

Constructor Parameters

Basic Usage

How It Works

The segmenter uses a HuggingFace token-classification pipeline with aggregation_strategy="simple". The model labels each token as:
  • B (Beginning): Start of a new word
  • I (Inside): Continuation of the current word
The _merge_bi_tags() method groups consecutive B+I* sequences into complete words:
Edge cases handled:
  • I without preceding B: Treated as a new word start
  • Unknown tag: Treated as B (new word start)
  • Empty tokens: Skipped

Device Support

The segmenter supports CPU, CUDA GPU, and Apple Silicon MPS: Device fallback behavior:
  • If a GPU index is requested but unavailable, falls back to CPU with a warning
  • If PyTorch is not installed, falls back to CPU with a warning

Batch Processing

segment_batch() is significantly more efficient than calling segment() in a loop:
If batch processing fails (e.g., GPU memory), it automatically falls back to processing each text individually.

Data Pipeline Integration

The transformer engine integrates with the data pipeline for building dictionaries from corpus files.

CLI Usage

CLI Flags

Python API

Pipeline Processing Behavior

When using the transformer engine, the pipeline processes chunks sequentially in the main process rather than using multiprocessing. This is because PyTorch’s internal C++ state (thread pools, memory allocators, CUDA contexts) does not survive fork() and loading the model in each spawned worker would be impractical (~1.1GB per worker). The pipeline automatically:
  1. Loads the transformer model once in the main process
  2. Processes chunks sequentially with per-chunk progress reporting
  3. Uses batch inference (segment_batch()) for efficient processing within each chunk

Compatible Model Requirements

The TransformerWordSegmenter is model-agnostic. Any HuggingFace model can be used as long as it meets these requirements:
  1. Task: Must be a token-classification model (compatible with transformers.pipeline("token-classification", ...))
  2. Labels: Must output entity_group values of "B" and "I":
    • B = Beginning of a new word
    • I = Inside/continuation of the current word
  3. Tokenizer: Must include a compatible tokenizer (automatically loaded by the HuggingFace pipeline)
  4. Hosting: Can be hosted on HuggingFace Hub (loaded by model ID) or stored locally (loaded by file path)
The default model is chuuhtetnaing/myanmar-text-segmentation-model, an XLM-RoBERTa model fine-tuned for Myanmar text segmentation.

Error Handling

Properties

Default Model Attribution

The default model is chuuhtetnaing/myanmar-text-segmentation-model:
  • Author: Chuu Htet Naing
  • Base: XLM-RoBERTa fine-tuned for token classification
  • Labels: B (beginning), I (inside)
  • License: See model page for details

Performance Comparison

Benchmarks on Apple M1, Python 3.11

Attribution

The word segmentation algorithms are based on research by Ye Kyaw Thu: The transformer word segmentation uses the model by Chuu Htet Naing:

See Also