Every passage in Bayan Library keeps its book’s own spelling, and that is the only spelling of it anyone is shown. Search reads two other spellings of it: one for keyword search, one for the embedding model. This post is about why they disagree.
One word, written many ways
A book may print a word fully vowelled, and a query may type it bare. A fully vowelled text has twice the characters for the same words. The letters vary too: an alif with or without its hamza, a final ى or ي, a final ة or ه. Compare characters as they are, and a query finds only the spelling it was typed in.
Take وَالصَّابِرِينَ, “and those who are patient”. Keyword search writes it والصابرين, and its light stem is صابر. In the 25 sample tafsirs on our machines, that exact form is in 133 passages. Words whose stem is صابر, such as الصابرون, للصابرين or والصابرات, are in 707 passages. A query for one form finds them all, and gives the exact form more weight.
The keyword spelling
normalize() makes the copy the keyword index holds. It removes the harakat and every other mark written above, below or between the letters, the dagger alif and the Qur’anic signs among them. Then it makes the letters that vary into one: أ إ آ ٱ become ا, ى and ئ become ي, ؤ becomes و, ة becomes ه. Digits become 0–9, honorific signs such as ﷺ become a word break, and punctuation goes. Queries pass through the same function, so a query typed without harakat finds text printed with them.
The index is SQLite FTS5 with content='': it keeps no text, only the index built from this copy and its stems. The Qur’an, book 0 of the library, is shown in Tanzil’s Uthmani text and searched in its Simple Clean text, the spelling people type. Even after normalize(), Uthmani spellings do not meet the ordinary ones.
The model spelling
An embedding model wants something else. forModel() strips the same marks and leaves every letter as written. It keeps punctuation and line breaks, writes ﷺ out in full, and drops the newer honorific signs the books use (U+FD40–FD4F, U+FDFE and U+FDFF, added in Unicode 14), which tokenizers made before them have never seen. Passages are embedded in this form, with their book and headings in front, and so are queries. The reranker reads it too.
// src/arabic.ts: one input, two spellingsnormalize("قَالَ: إِنَّ الصَّلَاةَ عَلَى وَقْتِهَا"); // "قال ان الصلاه علي وقتها"forModel("قَالَ: إِنَّ الصَّلَاةَ عَلَى وَقْتِهَا"); // "قال: إن الصلاة على وقتها" normalize("قال النبي ﷺ"); // "قال النبي"forModel("قال النبي ﷺ"); // "قال النبي صلى الله عليه وسلم"
The split comes from evidence we read while choosing models. In a study of retrieval over classical Arabic poetry, stripping harakat before embedding raised Recall@5 by 27 to 47 points. Unifying the forms of alif, ya and ta marbuta, the step keyword search depends on, hurt dense retrieval in the same study. So the model gets the letters without their vowels, but as they are written. Our own test on tafsir has not been run: the passages that answer its queries still have to be chosen by a person who reads tafsir.
A light stemmer, and a name it leaves whole
Arabic writes “and”, “by”, “the” and several endings onto the word, so without a stemmer والصلاة would not find الصلاة. Ours is Larkey’s light10, from the SIGIR 2002 paper and the UMass TREC 2002 notes, with Lucene’s tests among our own. It runs on the keyword spelling, in three steps:
- Remove و (“and”) if three or more letters remain.
- Remove one article, ال وال بال كال فال لل, if two or more letters remain.
- Go once through the endings ها ان ات ون ين يه ه ي, in that order, removing each one the word ends with, if two or more letters remain.
Lucene removes one prefix, so وبالكتاب keeps بال. We follow the paper, so it stems to كتاب, as الكتاب does.
The plain rules have one result we could not keep. They turn الله, والله and بالله into له, “for him”, and اللهم into لهم, “for them”. So stem() looks for the name of Allah first, with whatever is written onto it: والله, لله, تالله and فوالله all stem to الله, and اللهم stays whole. Light10’s other collisions are kept as published: إله and إلى both stem to ال. Every query word is looked up both ways, and bm25 weighs a match on the exact form twice as high as one on the stem.
Phrases first
A query of two or more words is also run as a phrase over the keyword spelling: its first 12 words, stopwords included. Phrase hits rank first, then passages with every word, then, only when those find fewer than ten, passages with any of the query’s three rarest words. This is what the Worker builds for الصبر عند المصيبة:
// buildMatch("الصبر عند المصيبة"), src/search/keyword.ts{phrase: 'norm:"الصبر عند المصيبه"',all: '(norm:"الصبر" OR stem:"صبر") AND (norm:"المصيبه" OR stem:"مصيب")',any: '(norm:"الصبر" OR stem:"صبر") OR (norm:"المصيبه" OR stem:"مصيب")',terms: ['(norm:"الصبر" OR stem:"صبر")', '(norm:"المصيبه" OR stem:"مصيب")'],words: ['الصبر', 'المصيبه'],}
عند is a stopword, so it leaves the word tiers and stays in the phrase. The phrase reads the exact form only. In our local index the phrase الصبر الجميل is in 19 tafsir passages, and 6 more have only والصبر الجميل. The phrase misses those six; the every-word tier finds them.
Words that are everywhere
Of the 152,294 searchable passages in the 25 samples, من is in 84%, في in 79%, الله in 70%, ان in 67%, قال in 66% and علي (على) in 62%. bm25 gives such words little weight, and they are the slowest to look up, so the word tiers drop stopwords. The list is in the keyword spelling: the function words among the 150 most frequent terms of all 275 tafsirs and the Qur’an, and the question words a person asks with, such as هل and كيف, which the every-word tier would otherwise demand of every passage.
Words just as frequent stay off the list because people search for them: the name of Allah in every form, the words of names and chains of narration such as ابن and عبد, and content words such as رسول and الناس. For those there is a guard instead. When every word left after the stopwords is in more than a fifth of the passages, as with قال الله تعالى, where only الله is left, keyword search runs the phrase alone, in book order rather than by bm25: bm25 would read every passage that holds the phrase before giving the first, and in book order the scan stops once it has enough. A lone الله has no phrase, so keyword search finds nothing for it and leaves it to meaning search: a word that is in every kind of passage gives keywords nothing to rank by.
What it costs
These are local measurements from one run, made before that guard, on a SQLite 3.54.1 copy of the 25 samples under Node 24: CPU time only, with no D1 overhead and no network. Medians:
| Query | What ran | Median |
|---|---|---|
| A rare word | one statement | 0.07 ms |
| الصبر عند المصيبة | the Worker’s keyword search | 0.5 ms |
| The five-word phrase of 2:156 | one phrase statement | 0.61 ms |
| الله alone | one statement | 58 ms |
| Eleven common words and one found nowhere | the Worker’s keyword search | 96 ms |
Common words are the cost: bm25 scores every passage a query matches before it returns the first, and the commonest words are in most of the library. D1 runs one query at a time per database, so the search bounds what it sends: at most 12 words in the phrase and word tiers, 3 in the any-word tier, and the phrase alone when every word is common. The search’s own notes put a lone الله, ranked by bm25, at about 0.4 s over all 275 tafsirs and the Qur’an. That is the statement the guard no longer sends.
Neither is shown
Both spellings exist to bring a query to the right page, and neither is shown. The keyword spelling lives only in the index, the model’s only on its way into a model. What a person reads is the passage as the book prints it, cited to where it stands in its book. That is the third spelling, and the only one that is the book’s own.