Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,15 @@ 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)
```

```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 |
Expand Down
31 changes: 21 additions & 10 deletions bidi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -126,19 +127,29 @@ 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
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:
# 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

Expand Down
Loading