syamjithnk commited on
Commit
9650459
·
verified ·
1 Parent(s): 6411f3e

Correct the Yousefmd finding: only shaping ran, not bidi

Browse files

Re-measured on 1,000 labels across the whole 20,000-row split: 3 to 50 forms per label, 0 of 5,423 multi-form words begin with a FINAL form, and reshape(NFKC(stored)) reproduces the stored string for 1,000 of 1,000. Ships verify_yousefmd.py so every figure is re-derivable from one sample.

Files changed (3) hide show
  1. FINDINGS.md +18 -11
  2. README.md +11 -8
  3. verify_yousefmd.py +102 -0
FINDINGS.md CHANGED
@@ -38,21 +38,28 @@ scanned with the detector from `arabic-lint` 0.5.0. 100 rows per dataset, first
38
  ### One dataset carries presentation forms in every sampled label, and it is an OCR ground-truth set
39
 
40
  **`Yousefmd/arabic_ocr_dataset`** — every text field stores presentation forms:
41
- 4 to 54 per label (median 24), INITIAL, MEDIAL, FINAL and ISOLATED forms together.
42
- Verified across four sample windows: **400 of 400 fields, 100%**, 444 of 457 findings being
43
- runs of five or more forms.
 
44
 
45
  **Only the shaping half of the recipe ran.** The words are in logical order, so
46
  `unicodedata.normalize("NFKC", stored)` alone recovers the text and no reversal is wanted.
47
  Two independent checks against the live rows:
48
 
49
- - of **2,130** sampled words carrying more than one presentation form, **0** begin with a
50
- FINAL form and **0** meaningfully end with an INITIAL form (one does, `الـ`, and that is a
51
- correctly shaped lam before a tatweel, not visual order). A word written in visual order
52
  reads FINAL-first from memory, so zero is decisive.
53
- - `arabic_reshaper.reshape(NFKC(stored)) == stored` for **400 of 400** fields. The stored
54
  string is exactly `reshape()` output with `get_display()` never applied.
55
 
 
 
 
 
 
 
56
  This matters more than its size because the corrupted field is the **label**. A model
57
  trained on it learns to emit presentation forms instead of plain letters, so its output will
58
  not compare equal to ordinary Arabic even when the recognition is correct. That is the
@@ -137,9 +144,9 @@ signature". Only the first ran.
137
 
138
  Measured against the live rows on 13 September: plain `unicodedata.normalize("NFKC", stored)`
139
  returns `بدأت قراءة الصفحات الأولى على نغمات`, correct readable Arabic in logical order, with
140
- no reversal of any kind. Structurally, 0 of 2,130 multi-form words begin with a FINAL form.
141
  And `arabic_reshaper.reshape(NFKC(stored))` reproduces the stored string byte for byte in
142
- 400 of 400 fields, which pins down exactly which half ran.
143
 
144
  **The part that mattered was the advice, not the wording.** The discussion warned that
145
  recovery "is not simply NFKC plus reverse" because a lam-alef ligature would come back
@@ -147,8 +154,8 @@ swapped. That hazard exists only when the surrounding text is in visual order. H
147
  not: `ﻻﺑﻌﺪ` becomes `لابعد` and `ﻭﻻﻧﻲ` becomes `ولاني`, both correct. We told a maintainer
148
  his problem was harder than it is, in public, on his own repository.
149
 
150
- Also corrected in the same pass: "23 to 29 presentation forms per label" (real range 4 to
151
- 54, median 24), and "fully corrupted" where "carries stored presentation forms in every
152
  sampled field" is what was measured.
153
 
154
  **Cause.** The detector reports stored glyph forms. Word order is a separate property, was
 
38
  ### One dataset carries presentation forms in every sampled label, and it is an OCR ground-truth set
39
 
40
  **`Yousefmd/arabic_ocr_dataset`** — every text field stores presentation forms:
41
+ 3 to 50 per label (median 24), INITIAL, MEDIAL, FINAL and ISOLATED forms together.
42
+ Verified on **1,000 of 1,000 labels, 100%**, sampled through ten windows spanning the whole
43
+ 20,000-row split; in the audit's own 100-row scan, 444 of 457 findings are runs of five or
44
+ more forms. Every figure here is re-derivable with `python3 verify_yousefmd.py`.
45
 
