Skip to main content
When two Myanmar strings look similar but differ by a character or two, the library needs a way to quantify that difference. mySpellChecker implements three edit distance variants — classic Levenshtein, Damerau-Levenshtein with transpositions, and a weighted version tuned for common Myanmar character confusions — each backed by optional Cython acceleration.

Overview

Edit distance measures how many operations are needed to transform one string into another. mySpellChecker implements several algorithms optimized for Myanmar text.

Algorithms

Levenshtein Distance

The classic edit distance algorithm with three operations:
  • Insertion: Add a character
  • Deletion: Remove a character
  • Substitution: Replace a character
Note: Always import from edit_distance (the Python wrapper), not edit_distance_c (the Cython module) directly. The wrapper automatically uses the Cython implementation when available and falls back to a pure Python implementation when Cython extensions are not compiled. This ensures your code works on all platforms.

Damerau-Levenshtein Distance

Extends Levenshtein with transposition (swap adjacent characters):

Weighted Damerau-Levenshtein

Uses custom costs for Myanmar-specific character confusions:

Myanmar-Specific Substitution Costs

The weighted algorithm uses reduced costs for commonly confused characters:

Setting Custom Costs

Substitution costs come from two sources that are merged at module import time:
  1. Hardcoded defaults: MYANMAR_SUBSTITUTION_COSTS in text/phonetic_data.py, which contains hand-crafted costs for well-known confusions.
  2. Data-driven overrides: rules/confusion_matrix.yaml, which provides corpus-derived substitution costs loaded via load_confusion_matrix(). This YAML file can override hardcoded costs (e.g., refining ျ↔ြ from 0.3 to 0.2) and add new pairs not covered by the defaults (e.g., asat↔dot_below, ka↔ta).
The merge uses YAML-wins semantics: if both sources define the same character pair, the YAML cost takes precedence. If confusion_matrix.yaml fails to load, the library falls back to the hardcoded costs only.

Implementation Details

UTF-8 Handling

The Cython implementation properly handles Myanmar Unicode:

Memory Optimization

The basic Levenshtein implementation uses row-based dynamic programming for O(min(m,n)) space. The Damerau-Levenshtein variants require the full O(m×n) matrix to support transposition lookups:

Performance

The Cython implementation provides significant speedup:

Benchmark

Integration with SymSpell

Edit distance is used by SymSpell for suggestion ranking:

Pure Python Fallback

If Cython extensions aren’t available, pure Python is used:

See Also