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 @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 16 additions & 4 deletions bidi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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]
Expand Down Expand Up @@ -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)


# ============================================================
Expand Down
7 changes: 4 additions & 3 deletions demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 שלום (עולם)",
Expand Down
Loading