46
  **Only the shaping half of the recipe ran.** The words are in logical order, so
47
  `unicodedata.normalize("NFKC", stored)` alone recovers the text and no reversal is wanted.
48
  Two independent checks against the live rows:
49
 
50
+ - of **5,423** sampled words carrying more than one presentation form, **0** begin with a
51
+ FINAL form and **0** meaningfully end with an INITIAL form (one does, `الـ20`, and that is
52
+ a correctly shaped lam before a tatweel, not visual order). A word written in visual order
53
  reads FINAL-first from memory, so zero is decisive.
54
+ - `arabic_reshaper.reshape(NFKC(stored)) == stored` for **1,000 of 1,000** labels. The stored
55
  string is exactly `reshape()` output with `get_display()` never applied.
56
 
57
+ ⚠️ The counts above are the corrected ones. A first pass classified a presentation form by
58
+ testing the Unicode name for `ARABIC LETTER ... FORM`, which **silently drops every lam-alef
59
+ ligature** (U+FEF5-FEFC are named `ARABIC LIGATURE ...`) — an undercount sitting exactly on
60
+ the character the visual-order test is most sensitive to. The check now asks arabic-lint's own
61
+ `is_presentation_form()`, so these numbers cannot drift from what the tool reports.
62
+
63
  This matters more than its size because the corrupted field is the **label**. A model
64
  trained on it learns to emit presentation forms instead of plain letters, so its output will
65
  not compare equal to ordinary Arabic even when the recognition is correct. That is the
 
144
 
145
  Measured against the live rows on 13 September: plain `unicodedata.normalize("NFKC", stored)`
146
  returns `بدأت قراءة الصفحات الأولى على نغمات`, correct readable Arabic in logical order, with
147
+ no reversal of any kind. Structurally, 0 of 5,423 multi-form words begin with a FINAL form.
148
  And `arabic_reshaper.reshape(NFKC(stored))` reproduces the stored string byte for byte in
149
+ 1,000 of 1,000 labels, which pins down exactly which half ran.
150
 
151
  **The part that mattered was the advice, not the wording.** The discussion warned that
152
  recovery "is not simply NFKC plus reverse" because a lam-alef ligature would come back
 
154
  not: `ﻻﺑﻌﺪ` becomes `لابعد` and `ﻭﻻﻧﻲ` becomes `ولاني`, both correct. We told a maintainer
155
  his problem was harder than it is, in public, on his own repository.
156
 
157
+ Also corrected in the same pass: "23 to 29 presentation forms per label" (real range 3 to
158
+ 50, median 24), and "fully corrupted" where "carries stored presentation forms in every
159
  sampled field" is what was measured.
160
 
161
  **Cause.** The detector reports stored glyph forms. Word order is a separate property, was
README.md CHANGED
@@ -95,18 +95,21 @@ spans are in a single dataset.**
95
 
96
  **276 datasets readable · 26,318 rows · 119,517 text fields · 21 with any finding · 1 with a presentation form in every sampled field.**
97
 
98
- **`Yousefmd/arabic_ocr_dataset` stores presentation forms in every sampled label** — 400 of
99
- 400 fields across four sample windows, 4 to 54 presentation forms per label (median 24),
100
- INITIAL/MEDIAL/FINAL/ISOLATED forms together.
101
 
102
  **Only the shaping half of the recipe ran there.** The words are in normal **logical**
103
  order, not visual order, so `unicodedata.normalize("NFKC", text)` alone recovers the
104
- text correctly and no reversal is needed. Measured two ways: on 2,130 sampled words
105
  carrying more than one presentation form, **0 begin with a FINAL form**, which is what
106
  visual order looks like in memory; and `arabic_reshaper.reshape(NFKC(stored)) == stored`
107
- for **400 of 400** fields, so the stored string is exactly `reshape()` output with
108
  `get_display()` never applied. An earlier version of this card claimed visual order. That
109
- was wrong. See *Corrections*.
 
 
 
110
 
111
  It matters because the corrupted field is the **label** of an OCR set: a model trained on
112
  it learns to emit presentation forms rather than plain letters, so its output will not
