A readable, pure-Python implementation of the Unicode Bidirectional Algorithm (UAX #9), targeting Unicode 17.0.
The bidi algorithm decides how text that mixes left-to-right scripts (Latin, Cyrillic, …) with right-to-left scripts (Hebrew, Arabic, …) is laid out on screen. This project is meant for learning how that algorithm works. It favours clarity over speed or brevity:
- The whole algorithm is in one file, bidi.py, with no dependencies beyond the Python standard library. The two Unicode data files it needs are kept, unchanged, in ucd/, and are read by ucd_data.py. The tables printed in verbose mode are formatted separately, in verbose_table.py. Neither file holds any algorithm logic; they are kept apart so that file reading and formatting code don't clutter the algorithm.
- Each UAX #9 rule has its own function, named after the rule (
_apply_w1,_apply_n0,_apply_l2, …), so you can read the code next to the spec. - A verbose mode prints a before/after table every time a rule is applied, so you can follow a string through the algorithm step by step.
Requires Python 3.10+ (developed with 3.13). No installation is needed:
git clone <this repo>
cd UnicodeBidiAlgorithm
python bidi.pyThis runs a set of example strings (pure LTR, pure RTL, mixed text with digits, explicit embeddings, isolates, and brackets) through the full algorithm with verbose output.
from bidi import apply_bidi_algorithm
levels, reordered, para_level, visual_order = apply_bidi_algorithm("Hello שלום")apply_bidi_algorithm(text, verbose=False, paragraph_embedding_level=None) returns a tuple:
| Value | Meaning |
|---|---|
levels |
The resolved embedding level of each character, in logical (input) order |
reordered |
The text in visual (display) order, with mirroring (rule L4) applied |
para_level |
The paragraph embedding level: 0 = left-to-right, 1 = right-to-left |
visual_order |
For each visual position, the index of the input character shown there |
By default the paragraph direction is detected from the first strong character (rules P2–P3). Pass paragraph_embedding_level=0 or 1 to force it.
Pass verbose=True to see every rule as it runs. Rules that change nothing print a single line; rules that do change something print a table showing each character's index, its code point, the character itself, its type (or level) before and after the rule, and its embedding level. Changed cells are highlighted in the terminal.
apply_bidi_algorithm("שלום 123 Hi", verbose=True)=== Phase P: Paragraph Embedding Level = 1 ===
...
--- Rule N1 | IRS level=1 ---
Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
Codepoint | 05E9 | 05DC | 05D5 | 05DD | 0020 | 0031 | 0032 | 0033 | 0020 | 0048 | 0069 |
Character | ש | ל | ו | ם | | 1 | 2 | 3 | | H | i |
Before | R | R | R | R | WS | EN | EN | EN | WS | L | L |
After | R | R | R | R | R | EN | EN | EN | WS | L | L |
Level | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
...
--- Rule I2 ---
Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
Codepoint | 05E9 | 05DC | 05D5 | 05DD | 0020 | 0031 | 0032 | 0033 | 0020 | 0048 | 0069 |
Character | ש | ל | ו | ם | | 1 | 2 | 3 | | H | i |
Before | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
After | 1 | 1 | 1 | 1 | 1 | 2 | 2 | 2 | 1 | 2 | 2 |
Level | 1 | 1 | 1 | 1 | 1 | 2 | 2 | 2 | 1 | 2 | 2 |
...
Here N1 turns the space between the Hebrew word and the digits into R, because it sits between two right-to-left contexts (numbers count as R for this rule). I2 then raises the digits and the Latin letters to level 2, so they display left-to-right inside the right-to-left paragraph.
apply_bidi_algorithm runs six phases in order. Each matches a section of UAX #9:
| Phase | Function | UAX #9 rules | What it does |
|---|---|---|---|
| P | determine_paragraph_embedding_level |
P2–P3 | Picks the paragraph direction |
| X | apply_explicit_levels |
X1–X9 | Applies explicit embeddings, overrides and isolates (LRE, RLO, LRI, PDF, PDI, …) |
| W | apply_weak_rules |
W1–W7 | Resolves weak types such as numbers and separators |
| N | apply_neutral_rules |
N0–N2 | Resolves neutrals: brackets first (N0), then spaces and punctuation |
| I | apply_implicit_rules |
I1–I2 | Turns the resolved types into final embedding levels |
| L | apply_reorder_rules |
L1–L4 | Resets trailing whitespace, then reverses runs into visual order |
Some things to know when reading the code:
CharacterInfois the record passed through every phase.original_typeis the character's Unicode bidi class and never changes;character_typeis a working copy that the rules update.- Isolating run sequences (definition BD13) are what phases W and N work on.
get_isolating_run_sequencesbuilds them by linking runs of the same level across an isolate (LRI/RLI/FSI … PDI), and attaches thesos/eostypes at each end. - Bracket pairs for rule N0 are found by
_find_bracket_pairs, using pairs read from ucd/BidiBrackets.txt. - Mirrored characters for rule L4 are read from ucd/BidiMirroring.txt.
- Both files are copied unchanged from the Unicode Character Database (currently Unicode 17.0.0). To update them, replace them with newer versions.
- The input is treated as a single paragraph; the algorithm does not split text at paragraph separators (rule P1).
- Line breaking is not handled: the L rules treat the whole input as one line.
- Bidi classes come from Python's
unicodedatamodule, so they follow the Unicode version built into your Python, which may be older than 17.0 (for example, Python 3.14 uses Unicode 16.0). Characters added in later Unicode versions may then get the wrong bidi class. - Rule L3 (combining marks) takes no action. The spec leaves this to the renderer.
- Rule L4 (mirroring, for example
(→)in right-to-left text) swaps the characters listed in BidiMirroring.txt. Some mirrored characters, such as∛, have no mirrored counterpart character, so drawing them mirrored is left to the renderer.
The implementation is checked against the official Unicode test files. You don't need these to understand the algorithm, but they're there if you want to change the code:
- BidiCharacterTest.txt (Unicode 17.0.0) tests real codepoints.
- BidiTest.txt (Unicode 17.0.0) tests abstract sequences of bidi types.
python tests/run_bidi_tests.py # BidiCharacterTest.txt
python tests/run_bidi_type_tests.py # BidiTest.txtBoth runners accept --max-failures N, --lines START-END and --fail-fast.