ocrmypdf-hebrew 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or Derivative
95
+ Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work, excluding
103
+ those notices that do not pertain to any part of the Derivative
104
+ Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and do
117
+ not modify the License. You may add Your own attribution notices
118
+ within Derivative Works that You distribute, alongside or as an
119
+ addendum to the NOTICE text from the Work, provided that such
120
+ additional attribution notices cannot be construed as modifying
121
+ the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2026 paddleocr-hebrew contributors
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,251 @@
1
+ Metadata-Version: 2.4
2
+ Name: ocrmypdf-hebrew
3
+ Version: 0.1.0
4
+ Summary: OCRmyPDF plugin: Hebrew OCR (with embedded English and numbers) using the paddleocr-hebrew models
5
+ Author-email: Rivok Labs <ronen@rivoklabs.com>
6
+ License-Expression: Apache-2.0
7
+ Project-URL: Homepage, https://github.com/RivoksLab/ocrmypdf-hebrew
8
+ Project-URL: Source, https://github.com/RivoksLab/ocrmypdf-hebrew
9
+ Project-URL: Issues, https://github.com/RivoksLab/ocrmypdf-hebrew/issues
10
+ Project-URL: Write-up, https://rivoklabs.com/research/hebrew-ocr
11
+ Keywords: ocr,hebrew,ocrmypdf,pdf,rtl
12
+ Requires-Python: >=3.11
13
+ Description-Content-Type: text/markdown
14
+ License-File: LICENSE
15
+ Requires-Dist: ocrmypdf>=17.14
16
+ Requires-Dist: paddleocr-hebrew>=0.1.2
17
+ Requires-Dist: onnxruntime>=1.18
18
+ Requires-Dist: huggingface_hub>=0.23
19
+ Requires-Dist: pydantic>=2
20
+ Provides-Extra: test
21
+ Requires-Dist: pytest>=8; extra == "test"
22
+ Requires-Dist: pdfminer.six; extra == "test"
23
+ Requires-Dist: python-bidi; extra == "test"
24
+ Dynamic: license-file
25
+
26
+ # ocrmypdf-hebrew
27
+
28
+ An [OCRmyPDF](https://github.com/ocrmypdf/OCRmyPDF) plugin for Hebrew documents, including
29
+ Hebrew with English words and numbers.
30
+
31
+ It offers three things:
32
+
33
+ - **The paddleocr-hebrew engine.** With `-l heb` it replaces Tesseract with the
34
+ paddleocr-hebrew models.
35
+ - **Tesseract with the Hebrew word order fixed** (`--hebrew-tess-order`). OCRmyPDF's own
36
+ Tesseract runs, with any `-l`, and the word order of Hebrew lines that contain English or
37
+ numbers is corrected.
38
+ - **`hebrew-tess-order`**, the same word-order fix as a command-line filter for Tesseract's TSV
39
+ or hOCR output.
40
+
41
+ ## Install
42
+
43
+ ```bash
44
+ pip install ocrmypdf-hebrew # pulls OCRmyPDF 17.14 or later and paddleocr-hebrew
45
+ ```
46
+
47
+ OCRmyPDF loads the plugin automatically (entry point). The install also adds the
48
+ `hebrew-tess-order` command. OCRmyPDF 17.14.0 fixed three bugs in how earlier versions write
49
+ right-to-left text into the PDF (#1781, #1782, #1783), so the plugin needs it.
50
+
51
+ ## The paddleocr-hebrew engine
52
+
53
+ ```bash
54
+ ocrmypdf -l heb+eng scan.pdf out.pdf # this plugin's models
55
+ ocrmypdf -l eng scan.pdf out.pdf # Tesseract, untouched
56
+ ocrmypdf --ocr-engine tesseract -l heb ... # force Tesseract
57
+ ```
58
+
59
+ Options: `--hebrew-pipeline word|line`, `--hebrew-rec-mode cascade|nrtr|ctc`,
60
+ `--hebrew-models DIR` (an offline model folder) and `--hebrew-no-rejoin` (below).
61
+
62
+ **Long numbers cut in two.** The word detector sometimes covers one long number, amount or e-mail
63
+ address with two overlapping boxes, so `0585769628` would come out as `058 5769628` and
64
+ `12,203.39` as `12,203 3.39`. The plugin joins such neighbours (no Hebrew, at least one digit or
65
+ `@`, touching or overlapping) and reads them again as one word. If the new reading has a space,
66
+ they were two words after all and are kept as they were. This is on by default for the word
67
+ pipeline; `--hebrew-no-rejoin` turns it off. The same pass re-reads numbers whose last `0` the box
68
+ cut off (`21,000.0C` → `21,000.00`). On our 7 example documents, numbers found go from 83.3% to
69
+ 87.2%; on 20 synthetic pages and on old newspaper scans nothing gets worse.
70
+
71
+ ## Tesseract with the Hebrew word order fixed: `--hebrew-tess-order`
72
+
73
+ ### The problem
74
+
75
+ Tesseract chooses one direction for each paragraph. When a line has as many English words or
76
+ numbers as Hebrew words, a tie, Tesseract chooses left to right. Numbers count as left-to-right
77
+ words. Short Hebrew lines then come out with their words in the wrong order:
78
+
79
+ | on the page (read right to left) | Tesseract's text |
80
+ |---|---|
81
+ | `תאריך: 04.11.2026` | `04.11.2026 תאריך:` |
82
+ | `דוח שנתי Annual Report` | `Annual Report דוח שנתי` |
83
+ | `חיבור: Bluetooth 5.3 / USB-C` | `Bluetooth 5.3 / USB-C חיבור:` |
84
+
85
+ These are mostly headings, `key: value` fields, table cells and list items, so forms and
86
+ invoices are where it shows. Long paragraphs are rarely affected: on 34 pages of a 19th-century
87
+ Hebrew newspaper, under 3% of the Hebrew lines come out in this order with stock `heb+eng`.
88
+
89
+ ### Usage
90
+
91
+ The mode is **opt-in**: without `--hebrew-tess-order`, Tesseract's output is not touched.
92
+
93
+ ```bash
94
+ ocrmypdf --hebrew-tess-order -l heb+eng scan.pdf out.pdf --sidecar out.txt
95
+ ocrmypdf --hebrew-tess-order -l heb_rivok scan.pdf out.pdf # a custom model: see setup below
96
+ ```
97
+
98
+ - Tesseract runs exactly as OCRmyPDF runs it, with the same `--tesseract-*` options. After each
99
+ page, the plugin rewrites Tesseract's hOCR before OCRmyPDF builds the text layer.
100
+ - `--ocr-engine` can be `auto` (the default) or `tesseract`. With this flag, the paddleocr-hebrew
101
+ models are not used and not downloaded.
102
+ - `--hebrew-order-conservative`: in paragraphs of 2 or more lines, only lines with at least as
103
+ many Hebrew letters as Latin ones are reordered. Use it for documents with English passages.
104
+ It gives up most of the gain on forms.
105
+ - `--hebrew-order-cells` (**experimental, off by default**): lines are first cut at column gaps
106
+ (more than 3× the word height), and the cells are read right to left. Meant for RTL tables. It
107
+ also reverses the cell order of lines that have no Hebrew. **It has not been validated on real
108
+ documents**, so check its output before relying on it.
109
+ - **Needs the default renderer.** It does not work with `--pdf-renderer sandwich`, where
110
+ Tesseract writes the text layer and the sidecar itself. The plugin logs a warning.
111
+
112
+ ### The rule
113
+
114
+ - **Page gate.** Only pages whose letters are at least 25% Hebrew are touched. All other pages
115
+ keep Tesseract's output byte for byte. Without the gate, English pages that contain a Hebrew
116
+ word would be ruined.
117
+ - **Direction.** On a Hebrew page, every line with a Hebrew letter is read right to left. Lines
118
+ without Hebrew keep Tesseract's order.
119
+ - **English runs.** Words are taken right to left. A run of English words is kept left to right.
120
+ A run starts and ends on an English word, and numbers and punctuation between English words
121
+ belong to the run. A word containing Hebrew never joins a run. Punctuation or a number at the
122
+ edge of a run stays on the Hebrew side.
123
+
124
+ ### Which text gets the fix
125
+
126
+ | where the text is read | fixed? | why |
127
+ |---|---|---|
128
+ | the `--sidecar` text | **yes** | it is rebuilt from the rewritten hOCR |
129
+ | MuPDF (`mutool`, PyMuPDF), and other tools that follow the PDF's content order | **yes** (partly) | the content stream holds the corrected word order |
130
+ | pdftotext / poppler (Evince, Okular) | **no** | rebuilds each line from glyph positions, with one text direction for the whole page |
131
+ | pdfium (Chrome) | **no** | rebuilds each line from glyph positions, choosing each line's direction by letter majority |
132
+
133
+ Results on 20 synthetic pages with exact references (187 rows, character error rate), measured
134
+ with OCRmyPDF 17.14.0 and heb_rivok 1.0.1:
135
+
136
+ | | stock `heb+eng` | heb_rivok |
137
+ |---|---:|---:|
138
+ | sidecar, Tesseract → `--hebrew-tess-order` | 17.8% → **7.9%** | 10.8% → **2.7%** |
139
+ | MuPDF text | 15.9% → 9.7% | 10.6% → 5.1% |
140
+ | pdfium text | 23.2% → 22.4% | 18.2% → 17.5% |
141
+ | pdftotext text | 20.4% → 20.2% | 11.5% → 8.6% * |
142
+ | sidecar with `--hebrew-order-cells` | 7.1% | 2.2% |
143
+ | sidecar with `--hebrew-order-conservative` | 12.0% | 5.9% |
144
+
145
+ \* This gain is incidental. In Tesseract's left-to-right lines, Hebrew words carry stray
146
+ left-to-right marks (U+200E). Rewritten lines drop those marks, and on 1 of the 20 pages that
147
+ flips pdftotext's direction for the whole page to right to left. On the other pages pdftotext
148
+ barely changes.
149
+
150
+ - The 3 English pages and the 7 heb_rivok example documents come out unchanged.
151
+
152
+ ### Limits
153
+
154
+ - **What 17.14.0 leaves to the PDF readers.** They rebuild right-to-left lines from the glyph
155
+ positions, each in its own way:
156
+ - A few words still run together. pdftotext joins a number or an English word to the Hebrew
157
+ word after it (44 pairs on the 20 pages with stock `heb+eng`), and MuPDF a Hebrew word to
158
+ the English word after it (6 pairs). pdfium keeps them all apart.
159
+ - A word that mixes Hebrew with digits or Latin can come back with its parts in another
160
+ order. MuPDF gives `48.2-ל` for `ל-48.2`, pdftotext `בAndroid-` for `ב-Android`, and pdfium
161
+ `ל48.2-` (it keeps `ב-Android` as written). The digits and Latin letters themselves keep
162
+ their order.
163
+ - **English passages inside Hebrew pages are flipped.** For example, an English list within a
164
+ Hebrew report. Use `--hebrew-order-conservative`.
165
+ - **Things a word order cannot fix:**
166
+ - Version strings: `3.8.1 (build 512)` comes out as `3.8.1 512) (build`.
167
+ - A Hebrew prefix glued to English: `ב-Dark Mode`.
168
+ - Hebrew misread as Latin junk.
169
+ - The character order inside a token, as Tesseract read it: for example `:1` for `1:`.
170
+ - English-only cells of an RTL table. Only `--hebrew-order-cells` fixes these.
171
+ - **Bidi marks in the sidecar.** Pages with a rewritten line get their sidecar text rebuilt from
172
+ the hOCR, with no bidi marks. Other pages keep Tesseract's text, which includes them.
173
+
174
+ ### Custom models (heb_rivok) with OCRmyPDF
175
+
176
+ OCRmyPDF asks Tesseract for its `hocr` and `txt` configs. A `TESSDATA_PREFIX` that holds only
177
+ your model fails with `read_params_file: Can't open hocr`. Make a folder with links to the model
178
+ and to the system `configs/`:
179
+
180
+ ```bash
181
+ mkdir -p ~/tessdata-rivok
182
+ ln -s /path/to/heb_rivok.traineddata ~/tessdata-rivok/
183
+ ln -s /usr/share/tesseract-ocr/5/tessdata/configs ~/tessdata-rivok/configs
184
+ TESSDATA_PREFIX=~/tessdata-rivok ocrmypdf --hebrew-tess-order -l heb_rivok scan.pdf out.pdf
185
+ ```
186
+
187
+ **Pages with more than one column:** heb_rivok 1.0.1 or later reads them right column first
188
+ with `-l heb_rivok` alone; no workaround is needed. Only heb_rivok 1.0 laid its pages out left to
189
+ right (it knows more Latin letters than Hebrew ones) and read the left column first. With 1.0, use
190
+ `-l heb+heb_rivok` (also link `/usr/share/tesseract-ocr/5/tessdata/heb.traineddata` into the
191
+ folder) or `-l heb_rivok_lite+eng` (link `heb_rivok_lite.traineddata` and `eng.traineddata`), or
192
+ better, update to 1.0.1. This is a page-layout matter that `--hebrew-tess-order` does not change,
193
+ because it reorders words within lines, not columns. See the heb_rivok model card.
194
+
195
+ ## Command-line filter: `hebrew-tess-order`
196
+
197
+ It reads Tesseract's TSV or hOCR, which hold word boxes, from a file or from standard input. It
198
+ applies the same rule and page gate, and writes plain text in reading order.
199
+
200
+ ```bash
201
+ tesseract page.png - -l heb+eng tsv | hebrew-tess-order > page.txt
202
+ tesseract page.png page -l heb+eng hocr && hebrew-tess-order page.hocr --emit hocr -o page.fixed.hocr
203
+ tesseract page.png - -l heb+eng tsv | hebrew-tess-order --emit tsv > page.fixed.tsv
204
+ ```
205
+
206
+ - **Options:**
207
+ - `--conservative` and `--cells`, as above.
208
+ - `--emit text|tsv|hocr`: the rewritten input, in the same format as the input.
209
+ - `-o FILE`: write to a file instead of standard output.
210
+ - `-v`: a per-page summary on standard error.
211
+ - **Plain text cannot be fixed.** It has no boxes and no page context.
212
+ - **Text format:**
213
+ - The output is Tesseract's text layout: one line per line, with a blank line between
214
+ paragraphs and a form feed between pages.
215
+ - It has no bidi marks.
216
+ - In rewritten TSV/hOCR, the moved words lose their bidi marks, and hOCR lines get
217
+ `dir='rtl'`.
218
+ - **heb_rivok with `--tessdata-dir`.** If the folder holds only the model, Tesseract cannot find
219
+ its `tsv`/`hocr` configs and prints plain text instead. Pass the setting directly:
220
+
221
+ ```bash
222
+ tesseract page.png - --tessdata-dir /path/to/tessdata -l heb_rivok -c tessedit_create_tsv=1 | hebrew-tess-order
223
+ ```
224
+
225
+ - **Results.** On the 20 synthetic pages, the CLI text goes from 19.3% to **7.2%** CER (stock
226
+ `heb+eng`) and from 11.8% to **2.4%** (heb_rivok 1.0.1). With `--cells`: 6.5% and 1.9%.
227
+ - **Library use:** `ocrmypdf_hebrew.tessorder.reorder_tsv(text)` and `reorder_hocr(text)`.
228
+
229
+ ## Born-digital PDFs with broken Hebrew text
230
+
231
+ ```bash
232
+ ocrmypdf -l heb+eng --skip-text --hebrew-reocr-broken in.pdf out.pdf
233
+ ```
234
+
235
+ This checks the text layer of each page. It re-OCRs only the pages whose Hebrew text is broken,
236
+ for example stored in visual order or with a broken font encoding (`ocrmypdf_hebrew.textlayer`).
237
+
238
+ ## Tests
239
+
240
+ ```bash
241
+ python -I -m pytest tests # unit tests, no models needed
242
+ OCRMYPDF_HEBREW_E2E=1 python -I -m pytest tests/test_tessorder_e2e.py
243
+ ```
244
+
245
+ The second command is an end-to-end check, about 3 minutes, for maintainers. It runs the CLI
246
+ and the plugin on the 20 synthetic pages of our word-order study (`scripts/eval_tess_order.py`),
247
+ which are not part of this repository.
248
+
249
+ ## Licence
250
+
251
+ Apache-2.0.
@@ -0,0 +1,226 @@
1
+ # ocrmypdf-hebrew
2
+
3
+ An [OCRmyPDF](https://github.com/ocrmypdf/OCRmyPDF) plugin for Hebrew documents, including
4
+ Hebrew with English words and numbers.
5
+
6
+ It offers three things:
7
+
8
+ - **The paddleocr-hebrew engine.** With `-l heb` it replaces Tesseract with the
9
+ paddleocr-hebrew models.
10
+ - **Tesseract with the Hebrew word order fixed** (`--hebrew-tess-order`). OCRmyPDF's own
11
+ Tesseract runs, with any `-l`, and the word order of Hebrew lines that contain English or
12
+ numbers is corrected.
13
+ - **`hebrew-tess-order`**, the same word-order fix as a command-line filter for Tesseract's TSV
14
+ or hOCR output.
15
+
16
+ ## Install
17
+
18
+ ```bash
19
+ pip install ocrmypdf-hebrew # pulls OCRmyPDF 17.14 or later and paddleocr-hebrew
20
+ ```
21
+
22
+ OCRmyPDF loads the plugin automatically (entry point). The install also adds the
23
+ `hebrew-tess-order` command. OCRmyPDF 17.14.0 fixed three bugs in how earlier versions write
24
+ right-to-left text into the PDF (#1781, #1782, #1783), so the plugin needs it.
25
+
26
+ ## The paddleocr-hebrew engine
27
+
28
+ ```bash
29
+ ocrmypdf -l heb+eng scan.pdf out.pdf # this plugin's models
30
+ ocrmypdf -l eng scan.pdf out.pdf # Tesseract, untouched
31
+ ocrmypdf --ocr-engine tesseract -l heb ... # force Tesseract
32
+ ```
33
+
34
+ Options: `--hebrew-pipeline word|line`, `--hebrew-rec-mode cascade|nrtr|ctc`,
35
+ `--hebrew-models DIR` (an offline model folder) and `--hebrew-no-rejoin` (below).
36
+
37
+ **Long numbers cut in two.** The word detector sometimes covers one long number, amount or e-mail
38
+ address with two overlapping boxes, so `0585769628` would come out as `058 5769628` and
39
+ `12,203.39` as `12,203 3.39`. The plugin joins such neighbours (no Hebrew, at least one digit or
40
+ `@`, touching or overlapping) and reads them again as one word. If the new reading has a space,
41
+ they were two words after all and are kept as they were. This is on by default for the word
42
+ pipeline; `--hebrew-no-rejoin` turns it off. The same pass re-reads numbers whose last `0` the box
43
+ cut off (`21,000.0C` → `21,000.00`). On our 7 example documents, numbers found go from 83.3% to
44
+ 87.2%; on 20 synthetic pages and on old newspaper scans nothing gets worse.
45
+
46
+ ## Tesseract with the Hebrew word order fixed: `--hebrew-tess-order`
47
+
48
+ ### The problem
49
+
50
+ Tesseract chooses one direction for each paragraph. When a line has as many English words or
51
+ numbers as Hebrew words, a tie, Tesseract chooses left to right. Numbers count as left-to-right
52
+ words. Short Hebrew lines then come out with their words in the wrong order:
53
+
54
+ | on the page (read right to left) | Tesseract's text |
55
+ |---|---|
56
+ | `תאריך: 04.11.2026` | `04.11.2026 תאריך:` |
57
+ | `דוח שנתי Annual Report` | `Annual Report דוח שנתי` |
58
+ | `חיבור: Bluetooth 5.3 / USB-C` | `Bluetooth 5.3 / USB-C חיבור:` |
59
+
60
+ These are mostly headings, `key: value` fields, table cells and list items, so forms and
61
+ invoices are where it shows. Long paragraphs are rarely affected: on 34 pages of a 19th-century
62
+ Hebrew newspaper, under 3% of the Hebrew lines come out in this order with stock `heb+eng`.
63
+
64
+ ### Usage
65
+
66
+ The mode is **opt-in**: without `--hebrew-tess-order`, Tesseract's output is not touched.
67
+
68
+ ```bash
69
+ ocrmypdf --hebrew-tess-order -l heb+eng scan.pdf out.pdf --sidecar out.txt
70
+ ocrmypdf --hebrew-tess-order -l heb_rivok scan.pdf out.pdf # a custom model: see setup below
71
+ ```
72
+
73
+ - Tesseract runs exactly as OCRmyPDF runs it, with the same `--tesseract-*` options. After each
74
+ page, the plugin rewrites Tesseract's hOCR before OCRmyPDF builds the text layer.
75
+ - `--ocr-engine` can be `auto` (the default) or `tesseract`. With this flag, the paddleocr-hebrew
76
+ models are not used and not downloaded.
77
+ - `--hebrew-order-conservative`: in paragraphs of 2 or more lines, only lines with at least as
78
+ many Hebrew letters as Latin ones are reordered. Use it for documents with English passages.
79
+ It gives up most of the gain on forms.
80
+ - `--hebrew-order-cells` (**experimental, off by default**): lines are first cut at column gaps
81
+ (more than 3× the word height), and the cells are read right to left. Meant for RTL tables. It
82
+ also reverses the cell order of lines that have no Hebrew. **It has not been validated on real
83
+ documents**, so check its output before relying on it.
84
+ - **Needs the default renderer.** It does not work with `--pdf-renderer sandwich`, where
85
+ Tesseract writes the text layer and the sidecar itself. The plugin logs a warning.
86
+
87
+ ### The rule
88
+
89
+ - **Page gate.** Only pages whose letters are at least 25% Hebrew are touched. All other pages
90
+ keep Tesseract's output byte for byte. Without the gate, English pages that contain a Hebrew
91
+ word would be ruined.
92
+ - **Direction.** On a Hebrew page, every line with a Hebrew letter is read right to left. Lines
93
+ without Hebrew keep Tesseract's order.
94
+ - **English runs.** Words are taken right to left. A run of English words is kept left to right.
95
+ A run starts and ends on an English word, and numbers and punctuation between English words
96
+ belong to the run. A word containing Hebrew never joins a run. Punctuation or a number at the
97
+ edge of a run stays on the Hebrew side.
98
+
99
+ ### Which text gets the fix
100
+
101
+ | where the text is read | fixed? | why |
102
+ |---|---|---|
103
+ | the `--sidecar` text | **yes** | it is rebuilt from the rewritten hOCR |
104
+ | MuPDF (`mutool`, PyMuPDF), and other tools that follow the PDF's content order | **yes** (partly) | the content stream holds the corrected word order |
105
+ | pdftotext / poppler (Evince, Okular) | **no** | rebuilds each line from glyph positions, with one text direction for the whole page |
106
+ | pdfium (Chrome) | **no** | rebuilds each line from glyph positions, choosing each line's direction by letter majority |
107
+
108
+ Results on 20 synthetic pages with exact references (187 rows, character error rate), measured
109
+ with OCRmyPDF 17.14.0 and heb_rivok 1.0.1:
110
+
111
+ | | stock `heb+eng` | heb_rivok |
112
+ |---|---:|---:|
113
+ | sidecar, Tesseract → `--hebrew-tess-order` | 17.8% → **7.9%** | 10.8% → **2.7%** |
114
+ | MuPDF text | 15.9% → 9.7% | 10.6% → 5.1% |
115
+ | pdfium text | 23.2% → 22.4% | 18.2% → 17.5% |
116
+ | pdftotext text | 20.4% → 20.2% | 11.5% → 8.6% * |
117
+ | sidecar with `--hebrew-order-cells` | 7.1% | 2.2% |
118
+ | sidecar with `--hebrew-order-conservative` | 12.0% | 5.9% |
119
+
120
+ \* This gain is incidental. In Tesseract's left-to-right lines, Hebrew words carry stray
121
+ left-to-right marks (U+200E). Rewritten lines drop those marks, and on 1 of the 20 pages that
122
+ flips pdftotext's direction for the whole page to right to left. On the other pages pdftotext
123
+ barely changes.
124
+
125
+ - The 3 English pages and the 7 heb_rivok example documents come out unchanged.
126
+
127
+ ### Limits
128
+
129
+ - **What 17.14.0 leaves to the PDF readers.** They rebuild right-to-left lines from the glyph
130
+ positions, each in its own way:
131
+ - A few words still run together. pdftotext joins a number or an English word to the Hebrew
132
+ word after it (44 pairs on the 20 pages with stock `heb+eng`), and MuPDF a Hebrew word to
133
+ the English word after it (6 pairs). pdfium keeps them all apart.
134
+ - A word that mixes Hebrew with digits or Latin can come back with its parts in another
135
+ order. MuPDF gives `48.2-ל` for `ל-48.2`, pdftotext `בAndroid-` for `ב-Android`, and pdfium
136
+ `ל48.2-` (it keeps `ב-Android` as written). The digits and Latin letters themselves keep
137
+ their order.
138
+ - **English passages inside Hebrew pages are flipped.** For example, an English list within a
139
+ Hebrew report. Use `--hebrew-order-conservative`.
140
+ - **Things a word order cannot fix:**
141
+ - Version strings: `3.8.1 (build 512)` comes out as `3.8.1 512) (build`.
142
+ - A Hebrew prefix glued to English: `ב-Dark Mode`.
143
+ - Hebrew misread as Latin junk.
144
+ - The character order inside a token, as Tesseract read it: for example `:1` for `1:`.
145
+ - English-only cells of an RTL table. Only `--hebrew-order-cells` fixes these.
146
+ - **Bidi marks in the sidecar.** Pages with a rewritten line get their sidecar text rebuilt from
147
+ the hOCR, with no bidi marks. Other pages keep Tesseract's text, which includes them.
148
+
149
+ ### Custom models (heb_rivok) with OCRmyPDF
150
+
151
+ OCRmyPDF asks Tesseract for its `hocr` and `txt` configs. A `TESSDATA_PREFIX` that holds only
152
+ your model fails with `read_params_file: Can't open hocr`. Make a folder with links to the model
153
+ and to the system `configs/`:
154
+
155
+ ```bash
156
+ mkdir -p ~/tessdata-rivok
157
+ ln -s /path/to/heb_rivok.traineddata ~/tessdata-rivok/
158
+ ln -s /usr/share/tesseract-ocr/5/tessdata/configs ~/tessdata-rivok/configs
159
+ TESSDATA_PREFIX=~/tessdata-rivok ocrmypdf --hebrew-tess-order -l heb_rivok scan.pdf out.pdf
160
+ ```
161
+
162
+ **Pages with more than one column:** heb_rivok 1.0.1 or later reads them right column first
163
+ with `-l heb_rivok` alone; no workaround is needed. Only heb_rivok 1.0 laid its pages out left to
164
+ right (it knows more Latin letters than Hebrew ones) and read the left column first. With 1.0, use
165
+ `-l heb+heb_rivok` (also link `/usr/share/tesseract-ocr/5/tessdata/heb.traineddata` into the
166
+ folder) or `-l heb_rivok_lite+eng` (link `heb_rivok_lite.traineddata` and `eng.traineddata`), or
167
+ better, update to 1.0.1. This is a page-layout matter that `--hebrew-tess-order` does not change,
168
+ because it reorders words within lines, not columns. See the heb_rivok model card.
169
+
170
+ ## Command-line filter: `hebrew-tess-order`
171
+
172
+ It reads Tesseract's TSV or hOCR, which hold word boxes, from a file or from standard input. It
173
+ applies the same rule and page gate, and writes plain text in reading order.
174
+
175
+ ```bash
176
+ tesseract page.png - -l heb+eng tsv | hebrew-tess-order > page.txt
177
+ tesseract page.png page -l heb+eng hocr && hebrew-tess-order page.hocr --emit hocr -o page.fixed.hocr
178
+ tesseract page.png - -l heb+eng tsv | hebrew-tess-order --emit tsv > page.fixed.tsv
179
+ ```
180
+
181
+ - **Options:**
182
+ - `--conservative` and `--cells`, as above.
183
+ - `--emit text|tsv|hocr`: the rewritten input, in the same format as the input.
184
+ - `-o FILE`: write to a file instead of standard output.
185
+ - `-v`: a per-page summary on standard error.
186
+ - **Plain text cannot be fixed.** It has no boxes and no page context.
187
+ - **Text format:**
188
+ - The output is Tesseract's text layout: one line per line, with a blank line between
189
+ paragraphs and a form feed between pages.
190
+ - It has no bidi marks.
191
+ - In rewritten TSV/hOCR, the moved words lose their bidi marks, and hOCR lines get
192
+ `dir='rtl'`.
193
+ - **heb_rivok with `--tessdata-dir`.** If the folder holds only the model, Tesseract cannot find
194
+ its `tsv`/`hocr` configs and prints plain text instead. Pass the setting directly:
195
+
196
+ ```bash
197
+ tesseract page.png - --tessdata-dir /path/to/tessdata -l heb_rivok -c tessedit_create_tsv=1 | hebrew-tess-order
198
+ ```
199
+
200
+ - **Results.** On the 20 synthetic pages, the CLI text goes from 19.3% to **7.2%** CER (stock
201
+ `heb+eng`) and from 11.8% to **2.4%** (heb_rivok 1.0.1). With `--cells`: 6.5% and 1.9%.
202
+ - **Library use:** `ocrmypdf_hebrew.tessorder.reorder_tsv(text)` and `reorder_hocr(text)`.
203
+
204
+ ## Born-digital PDFs with broken Hebrew text
205
+
206
+ ```bash
207
+ ocrmypdf -l heb+eng --skip-text --hebrew-reocr-broken in.pdf out.pdf
208
+ ```
209
+
210
+ This checks the text layer of each page. It re-OCRs only the pages whose Hebrew text is broken,
211
+ for example stored in visual order or with a broken font encoding (`ocrmypdf_hebrew.textlayer`).
212
+
213
+ ## Tests
214
+
215
+ ```bash
216
+ python -I -m pytest tests # unit tests, no models needed
217
+ OCRMYPDF_HEBREW_E2E=1 python -I -m pytest tests/test_tessorder_e2e.py
218
+ ```
219
+
220
+ The second command is an end-to-end check, about 3 minutes, for maintainers. It runs the CLI
221
+ and the plugin on the 20 synthetic pages of our word-order study (`scripts/eval_tess_order.py`),
222
+ which are not part of this repository.
223
+
224
+ ## Licence
225
+
226
+ Apache-2.0.