File size: 9,090 Bytes
ae9c7a5
 
 
 
 
 
dfe0cfc
ba34f47
 
 
 
 
 
 
 
 
 
fa5bb14
dfe0cfc
 
ba34f47
 
 
 
 
 
7372f12
ba34f47
dfe0cfc
ba34f47
 
 
 
 
 
 
 
9129401
dfe0cfc
 
 
ba34f47
 
 
 
dfe0cfc
ba34f47
 
 
dfe0cfc
 
e8d0e8c
dfe0cfc
ba34f47
dfe0cfc
 
ba34f47
dfe0cfc
 
 
ba34f47
dfe0cfc
ba34f47
 
 
 
dfe0cfc
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
---
base_model: dicta-il/BEREL_3.0
language: [he, arc]
library_name: transformers
tags: [hebrew, abbreviations, roshei-tevot, sefaria]
---
# BeRTevot: expanding Hebrew and Aramaic abbreviations in context (BEREL 3.0, siamese), v6 (Nov 25, 2026)

Picks the meaning of a Hebrew or Aramaic abbreviation (roshei tevot, e.g. 专"砖, or a truncated word, e.g. 讗讞专讬') in context, by ranking the **sense classes** the Sefaria roshei tevot lexicon lists for it.

**This repository is a complete release, not just weights.** The model only ranks the readings it is offered, so the lexicon, the per-reading embeddings, the sense classes and the code that builds candidates are all here, from one build, so they cannot be paired wrongly:

| path | what |
|---|---|
| `model.safetensors`, `config.json`, tokenizer files, `roshei_tevot_config.json` | the siamese model (BEREL 3.0 fine-tuned; `logit_scale`, span pooling) |
| `index/lexicon.json`, `index/embeddings.npy` | which readings a token can have (with library frequencies), one fp16 embedding per reading (135 MB) |
| `senses.json` | sense classes: readings that are spellings of one answer (`讗讬讻讗 讚讗诪专讬` / `讗讬转 讚讗诪专讬谉`) |
| `train_counts.json` | per key, how many training examples each reading labels: the frequency prior (2 MB) |
| `book_tables.json` | optional per-book tables: the readings a book uses for its own abbreviations (see below) |
| `roshei_tevot/` | the inference code (`predict.py`, `book_tables.py`, candidate generation, encoding, detection) |

## Use
```python
import sys
from huggingface_hub import snapshot_download

folder = snapshot_download("Sefaria/BeRTevot")
sys.path.insert(0, folder)
from roshei_tevot.predict import RosheiTevot                          # needs torch, transformers, tokenizers, numpy

rt = RosheiTevot.from_pretrained(folder)                              # CPU is fine: candidates are embedded in advance
for t in rt.expand('讗诪专 专"砖 讘谉 诇拽讬砖 讘驻"讘 讚砖讘转, 讜讘住拽"讙 讻转讘 讛专诪"讗')["tokens"]:
    best = t["senses"][0]
    print(t["surface"], "->", best["text"], round(best["probability"], 2), best["label"])
```
`expand` returns the cleaned text and, per abbreviation found, `senses`: sense classes best first (`probability` summed over a class's spellings, `readings` with each spelling's share, `label` the English gloss). A token the lexicon has no reading for gets empty `senses`.

**Per-book tables (optional).** If you know the book a passage is from, pass its English index title: `rt.expand(text, book="Mishnah Berurah")` (or `rt.score(rows)` with a `book` in each row). A book with a table also gets its own readings of the key offered as candidates when the lexicon lacks them (`table_inject`) and boosted by `table_boost` (5.0 for a book's own table, 6.0 for the Rashi and Tosafot family tables; leave-one-out over 49 books: 89.9% top-1 for the model alone, about 95% with the tables); Rashi on the Talmud and Tosafot have one table each that serves every tractate, layered under a volume's own table. `rt.book_tables.titles()` lists the books. Tables were built from passages whose readings two independent LLM readers agreed on (not human gold) and are strictly a prior: without a `book` nothing changes.

**Transformers 4 and 5.** This release loads the tokenizer from `tokenizer.json` itself (`roshei_tevot.encoding.load_tokenizer`). BEREL's file defines its own pre-tokenizer, which keeps abbreviations like `转谞"讱` and `专"砖` whole; transformers 5's `AutoTokenizer` ignores it, splits abbreviations into pieces the model never saw, and loses about 10 points of top-1 (on 250 majority-sense validation rows: 95.6% with the correct tokenization against 85.2% with `AutoTokenizer` under transformers 5.21, at prior weight 0). If you write your own loading code, do not use `AutoTokenizer.from_pretrained` on this folder with transformers 5; use `load_tokenizer`.

## What the code does around the model (and why you should not skip it)
1. **Detect** tokens that look like abbreviations, with attached prefix letters; drop letter and divine names written with gershayim (讗诇"祝, 讛讜讬"讛): the model read them at p~1.
2. **Offer candidates**: the lexicon's readings for the token (as written or behind a prefix, letters must fit); the **number** for a numeral (`讬"讚` -> `讗专讘注讛 注砖专`, also behind a prefix, also one-letter geresh numerals `讘讙'`); `住注讬祝 拽讟谉 X` for `住拽"X`; for a **truncated word** (`诇讻转讞讬'`) the 8 most frequent library words that start with its letters; citation readings (`住拽讬"讗`), but never for a bare numeral or a token ending in a geresh (there they win with `拽讬爪讜专` / `住讬诪谉 讬'`).
3. **Encode**: the token wrapped in the added `<abbr>` markers, 200 words of context each side, 512 tokens, no title or category; the model's vector is the mean over the marked span.
4. **Score**: softmax over `logit_scale * cosine + prior_weight * log(1 + count)`, where `count` is how many *training* examples of the key the reading labels (`train_counts.json`), as in the evaluation. `prior_weight` defaults to 0.5 (0 when the counts file is absent). On the validation split the prior is worth about 1.3 points of top-1 (94.2% at weight 0, 95.5% at 0.5), but the gain comes from the frequent readings and the rarer ones pay for it: for answers that are under 10% of their key's examples (104 validation rows) top-1 is 81.7% at weight 0 and 76.0% at 0.5. **Do not raise it much above 0.5.** A reading the training set never saw (a recent lexicon addition) has count 0 and is pushed down by `prior_weight * log(1 + the other readings' counts)`: about 3.5 logits, a factor of 30, at 0.5 for a reading seen 1,000 times. Lower it, or pass `prior_weight=0`, for text unlike the library (a different genre) or when you want the lexicon's newest readings to compete evenly.
5. **Group by sense** and sum probabilities.

## Training and data
- Base `dicta-il/BEREL_3.0`; one epoch of 600,000 rows sampled from 1,062,365 (weight 1/count(key)^0.5, synthetic rows 0.5, sense balancing 尾 0.5); spot A100, Nov 25, 2026. Run `rt-numbers-v6-siamese-2026-11-25`. Code: `train-roshei-tevot` in Sefaria/ML-Workflows; this release is assembled by `app/roshei_tevot/publish_hf.py` there.
- Data: Sefaria's roshei tevot dataset (v6, not public; the lexicon, sense classes and counts it produced are in this repo). Built by Sefaria/translation-engine `experiments/expand_roshei_tevot` (`NUMBERS.md` has the rounds v3, v4, v5b; `rebuild_v6.sh` rebuilds it).
- v6 = v5b plus: 108 lexicon readings from two rounds of gap sweeps over the library; `讘砖谞讬`, `讘砖谞讬诐` and `讘砖转讬` removed from the readings of `讘'` (they came from first-letter alignment: the key is the number two, and the prefixed forms are not readings of it), and `驻拽讬"讗` = `驻专拽 拽讬"讗` (a citation pattern) removed. The model was trained on the dataset built before these last removals took effect (a later build step had put three readings back); the `index/` here is built from the corrected lexicon with the same model. The hyperparameters and the training procedure are the same as v5b.
- `senses.json` also holds 405 readings that were added to the lexicon after the dataset's sense classes were built, merged into the class of their spelling variants (or into a new class of their own) after two rounds of independent model readers agreed; the other candidate merges (inflected forms and the like) were left separate.

## Evaluation (v6 splits; top-1 over rows with 2+ candidates and the label among them; prior weight 0.5)
| split | answerable | v6 | v5b |
|---|---|---|---|
| val | 3,210 | 95.5 | 95.6 |
| test_seen_keys | 4,900 | 95.5 | 95.4 |
| test_unseen_keys | 336 | 89.6 | 88.7 |

Both models were scored on the same rows with the same code, v5b's index and v6's. **The two are equal within noise.** On val the balanced top-1 (mean over the majority, common and rare sense bands) is 87.8 for v6 and 88.7 for v5b at prior weight 0.5 (89.2 against 88.8 at weight 0); that gap is 6 rows. On the 554 val rows of the common and rare bands, 20 are right only for v5b and 14 only for v6 (McNemar p = 0.39); the rows v6 loses are mostly ambiguous rabbi names (`专"讬` as 讬讜住讬, 讬讜讞谞谉 or 讬讜讚谉; `诪讜讛专"谉` as 谞讞诪谉 or 谞转谉), and v6 wins rows of the same kind. The +0.9 on unseen keys is 3 rows of 336 and is not significant. v6's value is the corrected lexicon, not a measured accuracy gain.
Not measured on this revision: the comparison with Dicta (v4: 91.0% target accuracy on 500 held-out passages against Dicta's 58.0%).

## Known issues
- Whether a token is an abbreviation at all is decided by `detect`, not the model; common words with a geresh can be offered readings.
- Candidates are the lexicon's: a reading it lacks cannot be chosen, and `test_unseen_keys` (abbreviations never seen in training) is the weak spot (89.6%).
- Rabbi names written as the same abbreviation (`专"讬`) remain the largest source of errors; a book table or the author's other works help only where the book's habit is stable.