@@ -175,12 +178,12 @@ findings write-up and a discussion opened on that repository all stated that the
175
  been through `arabic_reshaper.reshape()` **and** `bidi.algorithm.get_display()`, and called
176
  that "the recipe's complete signature". Only the shaping step ran. Verified two ways against
177
  the live rows: plain `unicodedata.normalize("NFKC", stored)` returns correct readable Arabic
178
- in logical order with no reversal, and of 2,130 multi-form words **0** begin with a FINAL
179
  form. The practical consequence is that the repair advice given was wrong in the worse
180
  direction: it warned that recovery needed more than NFKC because of the lam-alef ligature,
181
  when for this data **NFKC alone is a complete and safe repair**. The counts were sound; the
182
  mechanism inferred from them was not. Also corrected in the same pass: "23 to 29 presentation
183
- forms per label" (real range 4 to 54, median 24) and the headline word "corrupted" where
184
  "carries stored presentation forms" is what was actually measured.
185
 
186
  **Cause, stated plainly.** The detector reports stored glyph forms. Visual order is a
 
95
 
96
  **276 datasets readable · 26,318 rows · 119,517 text fields · 21 with any finding · 1 with a presentation form in every sampled field.**
97
 
98
+ **`Yousefmd/arabic_ocr_dataset` stores presentation forms in every sampled label** — 1,000 of
99
+ 1,000 labels, sampled through ten windows spanning the whole 20,000-row split, 3 to 50
100
+ presentation forms per label (median 24), INITIAL/MEDIAL/FINAL/ISOLATED forms together.
101
 
102
  **Only the shaping half of the recipe ran there.** The words are in normal **logical**
103
  order, not visual order, so `unicodedata.normalize("NFKC", text)` alone recovers the
104
+ text correctly and no reversal is needed. Measured two ways: on 5,423 sampled words
105
  carrying more than one presentation form, **0 begin with a FINAL form**, which is what
106
  visual order looks like in memory; and `arabic_reshaper.reshape(NFKC(stored)) == stored`
107
+ for **1,000 of 1,000** labels, so the stored string is exactly `reshape()` output with
108
  `get_display()` never applied. An earlier version of this card claimed visual order. That
109
+ was wrong. See *Corrections*. Both figures are re-derivable with the shipped
110
+ `verify_yousefmd.py`, which asks arabic-lint's own `is_presentation_form()` rather than
111
+ matching Unicode names — a name test for `ARABIC LETTER ... FORM` drops every lam-alef
112
+ ligature, which is the one character this measurement is most sensitive to.
113
 
114
  It matters because the corrupted field is the **label** of an OCR set: a model trained on
115
  it learns to emit presentation forms rather than plain letters, so its output will not
 
178
  been through `arabic_reshaper.reshape()` **and** `bidi.algorithm.get_display()`, and called
179
  that "the recipe's complete signature". Only the shaping step ran. Verified two ways against
180
  the live rows: plain `unicodedata.normalize("NFKC", stored)` returns correct readable Arabic
181
+ in logical order with no reversal, and of 5,423 multi-form words **0** begin with a FINAL
182
  form. The practical consequence is that the repair advice given was wrong in the worse
183
  direction: it warned that recovery needed more than NFKC because of the lam-alef ligature,
184
  when for this data **NFKC alone is a complete and safe repair**. The counts were sound; the
185
  mechanism inferred from them was not. Also corrected in the same pass: "23 to 29 presentation
186
+ forms per label" (real range 3 to 50, median 24) and the headline word "corrupted" where
187
  "carries stored presentation forms" is what was actually measured.
188
 
189
  **Cause, stated plainly.** The detector reports stored glyph forms. Visual order is a
