From b3b5f578990564f73cae5cca4867f7a768cdd784 Mon Sep 17 00:00:00 2001 From: isgav <51133755+isaranga@users.noreply.github.com> Date: Thu, 26 Nov 2026 10:39:28 +0200 Subject: [PATCH] Show the levels set by Phase X in a verbose table print_table reports "(no changes)" when the Before/After rows match, and Phase X only passed it types. An isolate or embedding without an override changes no types, so the level assignment X1-X8 makes was never shown (e.g. example 7 printed "Rule X1-X9 (no changes)"). Phase X now prints an "X1-X8 (levels)" table (Before = paragraph level from X1) next to the renamed "X1-X9 (types)" table. Update the demo notes for examples 6 and 7, the apply_explicit_levels docstring (FSI type change), README.md and CLAUDE.md. --- CLAUDE.md | 2 +- README.md | 2 +- bidi.py | 20 ++++++++++++++++---- demo.py | 7 ++++--- 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index bd13615..d1c69dc 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -82,7 +82,7 @@ Bracket pair detection for N0 is handled by `_find_bracket_pairs`, using `OPEN_B ### Verbose mode -Every phase function (and `_apply_i1`/`_apply_i2`) accepts `verbose=True`; the per-rule W, N and L helpers do not, and their phase function prints on their behalf. When enabled, `print_table` (in `verbose_table.py`, together with the helpers below) prints a before/after table for each rule application, with rows for character index, code point, the character itself, the type (or level, or position) before and after the rule, and embedding level. Columns are always in logical order, including the L2 and L4 tables, where Before/After give each character's position before and after reordering. +Every phase function (and `_apply_i1`/`_apply_i2`) accepts `verbose=True`; the per-rule W, N and L helpers do not, and their phase function prints on their behalf. When enabled, `print_table` (in `verbose_table.py`, together with the helpers below) prints a before/after table for each rule application, with rows for character index, code point, the character itself, the type (or level, or position) before and after the rule, and embedding level. `print_table` decides "(no changes)" by comparing only the Before/After rows, so Phase X prints two tables: "X1-X8 (levels)" (Before is the paragraph level from X1) and "X1-X9 (types)" (overrides, FSI resolution, X9's BN). Without the levels table, an isolate or embedding with no override would show as "(no changes)". Columns are always in logical order, including the L2 and L4 tables, where Before/After give each character's position before and after reordering. Characters in table cells go through `format_char_for_table`: controls and whitespace are left blank, combining marks are shown on a dotted circle, and each R/AL character is followed by an invisible LEFT-TO-RIGHT MARK so that bidi-aware viewers (Markdown previews, GitHub) don't reverse runs of the row. Cells are sized and centered by `display_width`/`center_in_column`, not `len()`, because those marks take no column on screen. The tracing example in README.md is copied from real output (LRMs included); update it whenever the table format changes. diff --git a/README.md b/README.md index efa390e..a87559b 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,7 @@ for paragraph in split_into_paragraphs("שלום\nHello"): ## Tracing the algorithm -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. +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. Phase X prints two tables, one for the levels it assigns and one for the types it changes, so that an embedding or isolate shows up even when no type changes. ```python apply_bidi_algorithm("שלום 123 Hi", verbose=True) diff --git a/bidi.py b/bidi.py index 7e0b90c..570e505 100644 --- a/bidi.py +++ b/bidi.py @@ -333,8 +333,11 @@ def apply_explicit_levels(chars: list[CharacterInfo], paragraph_embedding_level: X1-X9: Assign embedding levels based on explicit formatting characters. Modifies chars[i].embedding_level and chars[i].character_type in place. character_type changes: characters inside a directional override (RLO/LRO) - have their type set to 'R' or 'L'; characters whose original_type is in - X9_REMOVE_TYPES (RLE, LRE, RLO, LRO, PDF, BN) are reset to 'BN'. + have their type set to 'R' or 'L'; an FSI's type becomes 'RLI' or 'LRI' + (X5c); characters whose original_type is in X9_REMOVE_TYPES (RLE, LRE, + RLO, LRO, PDF, BN) are reset to 'BN'. + In verbose mode, prints one table for the levels (before = the paragraph + level from X1) and one for the types. """ # X1: Initialize stack = [(paragraph_embedding_level, 'neutral', False)] @@ -344,6 +347,10 @@ def apply_explicit_levels(chars: list[CharacterInfo], paragraph_embedding_level: before_types = [ci.original_type for ci in chars] + # For the verbose levels table: X1 starts every character at the + # paragraph embedding level, so that is the "before" level. + before_levels = [paragraph_embedding_level] * len(chars) + for ci in chars: t = ci.character_type level, override, is_isolate = stack[-1] @@ -485,9 +492,14 @@ def apply_explicit_levels(chars: list[CharacterInfo], paragraph_embedding_level: ci.character_type = 'BN' if verbose: - after_types = [ci.character_type for ci in chars] print("=== Phase X: Explicit Embedding Levels ===") - print_table("X1-X9", chars, before_types, after_types) + # Two tables: the levels set by X1-X8, then the types changed by + # directional overrides (X5a-X6a, FSI resolution in X5c) and by X9. + after_levels = [ci.embedding_level for ci in chars] + print_table("X1-X8 (levels)", chars, + [str(l) for l in before_levels], [str(l) for l in after_levels]) + after_types = [ci.character_type for ci in chars] + print_table("X1-X9 (types)", chars, before_types, after_types) # ============================================================ diff --git a/demo.py b/demo.py index c4b3fa3..28615ef 100644 --- a/demo.py +++ b/demo.py @@ -64,13 +64,14 @@ ( "[LRE]Hello שלום[PDF]", "LRE/PDF explicit embedding", - "X1-X9 put the text between LRE and PDF at level 2, so the Hebrew word ends up at level 3 after I1.", + "The X1-X8 (levels) table puts the text between LRE and PDF at level 2, so the Hebrew word " + "ends up at level 3 after I1; the X1-X9 (types) table shows X9 turning LRE and PDF into BN.", ), ( "הוא אמר: ״[LRI]Car is מכונית in Hebrew[PDI]״.[RLM]", "LRI/PDI isolate", - "The text between LRI and PDI forms its own isolating run sequence (level 2), " - "separate from the Hebrew sentence around it (level 1).", + "The X1-X8 (levels) table puts the text between LRI and PDI at level 2; it forms its own " + "isolating run sequence, separate from the Hebrew sentence around it (level 1).", ), ( "Hello שלום (עולם)",