lectris 0.3.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.
- lectris-0.3.0/PKG-INFO +243 -0
- lectris-0.3.0/README.md +218 -0
- lectris-0.3.0/pyproject.toml +132 -0
- lectris-0.3.0/pyproject.toml.orig +97 -0
- lectris-0.3.0/src/lectris/__init__.py +45 -0
- lectris-0.3.0/src/lectris/align.py +159 -0
- lectris-0.3.0/src/lectris/cli.py +290 -0
- lectris-0.3.0/src/lectris/confidence.py +154 -0
- lectris-0.3.0/src/lectris/decide.py +164 -0
- lectris-0.3.0/src/lectris/distance.py +231 -0
- lectris-0.3.0/src/lectris/edits.py +253 -0
- lectris-0.3.0/src/lectris/errors.py +9 -0
- lectris-0.3.0/src/lectris/eval/__init__.py +14 -0
- lectris-0.3.0/src/lectris/eval/calibrate.py +187 -0
- lectris-0.3.0/src/lectris/eval/experiment.py +448 -0
- lectris-0.3.0/src/lectris/eval/lines.py +47 -0
- lectris-0.3.0/src/lectris/eval/matching.py +71 -0
- lectris-0.3.0/src/lectris/eval/scoring.py +437 -0
- lectris-0.3.0/src/lectris/eval/stats.py +58 -0
- lectris-0.3.0/src/lectris/eval/truth.py +103 -0
- lectris-0.3.0/src/lectris/evidence.py +147 -0
- lectris-0.3.0/src/lectris/io/__init__.py +62 -0
- lectris-0.3.0/src/lectris/io/_common.py +153 -0
- lectris-0.3.0/src/lectris/io/_json.py +145 -0
- lectris-0.3.0/src/lectris/io/alto.py +274 -0
- lectris-0.3.0/src/lectris/io/detect.py +74 -0
- lectris-0.3.0/src/lectris/io/glypharis.py +165 -0
- lectris-0.3.0/src/lectris/io/hocr.py +332 -0
- lectris-0.3.0/src/lectris/io/jsonio.py +269 -0
- lectris-0.3.0/src/lectris/io/plain.py +49 -0
- lectris-0.3.0/src/lectris/io/tsv.py +177 -0
- lectris-0.3.0/src/lectris/layout/__init__.py +129 -0
- lectris-0.3.0/src/lectris/layout/columns.py +71 -0
- lectris-0.3.0/src/lectris/layout/geometry.py +224 -0
- lectris-0.3.0/src/lectris/layout/lines.py +133 -0
- lectris-0.3.0/src/lectris/layout/order.py +240 -0
- lectris-0.3.0/src/lectris/layout/paragraphs.py +194 -0
- lectris-0.3.0/src/lectris/lexicon.py +240 -0
- lectris-0.3.0/src/lectris/metrics.py +123 -0
- lectris-0.3.0/src/lectris/model.py +247 -0
- lectris-0.3.0/src/lectris/models/__init__.py +11 -0
- lectris-0.3.0/src/lectris/models/acceptance.py +174 -0
- lectris-0.3.0/src/lectris/models/channel.py +136 -0
- lectris-0.3.0/src/lectris/models/charlm.py +183 -0
- lectris-0.3.0/src/lectris/models/pack.py +244 -0
- lectris-0.3.0/src/lectris/models/risk.py +101 -0
- lectris-0.3.0/src/lectris/pipeline.py +423 -0
- lectris-0.3.0/src/lectris/profiles.py +117 -0
- lectris-0.3.0/src/lectris/propose.py +177 -0
- lectris-0.3.0/src/lectris/py.typed +0 -0
- lectris-0.3.0/src/lectris/quality.py +269 -0
- lectris-0.3.0/src/lectris/report.py +157 -0
- lectris-0.3.0/src/lectris/text/__init__.py +1 -0
- lectris-0.3.0/src/lectris/text/confusables.py +176 -0
- lectris-0.3.0/src/lectris/text/hyphenation.py +149 -0
- lectris-0.3.0/src/lectris/text/lexis.py +46 -0
- lectris-0.3.0/src/lectris/text/noise.py +62 -0
- lectris-0.3.0/src/lectris/text/numbers.py +77 -0
- lectris-0.3.0/src/lectris/text/punctuation.py +257 -0
- lectris-0.3.0/src/lectris/text/unicode.py +301 -0
- lectris-0.3.0/src/lectris/text/unwrap.py +108 -0
- lectris-0.3.0/src/lectris/text/whitespace.py +81 -0
lectris-0.3.0/PKG-INFO
ADDED
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: lectris
|
|
3
|
+
Version: 0.3.0
|
|
4
|
+
Summary: OCR output, read back into clean, ordered text: every change it makes is recorded and explained.
|
|
5
|
+
Keywords: ocr,post-processing,hocr,alto,text-normalisation,dehyphenation,reading-order,cer
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Classifier: Development Status :: 3 - Alpha
|
|
8
|
+
Classifier: Intended Audience :: Developers
|
|
9
|
+
Classifier: Intended Audience :: Science/Research
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
16
|
+
Classifier: Topic :: Scientific/Engineering :: Image Recognition
|
|
17
|
+
Classifier: Topic :: Text Processing
|
|
18
|
+
Classifier: Typing :: Typed
|
|
19
|
+
Requires-Python: >=3.12
|
|
20
|
+
Project-URL: Homepage, https://github.com/DuhItzAniket/Lectris
|
|
21
|
+
Project-URL: Repository, https://github.com/DuhItzAniket/Lectris
|
|
22
|
+
Project-URL: Issues, https://github.com/DuhItzAniket/Lectris/issues
|
|
23
|
+
Project-URL: Changelog, https://github.com/DuhItzAniket/Lectris/blob/main/CHANGELOG.md
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
|
|
26
|
+
# LECTRIS
|
|
27
|
+
|
|
28
|
+
**OCR output, read back into clean, ordered text: every change it makes is recorded and explained.**
|
|
29
|
+
|
|
30
|
+
[](https://github.com/DuhItzAniket/Lectris/actions/workflows/ci.yml)
|
|
31
|
+
[](pyproject.toml)
|
|
32
|
+
[](LICENSE)
|
|
33
|
+
[](https://github.com/DuhItzAniket/Repro)
|
|
34
|
+
|
|
35
|
+
OCR engines return words, lines and boxes, and the text they recover still needs work: columns
|
|
36
|
+
read in the wrong order, words split across lines, `fi` ligatures, `O` read for `0`, spaces in the
|
|
37
|
+
middle of numbers, specks read as lines of punctuation. LECTRIS reads the engine's output
|
|
38
|
+
(hOCR, ALTO, Tesseract TSV, JSON or plain text), puts the lines in reading order, groups them into
|
|
39
|
+
paragraphs, and repairs the text with small, conservative rules. Each rule records each edit it
|
|
40
|
+
makes, so you can audit a result, count what changed, or switch a rule off.
|
|
41
|
+
|
|
42
|
+
It does not run OCR, process images or use language models, and it has no dependencies outside
|
|
43
|
+
the standard library.
|
|
44
|
+
|
|
45
|
+
> **Status: v0.1 released; v2 in planning.** v2 turns the rules into proposers whose candidates are
|
|
46
|
+
> scored from evidence (including the OCR engine's own uncertainty) and accepted under a threshold
|
|
47
|
+
> that bounds the harm done to correct text. It is designed alongside the
|
|
48
|
+
> [GLYPHARIS](https://github.com/DuhItzAniket/Glypharis) OCR engine. See
|
|
49
|
+
> [Research](#research).
|
|
50
|
+
|
|
51
|
+
## Example
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
from lectris import clean
|
|
55
|
+
|
|
56
|
+
ocr = (
|
|
57
|
+
"The first survey of the river recorded 1O5 species , most of them\n"
|
|
58
|
+
"in the upper reaches. Later surveys con-\n"
|
|
59
|
+
"firmed the count.\n"
|
|
60
|
+
"~~~ ,,, ' ' |||\n"
|
|
61
|
+
"Water quality fell by 3 .5 percent in ten years."
|
|
62
|
+
)
|
|
63
|
+
result = clean(ocr)
|
|
64
|
+
print(result.text)
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
```text
|
|
68
|
+
The first survey of the river recorded 105 species, most of them in the upper reaches. Later surveys confirmed the count. Water quality fell by 3.5 percent in ten years.
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Every change is in the result: ten edits here, each with its rule, its place and what it replaced.
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
for change in result.changes():
|
|
75
|
+
print(change.edit.rule, repr(change.edit.before), "->", repr(change.edit.after))
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
```text
|
|
79
|
+
unicode.ligatures 'fi' -> 'fi'
|
|
80
|
+
whitespace.collapse ' ' -> ' '
|
|
81
|
+
noise.drop_lines "~~~ ,,, ' ' |||\n" -> ''
|
|
82
|
+
hyphenation.join '-\nfirmed ' -> 'firmed\n'
|
|
83
|
+
unwrap.join_lines '\n' -> ' '
|
|
84
|
+
unwrap.join_lines '\n' -> ' '
|
|
85
|
+
unwrap.join_lines '\n' -> ' '
|
|
86
|
+
confusables.in_numbers '1O5' -> '105'
|
|
87
|
+
numbers.separators ' .' -> '.'
|
|
88
|
+
punctuation.spacing ' ' -> ''
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Install
|
|
92
|
+
|
|
93
|
+
```bash
|
|
94
|
+
pip install git+https://github.com/DuhItzAniket/Lectris
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
LECTRIS needs Python 3.12 or newer.
|
|
98
|
+
|
|
99
|
+
## The command line
|
|
100
|
+
|
|
101
|
+
```bash
|
|
102
|
+
lectris clean page.hocr # the cleaned text, on standard output
|
|
103
|
+
lectris clean page.xml -o page.txt -r report.json --summary
|
|
104
|
+
lectris clean scan.tsv --profile conservative
|
|
105
|
+
lectris clean results/page.json # what glypharis-ocr wrote
|
|
106
|
+
lectris eval reference.txt page.txt # character and word error rates
|
|
107
|
+
lectris rules # every profile and its rules
|
|
108
|
+
lectris experiment run configs/LX-MAIN-001.json # a registered experiment's report
|
|
109
|
+
lectris fit --pairs fit.json --engine glypharis --model glypharis_v1 --text books/ -o pack.lectris.json.gz
|
|
110
|
+
lectris calibrate --pack pack.lectris.json.gz --fit fit.json --cal cal.json -o calibrated.lectris.json.gz
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
The input's format is detected from its content; `--format` names it. `--lexicon words.txt` (one
|
|
114
|
+
word per line, an optional count after it) makes de-hyphenation and the confusion rules more
|
|
115
|
+
precise. No dictionary is bundled.
|
|
116
|
+
|
|
117
|
+
## With a model pack: risk-controlled corrections
|
|
118
|
+
|
|
119
|
+
A model pack holds what LECTRIS learns about one OCR engine: what it confuses, a character
|
|
120
|
+
language model, word counts, and an acceptance model calibrated so that each profile keeps the
|
|
121
|
+
harm it does to correct text within a budget, with a stated confidence.
|
|
122
|
+
|
|
123
|
+
```bash
|
|
124
|
+
lectris fit --pairs fit.json --engine glypharis --model glypharis_v1 --text books/ -o pack.lectris.json.gz
|
|
125
|
+
lectris calibrate --pack pack.lectris.json.gz --fit fit.json --cal cal.json -o calibrated.lectris.json.gz
|
|
126
|
+
lectris clean page.json --pack calibrated.lectris.json.gz -r report.md --report-format markdown
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
With a calibrated pack, every guess (the v0.1 rules' and those of two more proposers: words one
|
|
130
|
+
edit from a lexicon word, missing and spurious spaces) is accepted only if its probability of
|
|
131
|
+
being right reaches the profile's threshold. The report states the guarantee, for instance
|
|
132
|
+
*harm rate at most 0.02 with confidence 0.9*, and whether it applies: it holds only for input
|
|
133
|
+
from the engine and model the pack was calibrated for, and the command warns when it does not.
|
|
134
|
+
It also lists, *for review*, the corrections it declined that were still likely.
|
|
135
|
+
|
|
136
|
+
```python
|
|
137
|
+
from lectris import clean, io
|
|
138
|
+
from lectris.models import pack
|
|
139
|
+
|
|
140
|
+
model = pack.load("calibrated.lectris.json.gz")
|
|
141
|
+
result = clean(io.read("page.json"), pack=model, policy=model.policy("standard"))
|
|
142
|
+
result.guarantee, result.guarantee_applies # the promise, and whether it holds here
|
|
143
|
+
result.flags() # declined but likely corrections
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
Without a pack, LECTRIS behaves exactly as v0.1. The pairs come from running the engine on pages
|
|
147
|
+
with ground truth; `scripts/build_hiertext_pairs.py` builds them for HierText.
|
|
148
|
+
|
|
149
|
+
## Profiles
|
|
150
|
+
|
|
151
|
+
| Profile | What it changes |
|
|
152
|
+
|---|---|
|
|
153
|
+
| `conservative` | The safest repairs: line breaks, invisible characters, compatibility forms, ligatures, Unicode composition, extra spaces, and words hyphenated across lines. Line breaks are kept. |
|
|
154
|
+
| `standard` (default) | The conservative rules, plus noise lines dropped, paragraphs unwrapped onto one line, letters read inside numbers, spaces inside numbers and before punctuation, and dash variants. |
|
|
155
|
+
| `aggressive` | The standard rules, plus digits read inside words, straight quotes and three-dot ellipses. Recall over precision: read its report before trusting it. |
|
|
156
|
+
|
|
157
|
+
Every profile is idempotent: cleaning its own output changes nothing. The rules, their evidence
|
|
158
|
+
and what each one deliberately leaves alone are listed in [docs/rules.md](docs/rules.md).
|
|
159
|
+
|
|
160
|
+
## Reading OCR output
|
|
161
|
+
|
|
162
|
+
```python
|
|
163
|
+
from pathlib import Path
|
|
164
|
+
|
|
165
|
+
from lectris import clean, io, report
|
|
166
|
+
|
|
167
|
+
document = io.read("page.hocr") # or .xml (ALTO), .tsv, .json (incl. GLYPHARIS), .txt
|
|
168
|
+
result = clean(document, profile="standard")
|
|
169
|
+
print(report.summary(result)) # edits per rule
|
|
170
|
+
Path("report.json").write_text(report.to_json(result), encoding="utf-8")
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
| Format | What is read |
|
|
174
|
+
|---|---|
|
|
175
|
+
| hOCR | pages, areas and paragraphs, lines, words, with `bbox` and `x_wconf` |
|
|
176
|
+
| ALTO XML 2–4 | text blocks, lines and strings with their positions, `WC` confidences and `HYP` hyphens |
|
|
177
|
+
| Tesseract TSV | pages, paragraphs, lines and words with boxes and confidences |
|
|
178
|
+
| JSON | LECTRIS's own form, or a flat list of lines with `text`, `bbox` and `confidence` |
|
|
179
|
+
| [GLYPHARIS](https://github.com/DuhItzAniket/Glypharis) JSON | the engine and model versions, the page size, and each line's text, box, polygon, calibrated confidence and detection score |
|
|
180
|
+
| Plain text | pages separated by form feeds, paragraphs by blank lines |
|
|
181
|
+
|
|
182
|
+
When the lines have boxes, the layout steps put them in reading order (columns read whole, a
|
|
183
|
+
title before the columns under it) and group them into paragraphs from their spacing, indents
|
|
184
|
+
and sentence ends. Every threshold is relative to the size of the text, so a scan at any
|
|
185
|
+
resolution gives the same result.
|
|
186
|
+
|
|
187
|
+
## Measuring OCR
|
|
188
|
+
|
|
189
|
+
```python
|
|
190
|
+
from lectris import metrics
|
|
191
|
+
|
|
192
|
+
metrics.cer("kitten sat", "sitting sat") # 0.3: 3 errors in 10 characters
|
|
193
|
+
metrics.word_errors("kitten sat", "sitting sat") # substitutions, deletions, insertions
|
|
194
|
+
metrics.confusions(reference, ocr_text) # which characters the engine mixes up
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
## Design
|
|
198
|
+
|
|
199
|
+
- **No silent edits.** A rule returns its new text and the edits that produced it; the pipeline
|
|
200
|
+
keeps them all.
|
|
201
|
+
- **Precision before recall.** A rule changes text only where local evidence is strong, and each
|
|
202
|
+
rule's tests list the correct text it must leave alone.
|
|
203
|
+
- **Pure and deterministic.** The same input always gives the same output.
|
|
204
|
+
|
|
205
|
+
The architecture is in [docs/ARCHITECTURE.md](docs/ARCHITECTURE.md).
|
|
206
|
+
|
|
207
|
+
**Scope**: Latin-script text, horizontal lines (scans skewed by a few degrees are corrected),
|
|
208
|
+
left-to-right reading order. Tables, and tables of contents whose page numbers stand far to the
|
|
209
|
+
right, are read column by column.
|
|
210
|
+
|
|
211
|
+
## Research
|
|
212
|
+
|
|
213
|
+
LECTRIS v2 is also a research project: *auditable, risk-controlled post-OCR correction with engine
|
|
214
|
+
uncertainty*. Every experiment is registered before it runs, and every reported number traces back
|
|
215
|
+
to a generated report.
|
|
216
|
+
|
|
217
|
+
| Document | What's in it |
|
|
218
|
+
|---|---|
|
|
219
|
+
| [Architecture](docs/ARCHITECTURE.md) | v0.1 as built and the v2 target: propose, evidence, decide |
|
|
220
|
+
| [Implementation plan](docs/IMPLEMENTATION_PLAN.md) | research questions, phases, tasks and exit criteria |
|
|
221
|
+
| [Roadmap](docs/ROADMAP.md) | milestones, dependencies, where LECTRIS meets GLYPHARIS |
|
|
222
|
+
| [Phase status](docs/PHASE_STATUS.md) | what is done, with the numbers behind it |
|
|
223
|
+
| [Experiment protocol](docs/EXPERIMENT_PROTOCOL.md) | splits, metrics (harm included), statistics, risk-control validity |
|
|
224
|
+
| [Decisions](docs/decisions/) | architecture decision records |
|
|
225
|
+
| [Risk register](docs/RISK_REGISTER.md) | known risks and mitigations |
|
|
226
|
+
| [Datasets](DATASETS.md) | provenance, licences and roles |
|
|
227
|
+
| [Research log](research/) | hypotheses, experiment registry, related work, paper outline |
|
|
228
|
+
|
|
229
|
+
## Development
|
|
230
|
+
|
|
231
|
+
```bash
|
|
232
|
+
uv sync
|
|
233
|
+
uv run pytest
|
|
234
|
+
uv run ruff check && uv run mypy
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
Every pull request is checked by [Repro](https://github.com/DuhItzAniket/Repro), which runs the
|
|
238
|
+
tests on both sides of the change, compares what the changed functions actually return, and
|
|
239
|
+
posts a Reproduction Card on the pull request. See [CONTRIBUTING.md](CONTRIBUTING.md).
|
|
240
|
+
|
|
241
|
+
## License
|
|
242
|
+
|
|
243
|
+
[Apache License 2.0](LICENSE).
|
lectris-0.3.0/README.md
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
# LECTRIS
|
|
2
|
+
|
|
3
|
+
**OCR output, read back into clean, ordered text: every change it makes is recorded and explained.**
|
|
4
|
+
|
|
5
|
+
[](https://github.com/DuhItzAniket/Lectris/actions/workflows/ci.yml)
|
|
6
|
+
[](pyproject.toml)
|
|
7
|
+
[](LICENSE)
|
|
8
|
+
[](https://github.com/DuhItzAniket/Repro)
|
|
9
|
+
|
|
10
|
+
OCR engines return words, lines and boxes, and the text they recover still needs work: columns
|
|
11
|
+
read in the wrong order, words split across lines, `fi` ligatures, `O` read for `0`, spaces in the
|
|
12
|
+
middle of numbers, specks read as lines of punctuation. LECTRIS reads the engine's output
|
|
13
|
+
(hOCR, ALTO, Tesseract TSV, JSON or plain text), puts the lines in reading order, groups them into
|
|
14
|
+
paragraphs, and repairs the text with small, conservative rules. Each rule records each edit it
|
|
15
|
+
makes, so you can audit a result, count what changed, or switch a rule off.
|
|
16
|
+
|
|
17
|
+
It does not run OCR, process images or use language models, and it has no dependencies outside
|
|
18
|
+
the standard library.
|
|
19
|
+
|
|
20
|
+
> **Status: v0.1 released; v2 in planning.** v2 turns the rules into proposers whose candidates are
|
|
21
|
+
> scored from evidence (including the OCR engine's own uncertainty) and accepted under a threshold
|
|
22
|
+
> that bounds the harm done to correct text. It is designed alongside the
|
|
23
|
+
> [GLYPHARIS](https://github.com/DuhItzAniket/Glypharis) OCR engine. See
|
|
24
|
+
> [Research](#research).
|
|
25
|
+
|
|
26
|
+
## Example
|
|
27
|
+
|
|
28
|
+
```python
|
|
29
|
+
from lectris import clean
|
|
30
|
+
|
|
31
|
+
ocr = (
|
|
32
|
+
"The first survey of the river recorded 1O5 species , most of them\n"
|
|
33
|
+
"in the upper reaches. Later surveys con-\n"
|
|
34
|
+
"firmed the count.\n"
|
|
35
|
+
"~~~ ,,, ' ' |||\n"
|
|
36
|
+
"Water quality fell by 3 .5 percent in ten years."
|
|
37
|
+
)
|
|
38
|
+
result = clean(ocr)
|
|
39
|
+
print(result.text)
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
```text
|
|
43
|
+
The first survey of the river recorded 105 species, most of them in the upper reaches. Later surveys confirmed the count. Water quality fell by 3.5 percent in ten years.
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Every change is in the result: ten edits here, each with its rule, its place and what it replaced.
|
|
47
|
+
|
|
48
|
+
```python
|
|
49
|
+
for change in result.changes():
|
|
50
|
+
print(change.edit.rule, repr(change.edit.before), "->", repr(change.edit.after))
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
```text
|
|
54
|
+
unicode.ligatures 'fi' -> 'fi'
|
|
55
|
+
whitespace.collapse ' ' -> ' '
|
|
56
|
+
noise.drop_lines "~~~ ,,, ' ' |||\n" -> ''
|
|
57
|
+
hyphenation.join '-\nfirmed ' -> 'firmed\n'
|
|
58
|
+
unwrap.join_lines '\n' -> ' '
|
|
59
|
+
unwrap.join_lines '\n' -> ' '
|
|
60
|
+
unwrap.join_lines '\n' -> ' '
|
|
61
|
+
confusables.in_numbers '1O5' -> '105'
|
|
62
|
+
numbers.separators ' .' -> '.'
|
|
63
|
+
punctuation.spacing ' ' -> ''
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Install
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
pip install git+https://github.com/DuhItzAniket/Lectris
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
LECTRIS needs Python 3.12 or newer.
|
|
73
|
+
|
|
74
|
+
## The command line
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
lectris clean page.hocr # the cleaned text, on standard output
|
|
78
|
+
lectris clean page.xml -o page.txt -r report.json --summary
|
|
79
|
+
lectris clean scan.tsv --profile conservative
|
|
80
|
+
lectris clean results/page.json # what glypharis-ocr wrote
|
|
81
|
+
lectris eval reference.txt page.txt # character and word error rates
|
|
82
|
+
lectris rules # every profile and its rules
|
|
83
|
+
lectris experiment run configs/LX-MAIN-001.json # a registered experiment's report
|
|
84
|
+
lectris fit --pairs fit.json --engine glypharis --model glypharis_v1 --text books/ -o pack.lectris.json.gz
|
|
85
|
+
lectris calibrate --pack pack.lectris.json.gz --fit fit.json --cal cal.json -o calibrated.lectris.json.gz
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
The input's format is detected from its content; `--format` names it. `--lexicon words.txt` (one
|
|
89
|
+
word per line, an optional count after it) makes de-hyphenation and the confusion rules more
|
|
90
|
+
precise. No dictionary is bundled.
|
|
91
|
+
|
|
92
|
+
## With a model pack: risk-controlled corrections
|
|
93
|
+
|
|
94
|
+
A model pack holds what LECTRIS learns about one OCR engine: what it confuses, a character
|
|
95
|
+
language model, word counts, and an acceptance model calibrated so that each profile keeps the
|
|
96
|
+
harm it does to correct text within a budget, with a stated confidence.
|
|
97
|
+
|
|
98
|
+
```bash
|
|
99
|
+
lectris fit --pairs fit.json --engine glypharis --model glypharis_v1 --text books/ -o pack.lectris.json.gz
|
|
100
|
+
lectris calibrate --pack pack.lectris.json.gz --fit fit.json --cal cal.json -o calibrated.lectris.json.gz
|
|
101
|
+
lectris clean page.json --pack calibrated.lectris.json.gz -r report.md --report-format markdown
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
With a calibrated pack, every guess (the v0.1 rules' and those of two more proposers: words one
|
|
105
|
+
edit from a lexicon word, missing and spurious spaces) is accepted only if its probability of
|
|
106
|
+
being right reaches the profile's threshold. The report states the guarantee, for instance
|
|
107
|
+
*harm rate at most 0.02 with confidence 0.9*, and whether it applies: it holds only for input
|
|
108
|
+
from the engine and model the pack was calibrated for, and the command warns when it does not.
|
|
109
|
+
It also lists, *for review*, the corrections it declined that were still likely.
|
|
110
|
+
|
|
111
|
+
```python
|
|
112
|
+
from lectris import clean, io
|
|
113
|
+
from lectris.models import pack
|
|
114
|
+
|
|
115
|
+
model = pack.load("calibrated.lectris.json.gz")
|
|
116
|
+
result = clean(io.read("page.json"), pack=model, policy=model.policy("standard"))
|
|
117
|
+
result.guarantee, result.guarantee_applies # the promise, and whether it holds here
|
|
118
|
+
result.flags() # declined but likely corrections
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
Without a pack, LECTRIS behaves exactly as v0.1. The pairs come from running the engine on pages
|
|
122
|
+
with ground truth; `scripts/build_hiertext_pairs.py` builds them for HierText.
|
|
123
|
+
|
|
124
|
+
## Profiles
|
|
125
|
+
|
|
126
|
+
| Profile | What it changes |
|
|
127
|
+
|---|---|
|
|
128
|
+
| `conservative` | The safest repairs: line breaks, invisible characters, compatibility forms, ligatures, Unicode composition, extra spaces, and words hyphenated across lines. Line breaks are kept. |
|
|
129
|
+
| `standard` (default) | The conservative rules, plus noise lines dropped, paragraphs unwrapped onto one line, letters read inside numbers, spaces inside numbers and before punctuation, and dash variants. |
|
|
130
|
+
| `aggressive` | The standard rules, plus digits read inside words, straight quotes and three-dot ellipses. Recall over precision: read its report before trusting it. |
|
|
131
|
+
|
|
132
|
+
Every profile is idempotent: cleaning its own output changes nothing. The rules, their evidence
|
|
133
|
+
and what each one deliberately leaves alone are listed in [docs/rules.md](docs/rules.md).
|
|
134
|
+
|
|
135
|
+
## Reading OCR output
|
|
136
|
+
|
|
137
|
+
```python
|
|
138
|
+
from pathlib import Path
|
|
139
|
+
|
|
140
|
+
from lectris import clean, io, report
|
|
141
|
+
|
|
142
|
+
document = io.read("page.hocr") # or .xml (ALTO), .tsv, .json (incl. GLYPHARIS), .txt
|
|
143
|
+
result = clean(document, profile="standard")
|
|
144
|
+
print(report.summary(result)) # edits per rule
|
|
145
|
+
Path("report.json").write_text(report.to_json(result), encoding="utf-8")
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
| Format | What is read |
|
|
149
|
+
|---|---|
|
|
150
|
+
| hOCR | pages, areas and paragraphs, lines, words, with `bbox` and `x_wconf` |
|
|
151
|
+
| ALTO XML 2–4 | text blocks, lines and strings with their positions, `WC` confidences and `HYP` hyphens |
|
|
152
|
+
| Tesseract TSV | pages, paragraphs, lines and words with boxes and confidences |
|
|
153
|
+
| JSON | LECTRIS's own form, or a flat list of lines with `text`, `bbox` and `confidence` |
|
|
154
|
+
| [GLYPHARIS](https://github.com/DuhItzAniket/Glypharis) JSON | the engine and model versions, the page size, and each line's text, box, polygon, calibrated confidence and detection score |
|
|
155
|
+
| Plain text | pages separated by form feeds, paragraphs by blank lines |
|
|
156
|
+
|
|
157
|
+
When the lines have boxes, the layout steps put them in reading order (columns read whole, a
|
|
158
|
+
title before the columns under it) and group them into paragraphs from their spacing, indents
|
|
159
|
+
and sentence ends. Every threshold is relative to the size of the text, so a scan at any
|
|
160
|
+
resolution gives the same result.
|
|
161
|
+
|
|
162
|
+
## Measuring OCR
|
|
163
|
+
|
|
164
|
+
```python
|
|
165
|
+
from lectris import metrics
|
|
166
|
+
|
|
167
|
+
metrics.cer("kitten sat", "sitting sat") # 0.3: 3 errors in 10 characters
|
|
168
|
+
metrics.word_errors("kitten sat", "sitting sat") # substitutions, deletions, insertions
|
|
169
|
+
metrics.confusions(reference, ocr_text) # which characters the engine mixes up
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
## Design
|
|
173
|
+
|
|
174
|
+
- **No silent edits.** A rule returns its new text and the edits that produced it; the pipeline
|
|
175
|
+
keeps them all.
|
|
176
|
+
- **Precision before recall.** A rule changes text only where local evidence is strong, and each
|
|
177
|
+
rule's tests list the correct text it must leave alone.
|
|
178
|
+
- **Pure and deterministic.** The same input always gives the same output.
|
|
179
|
+
|
|
180
|
+
The architecture is in [docs/ARCHITECTURE.md](docs/ARCHITECTURE.md).
|
|
181
|
+
|
|
182
|
+
**Scope**: Latin-script text, horizontal lines (scans skewed by a few degrees are corrected),
|
|
183
|
+
left-to-right reading order. Tables, and tables of contents whose page numbers stand far to the
|
|
184
|
+
right, are read column by column.
|
|
185
|
+
|
|
186
|
+
## Research
|
|
187
|
+
|
|
188
|
+
LECTRIS v2 is also a research project: *auditable, risk-controlled post-OCR correction with engine
|
|
189
|
+
uncertainty*. Every experiment is registered before it runs, and every reported number traces back
|
|
190
|
+
to a generated report.
|
|
191
|
+
|
|
192
|
+
| Document | What's in it |
|
|
193
|
+
|---|---|
|
|
194
|
+
| [Architecture](docs/ARCHITECTURE.md) | v0.1 as built and the v2 target: propose, evidence, decide |
|
|
195
|
+
| [Implementation plan](docs/IMPLEMENTATION_PLAN.md) | research questions, phases, tasks and exit criteria |
|
|
196
|
+
| [Roadmap](docs/ROADMAP.md) | milestones, dependencies, where LECTRIS meets GLYPHARIS |
|
|
197
|
+
| [Phase status](docs/PHASE_STATUS.md) | what is done, with the numbers behind it |
|
|
198
|
+
| [Experiment protocol](docs/EXPERIMENT_PROTOCOL.md) | splits, metrics (harm included), statistics, risk-control validity |
|
|
199
|
+
| [Decisions](docs/decisions/) | architecture decision records |
|
|
200
|
+
| [Risk register](docs/RISK_REGISTER.md) | known risks and mitigations |
|
|
201
|
+
| [Datasets](DATASETS.md) | provenance, licences and roles |
|
|
202
|
+
| [Research log](research/) | hypotheses, experiment registry, related work, paper outline |
|
|
203
|
+
|
|
204
|
+
## Development
|
|
205
|
+
|
|
206
|
+
```bash
|
|
207
|
+
uv sync
|
|
208
|
+
uv run pytest
|
|
209
|
+
uv run ruff check && uv run mypy
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
Every pull request is checked by [Repro](https://github.com/DuhItzAniket/Repro), which runs the
|
|
213
|
+
tests on both sides of the change, compares what the changed functions actually return, and
|
|
214
|
+
posts a Reproduction Card on the pull request. See [CONTRIBUTING.md](CONTRIBUTING.md).
|
|
215
|
+
|
|
216
|
+
## License
|
|
217
|
+
|
|
218
|
+
[Apache License 2.0](LICENSE).
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "lectris"
|
|
3
|
+
version = "0.3.0"
|
|
4
|
+
description = "OCR output, read back into clean, ordered text: every change it makes is recorded and explained."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "Apache-2.0"
|
|
7
|
+
requires-python = ">=3.12"
|
|
8
|
+
keywords = [
|
|
9
|
+
"ocr",
|
|
10
|
+
"post-processing",
|
|
11
|
+
"hocr",
|
|
12
|
+
"alto",
|
|
13
|
+
"text-normalisation",
|
|
14
|
+
"dehyphenation",
|
|
15
|
+
"reading-order",
|
|
16
|
+
"cer",
|
|
17
|
+
]
|
|
18
|
+
classifiers = [
|
|
19
|
+
"Development Status :: 3 - Alpha",
|
|
20
|
+
"Intended Audience :: Developers",
|
|
21
|
+
"Intended Audience :: Science/Research",
|
|
22
|
+
"Operating System :: OS Independent",
|
|
23
|
+
"Programming Language :: Python :: 3",
|
|
24
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
25
|
+
"Programming Language :: Python :: 3.12",
|
|
26
|
+
"Programming Language :: Python :: 3.13",
|
|
27
|
+
"Programming Language :: Python :: 3.14",
|
|
28
|
+
"Topic :: Scientific/Engineering :: Image Recognition",
|
|
29
|
+
"Topic :: Text Processing",
|
|
30
|
+
"Typing :: Typed",
|
|
31
|
+
]
|
|
32
|
+
dependencies = []
|
|
33
|
+
|
|
34
|
+
[project.urls]
|
|
35
|
+
Homepage = "https://github.com/DuhItzAniket/Lectris"
|
|
36
|
+
Repository = "https://github.com/DuhItzAniket/Lectris"
|
|
37
|
+
Issues = "https://github.com/DuhItzAniket/Lectris/issues"
|
|
38
|
+
Changelog = "https://github.com/DuhItzAniket/Lectris/blob/main/CHANGELOG.md"
|
|
39
|
+
|
|
40
|
+
[project.scripts]
|
|
41
|
+
lectris = "lectris.cli:main"
|
|
42
|
+
|
|
43
|
+
[dependency-groups]
|
|
44
|
+
dev = [
|
|
45
|
+
"pytest>=9.1.1",
|
|
46
|
+
"hypothesis>=6.170.1",
|
|
47
|
+
"ruff>=0.17.1",
|
|
48
|
+
"mypy>=2.4.0",
|
|
49
|
+
]
|
|
50
|
+
|
|
51
|
+
[build-system]
|
|
52
|
+
requires = ["uv_build>=0.13.0,<0.14"]
|
|
53
|
+
build-backend = "uv_build"
|
|
54
|
+
|
|
55
|
+
[tool.pytest.ini_options]
|
|
56
|
+
testpaths = [
|
|
57
|
+
"tests",
|
|
58
|
+
"src",
|
|
59
|
+
]
|
|
60
|
+
addopts = [
|
|
61
|
+
"--strict-markers",
|
|
62
|
+
"--strict-config",
|
|
63
|
+
"-ra",
|
|
64
|
+
"--doctest-modules",
|
|
65
|
+
]
|
|
66
|
+
xfail_strict = true
|
|
67
|
+
|
|
68
|
+
[tool.ruff]
|
|
69
|
+
target-version = "py312"
|
|
70
|
+
line-length = 100
|
|
71
|
+
src = [
|
|
72
|
+
"src",
|
|
73
|
+
"tests",
|
|
74
|
+
]
|
|
75
|
+
|
|
76
|
+
[tool.ruff.format]
|
|
77
|
+
docstring-code-format = true
|
|
78
|
+
|
|
79
|
+
[tool.ruff.lint]
|
|
80
|
+
select = [
|
|
81
|
+
"E",
|
|
82
|
+
"W",
|
|
83
|
+
"F",
|
|
84
|
+
"I",
|
|
85
|
+
"B",
|
|
86
|
+
"UP",
|
|
87
|
+
"SIM",
|
|
88
|
+
"RUF",
|
|
89
|
+
"PT",
|
|
90
|
+
"C4",
|
|
91
|
+
"N",
|
|
92
|
+
"S",
|
|
93
|
+
"PERF",
|
|
94
|
+
"TID",
|
|
95
|
+
"PTH",
|
|
96
|
+
"T20",
|
|
97
|
+
"ARG",
|
|
98
|
+
"RET",
|
|
99
|
+
"PLC",
|
|
100
|
+
"PLE",
|
|
101
|
+
"PLR",
|
|
102
|
+
"PLW",
|
|
103
|
+
"ERA",
|
|
104
|
+
"BLE",
|
|
105
|
+
]
|
|
106
|
+
ignore = ["PLR0913"]
|
|
107
|
+
|
|
108
|
+
[tool.ruff.lint.per-file-ignores]
|
|
109
|
+
"tests/**" = [
|
|
110
|
+
"S101",
|
|
111
|
+
"PLR2004",
|
|
112
|
+
"ARG",
|
|
113
|
+
]
|
|
114
|
+
|
|
115
|
+
[tool.ruff.lint.flake8-tidy-imports]
|
|
116
|
+
ban-relative-imports = "all"
|
|
117
|
+
|
|
118
|
+
[tool.mypy]
|
|
119
|
+
python_version = "3.12"
|
|
120
|
+
strict = true
|
|
121
|
+
warn_unreachable = true
|
|
122
|
+
enable_error_code = [
|
|
123
|
+
"ignore-without-code",
|
|
124
|
+
"redundant-expr",
|
|
125
|
+
"truthy-bool",
|
|
126
|
+
"possibly-undefined",
|
|
127
|
+
]
|
|
128
|
+
files = [
|
|
129
|
+
"src",
|
|
130
|
+
"tests",
|
|
131
|
+
"scripts",
|
|
132
|
+
]
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "lectris"
|
|
3
|
+
version = "0.3.0"
|
|
4
|
+
description = "OCR output, read back into clean, ordered text: every change it makes is recorded and explained."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "Apache-2.0"
|
|
7
|
+
requires-python = ">=3.12"
|
|
8
|
+
keywords = ["ocr", "post-processing", "hocr", "alto", "text-normalisation", "dehyphenation", "reading-order", "cer"]
|
|
9
|
+
classifiers = [
|
|
10
|
+
"Development Status :: 3 - Alpha",
|
|
11
|
+
"Intended Audience :: Developers",
|
|
12
|
+
"Intended Audience :: Science/Research",
|
|
13
|
+
"Operating System :: OS Independent",
|
|
14
|
+
"Programming Language :: Python :: 3",
|
|
15
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
16
|
+
"Programming Language :: Python :: 3.12",
|
|
17
|
+
"Programming Language :: Python :: 3.13",
|
|
18
|
+
"Programming Language :: Python :: 3.14",
|
|
19
|
+
"Topic :: Scientific/Engineering :: Image Recognition",
|
|
20
|
+
"Topic :: Text Processing",
|
|
21
|
+
"Typing :: Typed",
|
|
22
|
+
]
|
|
23
|
+
dependencies = []
|
|
24
|
+
|
|
25
|
+
[project.urls]
|
|
26
|
+
Homepage = "https://github.com/DuhItzAniket/Lectris"
|
|
27
|
+
Repository = "https://github.com/DuhItzAniket/Lectris"
|
|
28
|
+
Issues = "https://github.com/DuhItzAniket/Lectris/issues"
|
|
29
|
+
Changelog = "https://github.com/DuhItzAniket/Lectris/blob/main/CHANGELOG.md"
|
|
30
|
+
|
|
31
|
+
[project.scripts]
|
|
32
|
+
lectris = "lectris.cli:main"
|
|
33
|
+
|
|
34
|
+
[dependency-groups]
|
|
35
|
+
dev = [
|
|
36
|
+
"pytest>=9.1.1",
|
|
37
|
+
"hypothesis>=6.170.1",
|
|
38
|
+
"ruff>=0.17.1",
|
|
39
|
+
"mypy>=2.4.0",
|
|
40
|
+
]
|
|
41
|
+
|
|
42
|
+
[build-system]
|
|
43
|
+
requires = ["uv_build>=0.13.0,<0.14"]
|
|
44
|
+
build-backend = "uv_build"
|
|
45
|
+
|
|
46
|
+
[tool.pytest.ini_options]
|
|
47
|
+
# The docstrings' examples are tests too.
|
|
48
|
+
testpaths = ["tests", "src"]
|
|
49
|
+
addopts = ["--strict-markers", "--strict-config", "-ra", "--doctest-modules"]
|
|
50
|
+
xfail_strict = true
|
|
51
|
+
|
|
52
|
+
[tool.ruff]
|
|
53
|
+
target-version = "py312"
|
|
54
|
+
line-length = 100
|
|
55
|
+
src = ["src", "tests"]
|
|
56
|
+
|
|
57
|
+
[tool.ruff.format]
|
|
58
|
+
docstring-code-format = true
|
|
59
|
+
|
|
60
|
+
[tool.ruff.lint]
|
|
61
|
+
select = [
|
|
62
|
+
"E", "W", "F", # pycodestyle, pyflakes
|
|
63
|
+
"I", # import order
|
|
64
|
+
"B", # bugbear
|
|
65
|
+
"UP", # pyupgrade
|
|
66
|
+
"SIM", # simplify
|
|
67
|
+
"RUF", # ruff-specific
|
|
68
|
+
"PT", # pytest style
|
|
69
|
+
"C4", # comprehensions
|
|
70
|
+
"N", # naming
|
|
71
|
+
"S", # bandit (security)
|
|
72
|
+
"PERF", # performance anti-patterns
|
|
73
|
+
"TID", # tidy imports
|
|
74
|
+
"PTH", # pathlib
|
|
75
|
+
"T20", # no print
|
|
76
|
+
"ARG", # unused arguments
|
|
77
|
+
"RET", # return style
|
|
78
|
+
"PLC", "PLE", "PLR", "PLW", # pylint subsets
|
|
79
|
+
"ERA", # commented-out code
|
|
80
|
+
"BLE", # blind except
|
|
81
|
+
]
|
|
82
|
+
ignore = [
|
|
83
|
+
"PLR0913", # keyword-only options read better than bundled option objects
|
|
84
|
+
]
|
|
85
|
+
|
|
86
|
+
[tool.ruff.lint.per-file-ignores]
|
|
87
|
+
"tests/**" = ["S101", "PLR2004", "ARG"]
|
|
88
|
+
|
|
89
|
+
[tool.ruff.lint.flake8-tidy-imports]
|
|
90
|
+
ban-relative-imports = "all"
|
|
91
|
+
|
|
92
|
+
[tool.mypy]
|
|
93
|
+
python_version = "3.12"
|
|
94
|
+
strict = true
|
|
95
|
+
warn_unreachable = true
|
|
96
|
+
enable_error_code = ["ignore-without-code", "redundant-expr", "truthy-bool", "possibly-undefined"]
|
|
97
|
+
files = ["src", "tests", "scripts"]
|