Labels are stored as presentation forms (shaping applied before saving)

#2
by syamjithnk - opened

Hello, and thank you for publishing this openly.

While auditing public Arabic datasets for a specific storage defect, this one came up, and I think the labels have a fixable problem. I have checked it carefully rather than trusting a tool, so here is exactly what I see.

The text labels are stored as presentation forms, in visual order.

A sample row reads:

ﺑﺪﺃﺕ ﻗﺮﺍﺀﺓ ﺍﻟﺼﻔﺤﺎﺕ ﺍﻷﻭﻟﻰ ﻋﻠﻰ ﻧﻐﻤﺎﺕ

The intended text is بدأت قراءة الصفحات الأولى على نغمات. Two separate things happened to it:

  1. Each letter was replaced by its contextual glyph form. The characters above are ARABIC LETTER BEH INITIAL FORM (U+FE91), ARABIC LETTER DAL FINAL FORM, and so on, instead of the plain letters ب and د. Those codepoints exist for legacy rendering, not for storing text.
  2. The words were then written in visual order rather than logical order.

This is the signature of arabic_reshaper.reshape() followed by bidi.algorithm.get_display(), applied before writing the file. It is an extremely common recipe and for a long time it was the correct advice, so this is a very easy thing to inherit from a preprocessing step.

I checked 400 rows across four offsets: 400 of 400 text fields are affected, with 23 to 29 presentation forms each.

Why it matters here specifically: in an OCR dataset the text field is the ground truth. A model trained on it learns to output presentation forms in visual order, so its predictions will not match ordinary Arabic text, and normal string comparison against real Arabic will fail even when the model is right.

A caution about fixing it. Undoing this is not simply NFKC plus reverse. Where the text contains a lam-alef ligature (ﻻ, U+FEFB), that single codepoint decomposes into two in logical order while the surrounding text is in visual order, so the pair comes back swapped. The result is a real Arabic word, just the wrong one, which survives a proofread. Some rows here are affected by that and some are not.

If it helps, pip install arabic-lint reports which spans can be reversed safely and which cannot, and refuses to auto-fix the unsafe ones. I wrote it, so treat that as a pointer rather than a recommendation; the analysis above stands on its own and you can verify every claim with unicodedata.name().

If the original text is still available upstream of whatever produced these labels, regenerating from it will be cleaner than any repair.

Happy to share the per-row breakdown if that would be useful.

I need to correct the report above. I said the labels are in visual order and that arabic_reshaper.reshape() was followed by bidi.algorithm.get_display(). That is wrong. Only the shaping step ran. The words are in normal logical order, and plain NFKC recovers them with no reversal of any kind:

>>> import unicodedata
>>> unicodedata.normalize("NFKC", "ﺑﺪﺃﺕ ﻗﺮﺍﺀﺓ ﺍﻟﺼﻔﺤﺎﺕ ﺍﻷﻭﻟﻰ ﻋﻠﻰ ﻧﻐﻤﺎﺕ")
'بدأت قراءة الصفحات الأولى على نغمات'

That is the row I quoted to you, and it comes back correct; had get_display() run it would have come back word-reversed. The same thing holds structurally across the sample: of 2,134 words carrying more than one presentation form, 0 begin with a FINAL form, which is what a word in visual order looks like read from memory.

So please ignore the lam-alef caution I gave, because it only applies to text that really is in visual order and yours is not. ﻻﺑﻌﺪ becomes لابعد and ﻭﻻﻧﻲ becomes ولاني, both correct under plain NFKC, and I found no row in the sample where NFKC alone was unsafe. I told you your problem was harder than it is, in public, and I am sorry for that.

What does still stand is the first half: the labels store presentation forms instead of plain letters, 400 of 400 sampled fields carry them, and for an OCR ground truth that is worth fixing, since a model trained on these labels learns to emit glyph forms that will not compare equal to ordinary Arabic. My count of "23 to 29 forms per label" was also too narrow; in my sample (400 labels, four windows) the real range is 4 to 41, median 24. The title of this discussion is wrong in the same way as the body, and I am renaming it so it does not mislead anyone who only reads the title.

Every number above is reproducible with unicodedata.name() on the rows as served by the datasets viewer.

syamjithnk changed discussion title from Labels are stored as presentation forms in visual order (reshape+bidi applied before saving) to Labels are stored as presentation forms (shaping applied before saving)

Sign up or log in to comment