verify_yousefmd.py ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Re-derive every published number about `Yousefmd/arabic_ocr_dataset` from one defined sample.
3
+
4
+ One sample, one set of figures, so nothing quoted anywhere can disagree with anything else.
5
+ Reads rows through the datasets-server (no download). `arabic_reshaper` is optional: without
6
+ it the round-trip line is skipped rather than guessed.
7
+
8
+ python3 verify_yousefmd.py
9
+ """
10
+ from __future__ import annotations
11
+
12
+ import json
13
+ import statistics
14
+ import sys
15
+ import unicodedata
16
+ import urllib.parse
17
+ import urllib.request
18
+ from pathlib import Path
19
+
20
+ sys.path.insert(0, str(Path(__file__).resolve().parent.parent / "arabic_lint" / "src"))
21
+ from arabic_lint.detect import is_presentation_form # noqa: E402
22
+
23
+ DATASET = "Yousefmd/arabic_ocr_dataset"
24
+ WINDOWS = 10 # evenly spaced across the split
25
+ PER_WINDOW = 100 # rows per window (the API maximum)
26
+
27
+
28
+ def fetch(offset: int, length: int) -> dict:
29
+ query = urllib.parse.urlencode(
30
+ {"dataset": DATASET, "config": "default", "split": "train",
31
+ "offset": offset, "length": length}
32
+ )
33
+ url = f"https://datasets-server.huggingface.co/rows?{query}"
34
+ with urllib.request.urlopen(url, timeout=90) as response:
35
+ return json.load(response)
36
+
37
+
38
+ def presentation_form(char: str) -> str | None:
39
+ """Return ISOLATED/INITIAL/MEDIAL/FINAL for a presentation form, else None.
40
+
41
+ Whether a codepoint counts as a presentation form is decided by arabic-lint itself,
42
+ so these figures cannot drift from what the tool reports. The positional class is then
43
+ read off the Unicode NAME, which covers the lam-alef *ligature* forms (U+FEF5-FEFC)
44
+ as well as the plain letter forms - their names are `ARABIC LIGATURE ...`, so a check
45
+ that only accepts `ARABIC LETTER ...` silently undercounts every lam-alef.
46
+ """
47
+ if not is_presentation_form(char):
48
+ return None
49
+ tail = unicodedata.name(char).split()[-2:]
50
+ return tail[0] if tail[1] == "FORM" and tail[0] in {
51
+ "ISOLATED", "INITIAL", "MEDIAL", "FINAL"} else None
52
+
53
+
54
+ def main() -> None:
55
+ total = fetch(0, 1)["num_rows_total"]
56
+ offsets = [min(total * i // WINDOWS, max(total - PER_WINDOW, 0)) for i in range(WINDOWS)]
57
+
58
+ labels: list[str] = []
59
+ for offset in offsets:
60
+ for row in fetch(offset, PER_WINDOW)["rows"]:
61
+ value = row["row"].get("text")
62
+ if isinstance(value, str) and value.strip():
63
+ labels.append(value)
64
+
65
+ counts = [sum(1 for char in label if presentation_form(char)) for label in labels]
66
+ with_forms = [count for count in counts if count]
67
+
68
+ multi_form_words = 0
69
+ starting_final = 0
70
+ ending_initial: list[str] = []
71
+ for label in labels:
72
+ for word in label.split():
73
+ forms = [f for f in (presentation_form(c) for c in word) if f]
74
+ if len(forms) > 1:
75
+ multi_form_words += 1
76
+ starting_final += forms[0] == "FINAL"
77
+ if forms[-1] == "INITIAL":
78
+ ending_initial.append(word)
79
+
80
+ print(f"dataset {DATASET}")
81
+ print(f"rows in split {total}")
82
+ print(f"sample {len(labels)} labels, {WINDOWS} windows at offsets {offsets}")
83
+ print(f"carry forms {len(with_forms)} of {len(labels)}")
84
+ print(f"forms per label min {min(with_forms)} max {max(with_forms)} "
85
+ f"median {statistics.median(with_forms):.0f}")
86
+ print(f"multi-form words {multi_form_words}, starting with a FINAL form: {starting_final}")
87
+ print(f"ending INITIAL {len(ending_initial)} distinct: {sorted(set(ending_initial))[:5]}")
88
+
89
+ try:
90
+ import arabic_reshaper
91
+ except ImportError:
92
+ print("round trip skipped (arabic_reshaper not installed)")
93
+ return
94
+ matches = sum(
95
+ arabic_reshaper.reshape(unicodedata.normalize("NFKC", label)) == label
96
+ for label in labels
97
+ )
98
+ print(f"round trip reshape(NFKC(stored)) == stored for {matches} of {len(labels)}")
99
+
100
+
101
+ if __name__ == "__main__":
102
+ main()