From 26eee98a14b275e4e200e86f67f1ee8465b70af9 Mon Sep 17 00:00:00 2001 From: isgav <51133755+isaranga@users.noreply.github.com> Date: Mon, 30 Nov 2026 11:46:34 +0200 Subject: [PATCH 1/2] Name the first strong character in the Phase P verbose trace The P2/P3 trace passed identical Before/After rows to print_table, so it only ever printed "(no changes)". It now prints one line naming the character that set the paragraph level (P2), or saying that none was found and the default level applies (P3). README.md and CLAUDE.md describe the new line. Co-Authored-By: Claude --- CLAUDE.md | 2 +- README.md | 3 ++- bidi.py | 22 +++++++++++++++++----- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index c2e6ef2..4739496 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -83,7 +83,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. `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)". When `paragraph_embedding_level` is given (the demo's `--direction ltr|rtl`), `determine_paragraph_embedding_level` is not called; `apply_bidi_algorithm` prints the Phase P header itself, followed by a line saying that P2/P3 were skipped. After phase X, `apply_bidi_algorithm` calls `print_isolating_run_sequences` (also in `verbose_table.py`), which prints the X10 sequences one line each (level, character indices as ranges, `sos`/`eos`), then the indices of the characters removed by X9. Columns are always in logical order, including the L2 and L4 tables, where Before/After give each character's position before and after reordering. From I1 on (the I1, I2, L1, L2 and L4 tables), a character removed by X9 shows `x` as its level, in the Level row and in the I1/I2/L1 Before/After rows, and `x` as its L2 position. `print_table` does this when passed `after_x9=True`, through `format_level_for_table`. The P and X tables keep the numbers the X rules assigned, and the W and N tables never contain removed characters. +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)". Phase P changes no types, so `determine_paragraph_embedding_level` prints no table: after the Phase P header it prints one line, either "Rule P2" naming the first strong character outside isolates (index, code point, type) or "Rule P3" saying that none was found and the default level applies. When `paragraph_embedding_level` is given (the demo's `--direction ltr|rtl`), `determine_paragraph_embedding_level` is not called; `apply_bidi_algorithm` prints the Phase P header itself, followed by a line saying that P2/P3 were skipped. After phase X, `apply_bidi_algorithm` calls `print_isolating_run_sequences` (also in `verbose_table.py`), which prints the X10 sequences one line each (level, character indices as ranges, `sos`/`eos`), then the indices of the characters removed by X9. Columns are always in logical order, including the L2 and L4 tables, where Before/After give each character's position before and after reordering. From I1 on (the I1, I2, L1, L2 and L4 tables), a character removed by X9 shows `x` as its level, in the Level row and in the I1/I2/L1 Before/After rows, and `x` as its L2 position. `print_table` does this when passed `after_x9=True`, through `format_level_for_table`. The X tables keep the numbers the X rules assigned, and the W and N tables never contain removed characters. 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 0c7f7e4..d259e07 100644 --- a/README.md +++ b/README.md @@ -72,7 +72,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. 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. Rule X10 then lists the isolating run sequences, one line each, with their level, characters and `sos`/`eos`, followed by the characters removed by X9, which belong to no sequence. From rule I1 on, the tables show `x` as the level of those removed characters. +Pass `verbose=True` to see every rule as it runs. Phase P names the first strong character, which sets the paragraph level (rule P2), or says that none was found and the default level 0 applies (rule P3). 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. Rule X10 then lists the isolating run sequences, one line each, with their level, characters and `sos`/`eos`, followed by the characters removed by X9, which belong to no sequence. From rule I1 on, the tables show `x` as the level of those removed characters. ```python apply_bidi_algorithm("שלום 123 Hi", verbose=True) @@ -80,6 +80,7 @@ apply_bidi_algorithm("שלום 123 Hi", verbose=True) ```text === Phase P: Paragraph Embedding Level = 1 === +--- Rule P2 --- first strong character outside isolates: index 0 (U+05E9 ש‎, type R) -> level 1 ... --- Rule N1 | IRS level=1 --- Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | diff --git a/bidi.py b/bidi.py index 164a7d2..2e9089a 100644 --- a/bidi.py +++ b/bidi.py @@ -116,7 +116,8 @@ def determine_paragraph_embedding_level(chars: list[CharacterInfo], verbose: boo Returns 0 for LTR paragraph, 1 for RTL paragraph. """ open_isolate_count = 0 - paragraph_level = 0 # P3 default: LTR if no strong type is found + paragraph_level = 0 # P3 default: LTR if no strong type is found + first_strong_char = None # the character found by P2, if any for ci in chars: char_type = ci.character_type @@ -128,17 +129,28 @@ def determine_paragraph_embedding_level(chars: list[CharacterInfo], verbose: boo elif open_isolate_count == 0: if char_type in ('R', 'AL'): paragraph_level = 1 + first_strong_char = ci break elif char_type == 'L': paragraph_level = 0 # explicit for symmetry with the RTL case above + first_strong_char = ci break if verbose: - # Phase P only reads character types; it does not mutate them. - # Both before and after columns are the same — showing current state only. - types = [ci.character_type for ci in chars] + # Phase P changes no types, so a before/after table would be empty. + # Instead, name the character that decided the level (P2), or say + # that none was found and the default applied (P3). print(f"=== Phase P: Paragraph Embedding Level = {paragraph_level} ===") - print_table("P2/P3 (read-only — no type changes)", chars, types, types) + if first_strong_char is not None: + shown_char = format_char_for_table(first_strong_char.character) + print(f"--- Rule P2 --- first strong character outside isolates: " + f"index {first_strong_char.index} " + f"(U+{ord(first_strong_char.character):04X} {shown_char}, " + f"type {first_strong_char.character_type}) " + f"-> level {paragraph_level}") + else: + print(f"--- Rule P3 --- no strong character outside isolates " + f"-> default level {paragraph_level}") return paragraph_level From a929bd6203c3e38eff90e8d7f6bbd9a5c5fdb934 Mon Sep 17 00:00:00 2001 From: isgav <51133755+isaranga@users.noreply.github.com> Date: Mon, 30 Nov 2026 12:13:48 +0200 Subject: [PATCH 2/2] Check STRONG_TYPES once in the P2 scan, then pick the level The scan tested R/AL and L in two branches that both stored the character and stopped. It now tests STRONG_TYPES once and chooses the level from the type: 0 for L, 1 for R or AL. Co-Authored-By: Claude --- bidi.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/bidi.py b/bidi.py index 2e9089a..34487bb 100644 --- a/bidi.py +++ b/bidi.py @@ -127,13 +127,12 @@ def determine_paragraph_embedding_level(chars: list[CharacterInfo], verbose: boo if open_isolate_count > 0: open_isolate_count -= 1 elif open_isolate_count == 0: - if char_type in ('R', 'AL'): - paragraph_level = 1 - first_strong_char = ci - break - elif char_type == 'L': - paragraph_level = 0 # explicit for symmetry with the RTL case above + if char_type in STRONG_TYPES: first_strong_char = ci + if char_type == 'L': + paragraph_level = 0 + else: # R or AL + paragraph_level = 1 break if verbose: