@procedures/ocr 0.68.9 → 0.68.10
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.
- package/CHANGELOG.md +10 -0
- package/README.md +21 -0
- package/dist/parse.d.ts +1 -0
- package/dist/parse.d.ts.map +1 -1
- package/dist/parse.js +1 -0
- package/dist/parse.js.map +1 -1
- package/dist/programs.d.ts +25 -1
- package/dist/programs.d.ts.map +1 -1
- package/dist/programs.js +26 -3
- package/dist/programs.js.map +1 -1
- package/dist/run.d.ts.map +1 -1
- package/dist/run.js +3 -0
- package/dist/run.js.map +1 -1
- package/dist/table.d.ts +59 -0
- package/dist/table.d.ts.map +1 -0
- package/dist/table.js +152 -0
- package/dist/table.js.map +1 -0
- package/package.json +4 -3
- package/src/parse.ts +1 -0
- package/src/programs.ts +39 -3
- package/src/run.ts +2 -0
- package/src/table.ts +188 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# @procedures/ocr
|
|
2
2
|
|
|
3
|
+
## 0.68.10
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- ee0504e: The new pure function `tableOfText` gives the rows and the columns of a table from the boxes of its words. Each row has one cell for each column, with its text, its confidence and its box. A right-aligned column of numbers stays one column.
|
|
8
|
+
- 4b926ae: When `pdftotext` or `pdftoppm` fails and its output comes from Xpdf, not from Poppler, the call fails with an `OcrToolMissingError` with `other: "Xpdf"`. The error tells you to install Poppler or to set `POPPLER_PATH`. Before, the call failed with the usage text of Xpdf. The new field `others` of a `ProgramSpec` lists the different programs with the same name, and the new export `XPDF` describes Xpdf.
|
|
9
|
+
- Updated dependencies [c2899fe]
|
|
10
|
+
- @procedures/shell@0.68.10
|
|
11
|
+
- @procedures/client@0.68.10
|
|
12
|
+
|
|
3
13
|
## 0.68.9
|
|
4
14
|
|
|
5
15
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -71,6 +71,24 @@ const scan = await outputValue(await invokePath(["ocr", "page"], { file: "auctio
|
|
|
71
71
|
|
|
72
72
|
The function `turnText(text, turn)` of the parsers does step 3 for a text from another source.
|
|
73
73
|
|
|
74
|
+
## A table
|
|
75
|
+
|
|
76
|
+
OCR gives words and lines, not cells. The pure function `tableOfText` gives the cells of a table from the boxes of its words:
|
|
77
|
+
|
|
78
|
+
1. Two words are in one row when the vertical centers of their boxes are near.
|
|
79
|
+
2. In a row, a wide gap between two words starts a new cell.
|
|
80
|
+
3. A column starts at an x position where many rows start a cell. A right-aligned column of numbers has starts that are far apart. Its cells end at the same x, so it stays one column.
|
|
81
|
+
4. Each word goes to the column that holds the center of its box.
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
import { tableOfText } from "@procedures/ocr/parse";
|
|
85
|
+
|
|
86
|
+
const table = tableOfText(scan, { minShare: 0.25 });
|
|
87
|
+
const prices = table.rows.map((row) => row.cells[3]?.text ?? null);
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Each row has one cell for each column, or `null` when the row has no word in that column. Each cell has its `text`, its `confidence`, its `box` and its `words`. The tolerances are shares of the median height of the words, so the same options work for pixels and for points. Give the words of one table only: a title above the table becomes a row too.
|
|
91
|
+
|
|
74
92
|
## The result
|
|
75
93
|
|
|
76
94
|
Each result has `unit`, `width`, `height`, `text`, `confidence` and `lines`. Each line has `text`, `confidence`, `box` and `words`. Each word has `text`, `confidence` and `box`.
|
|
@@ -99,6 +117,8 @@ The search examines these paths, in this order:
|
|
|
99
117
|
|
|
100
118
|
When the search finds no program, the call fails with an `OcrToolMissingError`. The error has the name of the program (`tool`), the paths that the search examined (`searched`) and the code `OCR_TOOL_MISSING`. A program that does not start gives the same error.
|
|
101
119
|
|
|
120
|
+
Xpdf has programs with the names `pdftotext` and `pdftoppm`, but not the options of Poppler. For example, Git for Windows puts the `pdftotext` of Xpdf on the `PATH`. When a run fails and its output comes from Xpdf, the call fails with an `OcrToolMissingError` with `other: "Xpdf"`. The error tells you to install Poppler or to set `POPPLER_PATH`. The field `others` of a `ProgramSpec` lists these different programs with the same name.
|
|
121
|
+
|
|
102
122
|
## The parsers
|
|
103
123
|
|
|
104
124
|
The parsers start no process and read no file. The root and the entry `@procedures/ocr/parse` export them. A browser can also load the entry `./parse`.
|
|
@@ -107,6 +127,7 @@ The parsers start no process and read no file. The root and the entry `@procedur
|
|
|
107
127
|
- `parsePdftotextBbox(xhtml)` reads the output of `pdftotext -bbox-layout` for one page.
|
|
108
128
|
- `parseTesseractOsd(output)` reads the orientation output of tesseract (`tesseract <image> stdout --psm 0`).
|
|
109
129
|
- `turnText(text, turn)` turns each box, the width and the height by 0, 90, 180 or 270 degrees in the clock direction.
|
|
130
|
+
- `tableOfText(text, options)` gives the rows and the columns of a table from the boxes of its words. Refer to [A table](#a-table).
|
|
110
131
|
- `scaleText(text, factor, unit)` changes the unit of the boxes. For example, the factor `72 / 300` changes the pixels of an image at 300 dots per inch to PDF points.
|
|
111
132
|
|
|
112
133
|
## Other engines
|
package/dist/parse.d.ts
CHANGED
package/dist/parse.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parse.d.ts","sourceRoot":"","sources":["../src/parse.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC"}
|
|
1
|
+
{"version":3,"file":"parse.d.ts","sourceRoot":"","sources":["../src/parse.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC;AACzB,cAAc,YAAY,CAAC"}
|
package/dist/parse.js
CHANGED
package/dist/parse.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parse.js","sourceRoot":"","sources":["../src/parse.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC"}
|
|
1
|
+
{"version":3,"file":"parse.js","sourceRoot":"","sources":["../src/parse.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC;AACzB,cAAc,YAAY,CAAC"}
|
package/dist/programs.d.ts
CHANGED
|
@@ -5,6 +5,13 @@
|
|
|
5
5
|
*/
|
|
6
6
|
/** The environment of the search. */
|
|
7
7
|
export type ProgramEnv = Readonly<Record<string, string | undefined>>;
|
|
8
|
+
/** A different program with the same name, for example the `pdftotext` of Xpdf. */
|
|
9
|
+
export interface OtherProgram {
|
|
10
|
+
/** The name of the other program, for the error, for example `Xpdf`. */
|
|
11
|
+
readonly name: string;
|
|
12
|
+
/** This function gives `true` when the output of a failed run comes from the other program. */
|
|
13
|
+
readonly matches: (output: string) => boolean;
|
|
14
|
+
}
|
|
8
15
|
/** The description of a program for the search. */
|
|
9
16
|
export interface ProgramSpec {
|
|
10
17
|
/** The name of the program, without `.exe`, for example `tesseract`. */
|
|
@@ -17,6 +24,11 @@ export interface ProgramSpec {
|
|
|
17
24
|
readonly installed: (platform: NodeJS.Platform, env: ProgramEnv) => readonly string[];
|
|
18
25
|
/** The text that tells how to install the program, for the error. */
|
|
19
26
|
readonly install: string;
|
|
27
|
+
/**
|
|
28
|
+
* The different programs with the same name. When a run fails and its output comes from one of
|
|
29
|
+
* them, the runner throws an `OcrToolMissingError`, not the error of the run.
|
|
30
|
+
*/
|
|
31
|
+
readonly others?: readonly OtherProgram[] | undefined;
|
|
20
32
|
}
|
|
21
33
|
/** The options of the search. Each one has a default from the host. */
|
|
22
34
|
export interface FindProgramOptions {
|
|
@@ -29,6 +41,12 @@ export interface FindProgramOptions {
|
|
|
29
41
|
}
|
|
30
42
|
/** The OCR engine tesseract. The installer for Windows puts it in `Tesseract-OCR`, and not on the `PATH`. */
|
|
31
43
|
export declare const TESSERACT: ProgramSpec;
|
|
44
|
+
/**
|
|
45
|
+
* The programs of Xpdf have the names of the programs of Poppler, but not their options: for
|
|
46
|
+
* example, the `pdftotext` of Xpdf has no `-bbox-layout`. Git for Windows puts Xpdf on the `PATH`.
|
|
47
|
+
* The usage text of Xpdf names Glyph & Cog. The usage text of Poppler names Glyph & Cog and Poppler.
|
|
48
|
+
*/
|
|
49
|
+
export declare const XPDF: OtherProgram;
|
|
32
50
|
/** The program of Poppler that gives the text layer of a PDF page. */
|
|
33
51
|
export declare const PDFTOTEXT: ProgramSpec;
|
|
34
52
|
/** The program of Poppler that makes an image of a PDF page. */
|
|
@@ -43,8 +61,14 @@ export declare class OcrToolMissingError extends Error {
|
|
|
43
61
|
readonly tool: string;
|
|
44
62
|
/** The paths that the search examined, in order. */
|
|
45
63
|
readonly searched: readonly string[];
|
|
64
|
+
/** The name of the different program that the search found, for example `Xpdf`, or `undefined`. */
|
|
65
|
+
readonly other: string | undefined;
|
|
46
66
|
readonly name: string;
|
|
47
|
-
|
|
67
|
+
/**
|
|
68
|
+
* The `cause` is the error of a start that failed. The `other` is the name of a different
|
|
69
|
+
* program with the same name at `searched[0]`, for example `Xpdf`.
|
|
70
|
+
*/
|
|
71
|
+
constructor(spec: ProgramSpec, searched: readonly string[], cause?: string, other?: string);
|
|
48
72
|
}
|
|
49
73
|
/**
|
|
50
74
|
* This function gives the paths that the search examines for a program, in order. The order is
|
package/dist/programs.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"programs.d.ts","sourceRoot":"","sources":["../src/programs.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAKH,qCAAqC;AACrC,MAAM,MAAM,UAAU,GAAG,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC,CAAC;AAEtE,mDAA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EAAE,KAAK,CAAC,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,EAYzF;CACF;AASD;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,CAAC,QAAQ,EAAE,GAAG,EAAE,UAAU,GAAG,MAAM,EAAE,CAMzG;AAED;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,WAAW,EAAE,OAAO,GAAE,kBAAkB,GAAG;IAAE,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CAAO,GAAG,MAAM,GAAG,SAAS,CASlJ;AAED,mHAAmH;AACnH,wBAAgB,cAAc,CAAC,IAAI,EAAE,WAAW,EAAE,OAAO,GAAE,kBAAkB,GAAG;IAAE,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CAAO,GAAG,MAAM,CAIzI"}
|
package/dist/programs.js
CHANGED
|
@@ -26,6 +26,15 @@ export const TESSERACT = {
|
|
|
26
26
|
},
|
|
27
27
|
install: "the package tesseract-ocr on Linux, brew install tesseract on macOS, or the Tesseract installer on Windows",
|
|
28
28
|
};
|
|
29
|
+
/**
|
|
30
|
+
* The programs of Xpdf have the names of the programs of Poppler, but not their options: for
|
|
31
|
+
* example, the `pdftotext` of Xpdf has no `-bbox-layout`. Git for Windows puts Xpdf on the `PATH`.
|
|
32
|
+
* The usage text of Xpdf names Glyph & Cog. The usage text of Poppler names Glyph & Cog and Poppler.
|
|
33
|
+
*/
|
|
34
|
+
export const XPDF = {
|
|
35
|
+
name: "Xpdf",
|
|
36
|
+
matches: (output) => /Glyph & Cog/.test(output) && !/poppler/i.test(output),
|
|
37
|
+
};
|
|
29
38
|
function poppler(name, env) {
|
|
30
39
|
return {
|
|
31
40
|
name,
|
|
@@ -37,6 +46,7 @@ function poppler(name, env) {
|
|
|
37
46
|
return platform === "darwin" ? HOMEBREW_FOLDERS.map((folder) => posix.join(folder, name)) : [];
|
|
38
47
|
},
|
|
39
48
|
install: "the package poppler-utils on Linux, brew install poppler on macOS, or a Poppler build on Windows",
|
|
49
|
+
others: [XPDF],
|
|
40
50
|
};
|
|
41
51
|
}
|
|
42
52
|
/** The program of Poppler that gives the text layer of a PDF page. */
|
|
@@ -53,13 +63,26 @@ export class OcrToolMissingError extends Error {
|
|
|
53
63
|
tool;
|
|
54
64
|
/** The paths that the search examined, in order. */
|
|
55
65
|
searched;
|
|
66
|
+
/** The name of the different program that the search found, for example `Xpdf`, or `undefined`. */
|
|
67
|
+
other;
|
|
56
68
|
name = "OcrToolMissingError";
|
|
57
|
-
|
|
69
|
+
/**
|
|
70
|
+
* The `cause` is the error of a start that failed. The `other` is the name of a different
|
|
71
|
+
* program with the same name at `searched[0]`, for example `Xpdf`.
|
|
72
|
+
*/
|
|
73
|
+
constructor(spec, searched, cause, other) {
|
|
58
74
|
const variables = [...spec.env, ...(spec.folderEnv ?? [])].join(" or ");
|
|
59
|
-
const
|
|
60
|
-
|
|
75
|
+
const fix = `Install ${spec.install}, or set ${variables}.`;
|
|
76
|
+
if (other !== undefined) {
|
|
77
|
+
super(`The program ${spec.name} at ${searched[0] ?? "the path of the search"} is the program of ${other}, and this package cannot use it. ${fix}`);
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
const reason = cause === undefined ? `The search examined ${searched.length} paths.` : `It did not start: ${cause}.`;
|
|
81
|
+
super(`The program ${spec.name} is not on this host. ${reason} ${fix}`);
|
|
82
|
+
}
|
|
61
83
|
this.tool = spec.name;
|
|
62
84
|
this.searched = searched;
|
|
85
|
+
this.other = other;
|
|
63
86
|
}
|
|
64
87
|
}
|
|
65
88
|
/** This function gives the folders of the `PATH`, in order. */
|
package/dist/programs.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"programs.js","sourceRoot":"","sources":["../src/programs.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,UAAU,EAA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AmB,GAAG,SAAS,CA6BvE"}
|
package/dist/run.js
CHANGED
|
@@ -38,6 +38,9 @@ export function createOcrRunner(options = {}) {
|
|
|
38
38
|
if (notFound(result))
|
|
39
39
|
throw new OcrToolMissingError(spec, [input.command], result.stderr.trim());
|
|
40
40
|
if (result.exitCode !== 0) {
|
|
41
|
+
const other = spec.others?.find((program) => program.matches(`${result.stderr}\n${result.stdout}`));
|
|
42
|
+
if (other !== undefined)
|
|
43
|
+
throw new OcrToolMissingError(spec, [input.command], undefined, other.name);
|
|
41
44
|
const text = result.stderr.trim() || result.stdout.trim();
|
|
42
45
|
throw new CliToolError(`${spec.name} failed (exit code ${result.exitCode}): ${text}`.trim(), result.exitCode, result.stderr);
|
|
43
46
|
}
|
package/dist/run.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"run.js","sourceRoot":"","sources":["../src/run.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,EAAE,YAAY,EAAE,aAAa,EAAgB,MAAM,uBAAuB,CAAC;AAClF,OAAO,EAAE,eAAe,EAAkB,MAAM,aAAa,CAAC;AAC9D,OAAO,EAAE,mBAAmB,EAAE,cAAc,EAA6C,MAAM,eAAe,CAAC;AAE/G,8DAA8D;AAC9D,MAAM,CAAC,MAAM,kBAAkB,GAAG,OAAO,CAAC;AAuB1C;;;GAGG;AACH,SAAS,QAAQ,CAAC,MAA4D;IAC5E,OAAO,MAAM,CAAC,QAAQ,KAAK,CAAC,IAAI,MAAM,CAAC,MAAM,KAAK,EAAE,IAAI,mBAAmB,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC;AACzG,CAAC;AAED,oFAAoF;AACpF,MAAM,UAAU,eAAe,CAAC,OAAO,GAAmB,EAAE;IAC1D,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,IAAI,eAAe,CAAC;IACjD,MAAM,IAAI,GAAuB,EAAE,GAAG,EAAE,OAAO,CAAC,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BAC1B,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,KAAK,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;gBACpG,IAAI,KAAK,KAAK,SAAS;oBAAE,MAAM,IAAI,mBAAmB,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,EAAE,SAAS,EAAE,KAAK,CAAC,IAAI,CAAC,CAAC;gBACrG,MAAM,IAAI,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,IAAI,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;gBAC1D,MAAM,IAAI,YAAY,CAAC,GAAG,IAAI,CAAC,IAAI,sBAAsB,MAAM,CAAC,QAAQ,MAAM,IAAI,EAAE,CAAC,IAAI,EAAE,EAAE,MAAM,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC;YAC/H,CAAC;YACD,OAAO,MAAM,CAAC,MAAM,CAAC;QACvB,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/dist/table.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The rows and the columns of a table, from the boxes of its words. This file starts no process and
|
|
3
|
+
* reads no file.
|
|
4
|
+
*
|
|
5
|
+
* OCR gives words and lines, not cells. A scan of a table needs its cells, for example to read
|
|
6
|
+
* one column of each row. The method uses only the boxes:
|
|
7
|
+
*
|
|
8
|
+
* 1. Two words are in one row when the vertical centers of their boxes are near.
|
|
9
|
+
* 2. In a row, a wide gap between two words starts a new cell.
|
|
10
|
+
* 3. A column starts at an x position where many rows start a cell.
|
|
11
|
+
* 4. Each word goes to the column that holds the center of its box.
|
|
12
|
+
*
|
|
13
|
+
* The lengths of the options are shares of the median height of the words, so one set of options
|
|
14
|
+
* works for pixels and for points.
|
|
15
|
+
*/
|
|
16
|
+
import { type OcrText, type OcrWord, type Rect } from "./text.js";
|
|
17
|
+
/** The options of `tableOfText`. Each length is a share of the median height of the words. */
|
|
18
|
+
export interface OcrTableOptions {
|
|
19
|
+
/** Two words are in one row when their vertical centers differ by less than this share. The default is 0.7. */
|
|
20
|
+
readonly rowTolerance?: number | undefined;
|
|
21
|
+
/** In a row, a gap of more than this share between two words starts a new cell. The default is 0.75. */
|
|
22
|
+
readonly cellGap?: number | undefined;
|
|
23
|
+
/** Two starts of cells are in one column when they differ by no more than this share. The default is 1. */
|
|
24
|
+
readonly columnTolerance?: number | undefined;
|
|
25
|
+
/** A column needs starts of cells in this share of the rows, from 0 to 1. The default is 0.25. */
|
|
26
|
+
readonly minShare?: number | undefined;
|
|
27
|
+
}
|
|
28
|
+
/** One cell: its words, their text with one space between words, the mean confidence and the union of the boxes. */
|
|
29
|
+
export interface OcrTableCell {
|
|
30
|
+
readonly text: string;
|
|
31
|
+
readonly confidence: number;
|
|
32
|
+
readonly box: Rect;
|
|
33
|
+
readonly words: readonly OcrWord[];
|
|
34
|
+
}
|
|
35
|
+
/** One row: the union of the boxes of its words, and one cell for each column. A column with no word in the row has `null`. */
|
|
36
|
+
export interface OcrTableRow {
|
|
37
|
+
readonly box: Rect;
|
|
38
|
+
readonly cells: readonly (OcrTableCell | null)[];
|
|
39
|
+
}
|
|
40
|
+
/** A table: the left edge of each column, from left to right, and the rows from top to bottom. */
|
|
41
|
+
export interface OcrTable {
|
|
42
|
+
readonly columns: readonly number[];
|
|
43
|
+
readonly rows: readonly OcrTableRow[];
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* This function gives the rows and the columns of a table from the boxes of its words. Give the
|
|
47
|
+
* words of one table only: a title or a note above the table becomes a row too.
|
|
48
|
+
*
|
|
49
|
+
* - A row has one cell for each column. A cell is `null` when the row has no word in its column.
|
|
50
|
+
* - A column that holds few starts of cells (less than `minShare` of the rows) is not a column. Its
|
|
51
|
+
* words go to the column at their left.
|
|
52
|
+
* - A word with an empty text is not a word of the table.
|
|
53
|
+
*
|
|
54
|
+
* @param source - A text from the OCR, or its words
|
|
55
|
+
* @param options - The tolerances, as shares of the median height of the words
|
|
56
|
+
* @throws RangeError when an option is not a finite number of 0 or more
|
|
57
|
+
*/
|
|
58
|
+
export declare function tableOfText(source: OcrText | readonly OcrWord[], options?: OcrTableOptions): OcrTable;
|
|
59
|
+
//# sourceMappingURL=table.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"table.d.ts","sourceRoot":"","sources":["../src/table.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAGH,OAAO,EAAY,KAAK,OAAO,EAAE,KAAK,OAAO,EAAE,KAAK,IAAI,EAAE,MAAM,WAAW,CAAC;AAE5E,8FAA8F;AAC9F,MAAM,WAAW,eAAe;IAC9B,+GAA+G;IAC/G,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC3C,wGAAwG;IACxG,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACtC,2GAA2G;IAC3G,QAAQ,CAAC,eAAe,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC9C,kGAAkG;IAClG,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CACxC;AAED,oHAAoH;AACpH,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,GAAG,EAAE,IAAI,CAAC;IACnB,QAAQ,CAAC,KAAK,EAAE,SAAS,OAAO,EAAE,CAAC;CACpC;AAED,+HAA+H;AAC/H,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,GAAG,EAAE,IAAI,CAAC;IACnB,QAAQ,CAAC,KAAK,EAAE,SAAS,CAAC,YAAY,GAAG,IAAI,CAAC,EAAE,CAAC;CAClD;AAED,kGAAkG;AAClG,MAAM,WAAW,QAAQ;IACvB,QAAQ,CAAC,OAAO,EAAE,SAAS,MAAM,EAAE,CAAC;IACpC,QAAQ,CAAC,IAAI,EAAE,SAAS,WAAW,EAAE,CAAC;CACvC;AAuGD;;;;;;;;;;;;GAYG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,OAAO,GAAG,SAAS,OAAO,EAAE,EAAE,OAAO,GAAE,eAAoB,GAAG,QAAQ,CAsBzG"}
|
package/dist/table.js
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The rows and the columns of a table, from the boxes of its words. This file starts no process and
|
|
3
|
+
* reads no file.
|
|
4
|
+
*
|
|
5
|
+
* OCR gives words and lines, not cells. A scan of a table needs its cells, for example to read
|
|
6
|
+
* one column of each row. The method uses only the boxes:
|
|
7
|
+
*
|
|
8
|
+
* 1. Two words are in one row when the vertical centers of their boxes are near.
|
|
9
|
+
* 2. In a row, a wide gap between two words starts a new cell.
|
|
10
|
+
* 3. A column starts at an x position where many rows start a cell.
|
|
11
|
+
* 4. Each word goes to the column that holds the center of its box.
|
|
12
|
+
*
|
|
13
|
+
* The lengths of the options are shares of the median height of the words, so one set of options
|
|
14
|
+
* works for pixels and for points.
|
|
15
|
+
*/
|
|
16
|
+
import { median } from "@mark1russell7/rigor-statistics";
|
|
17
|
+
import { unionBox } from "./text.js";
|
|
18
|
+
/** This function gives the value of an option, or its default. It refuses a value that is not finite or less than 0. */
|
|
19
|
+
function option(name, value, fallback) {
|
|
20
|
+
if (value === undefined)
|
|
21
|
+
return fallback;
|
|
22
|
+
if (!Number.isFinite(value) || value < 0)
|
|
23
|
+
throw new RangeError(`tableOfText: the option ${name} is ${value}. It is a finite number of 0 or more.`);
|
|
24
|
+
return value;
|
|
25
|
+
}
|
|
26
|
+
const center = (box) => box.x + box.w / 2;
|
|
27
|
+
const middle = (box) => box.y + box.h / 2;
|
|
28
|
+
/** This function groups the words into rows, from top to bottom. Each row has its words from left to right. */
|
|
29
|
+
function groupRows(words, tolerance) {
|
|
30
|
+
const sorted = [...words].sort((a, b) => middle(a.box) - middle(b.box) || a.box.x - b.box.x);
|
|
31
|
+
const rows = [];
|
|
32
|
+
for (const word of sorted) {
|
|
33
|
+
const row = rows[rows.length - 1];
|
|
34
|
+
const value = middle(word.box);
|
|
35
|
+
if (row !== undefined && Math.abs(value - row.mean) < tolerance) {
|
|
36
|
+
row.words.push(word);
|
|
37
|
+
row.mean += (value - row.mean) / row.words.length;
|
|
38
|
+
}
|
|
39
|
+
else
|
|
40
|
+
rows.push({ mean: value, words: [word] });
|
|
41
|
+
}
|
|
42
|
+
return rows.map((row) => row.words.sort((a, b) => a.box.x - b.box.x));
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* This function gives the left edge of each column.
|
|
46
|
+
*
|
|
47
|
+
* - Each start of a cell goes to a cluster of starts that are near.
|
|
48
|
+
* - A column of numbers is often right-aligned. Then its starts are far apart, and they make
|
|
49
|
+
* two clusters or more. Two clusters next to each other can be one column.
|
|
50
|
+
* This is true when the median right edges of their cells are near, and no row has a cell in each cluster.
|
|
51
|
+
* Also, the left edge of the two clusters must be to the right of each cell of the column before them.
|
|
52
|
+
* Thus a long cell that starts inside the column before does not move the edge of the next column.
|
|
53
|
+
* - A cluster with starts in less than `minShare` of the rows is not a column.
|
|
54
|
+
*/
|
|
55
|
+
function findColumns(rows, gap, tolerance, minShare) {
|
|
56
|
+
const starts = [];
|
|
57
|
+
rows.forEach((words, row) => {
|
|
58
|
+
let first = 0;
|
|
59
|
+
words.forEach((word, index) => {
|
|
60
|
+
const next = words[index + 1];
|
|
61
|
+
if (next === undefined || next.box.x - (word.box.x + word.box.w) > gap) {
|
|
62
|
+
const start = words[first];
|
|
63
|
+
starts.push({ x: start.box.x, end: word.box.x + word.box.w, row });
|
|
64
|
+
first = index + 1;
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
starts.sort((a, b) => a.x - b.x);
|
|
69
|
+
const clusters = [];
|
|
70
|
+
for (const start of starts) {
|
|
71
|
+
const cluster = clusters[clusters.length - 1];
|
|
72
|
+
const last = cluster?.[cluster.length - 1];
|
|
73
|
+
if (cluster !== undefined && last !== undefined && start.x - last.x <= tolerance)
|
|
74
|
+
cluster.push(start);
|
|
75
|
+
else
|
|
76
|
+
clusters.push([start]);
|
|
77
|
+
}
|
|
78
|
+
const merged = [];
|
|
79
|
+
for (const cluster of clusters) {
|
|
80
|
+
const before = merged[merged.length - 1];
|
|
81
|
+
const rowsBefore = new Set(before?.map((start) => start.row));
|
|
82
|
+
const previous = merged[merged.length - 2];
|
|
83
|
+
const aligned = before !== undefined &&
|
|
84
|
+
(previous === undefined || Math.min(...before.map((start) => start.x)) >= Math.max(...previous.map((start) => start.end))) &&
|
|
85
|
+
Math.abs(median(before.map((start) => start.end)) - median(cluster.map((start) => start.end))) <= tolerance &&
|
|
86
|
+
cluster.every((start) => !rowsBefore.has(start.row));
|
|
87
|
+
if (aligned)
|
|
88
|
+
before.push(...cluster);
|
|
89
|
+
else
|
|
90
|
+
merged.push([...cluster]);
|
|
91
|
+
}
|
|
92
|
+
return merged
|
|
93
|
+
.filter((cluster) => new Set(cluster.map((start) => start.row)).size >= minShare * rows.length)
|
|
94
|
+
.map((cluster) => Math.min(...cluster.map((start) => start.x)));
|
|
95
|
+
}
|
|
96
|
+
/** This function gives the index of the column that holds the x position: the last column with its left edge at or before it. */
|
|
97
|
+
function columnOf(columns, x) {
|
|
98
|
+
let index = 0;
|
|
99
|
+
columns.forEach((edge, column) => {
|
|
100
|
+
if (edge <= x)
|
|
101
|
+
index = column;
|
|
102
|
+
});
|
|
103
|
+
return index;
|
|
104
|
+
}
|
|
105
|
+
/** This function makes a cell from its words, in their order from left to right. */
|
|
106
|
+
function makeCell(words) {
|
|
107
|
+
return {
|
|
108
|
+
text: words.map((word) => word.text).join(" "),
|
|
109
|
+
confidence: words.reduce((sum, word) => sum + word.confidence, 0) / words.length,
|
|
110
|
+
box: unionBox(words.map((word) => word.box)),
|
|
111
|
+
words,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* This function gives the rows and the columns of a table from the boxes of its words. Give the
|
|
116
|
+
* words of one table only: a title or a note above the table becomes a row too.
|
|
117
|
+
*
|
|
118
|
+
* - A row has one cell for each column. A cell is `null` when the row has no word in its column.
|
|
119
|
+
* - A column that holds few starts of cells (less than `minShare` of the rows) is not a column. Its
|
|
120
|
+
* words go to the column at their left.
|
|
121
|
+
* - A word with an empty text is not a word of the table.
|
|
122
|
+
*
|
|
123
|
+
* @param source - A text from the OCR, or its words
|
|
124
|
+
* @param options - The tolerances, as shares of the median height of the words
|
|
125
|
+
* @throws RangeError when an option is not a finite number of 0 or more
|
|
126
|
+
*/
|
|
127
|
+
export function tableOfText(source, options = {}) {
|
|
128
|
+
const all = "lines" in source ? source.lines.flatMap((line) => line.words) : source;
|
|
129
|
+
const words = all.filter((word) => word.text.trim().length > 0);
|
|
130
|
+
const rowTolerance = option("rowTolerance", options.rowTolerance, 0.7);
|
|
131
|
+
const cellGap = option("cellGap", options.cellGap, 0.75);
|
|
132
|
+
const columnTolerance = option("columnTolerance", options.columnTolerance, 1);
|
|
133
|
+
const minShare = option("minShare", options.minShare, 0.25);
|
|
134
|
+
if (words.length === 0)
|
|
135
|
+
return { columns: [], rows: [] };
|
|
136
|
+
const heights = words.map((word) => word.box.h).sort((a, b) => a - b);
|
|
137
|
+
const height = heights[Math.floor(heights.length / 2)];
|
|
138
|
+
const rows = groupRows(words, rowTolerance * height);
|
|
139
|
+
const found = findColumns(rows, cellGap * height, columnTolerance * height, minShare);
|
|
140
|
+
// With no column that passes minShare, the table has one column, so that each word stays in a cell
|
|
141
|
+
const columns = found.length > 0 ? found : [Math.min(...words.map((word) => word.box.x))];
|
|
142
|
+
return {
|
|
143
|
+
columns,
|
|
144
|
+
rows: rows.map((row) => {
|
|
145
|
+
const cells = columns.map(() => []);
|
|
146
|
+
for (const word of row)
|
|
147
|
+
cells[columnOf(columns, center(word.box))].push(word);
|
|
148
|
+
return { box: unionBox(row.map((word) => word.box)), cells: cells.map((cell) => (cell.length === 0 ? null : makeCell(cell))) };
|
|
149
|
+
}),
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
//# sourceMappingURL=table.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"table.js","sourceRoot":"","sources":["../src/table.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,iCAAiC,CAAC;AACzD,OAAO,EAAE,QAAQ,EAAyC,MAAM,WAAW,CAAC;AAkC5E,wHAAwH;AACxH,SAAS,MAAM,CAAC,IAAY,EAAE,KAAyB,EAAE,QAAgB;IACvE,IAAI,KAAK,KAAK,SAAS;QAAE,OAAO,QAAQ,CAAC;IACzC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,KAAK,GAAG,CAAC;QAAE,MAAM,IAAI,UAAU,CAAC,2BAA2B,IAAI,OAAO,KAAK,uCAAuC,CAAC,CAAC;IACnJ,OAAO,KAAK,CAAC;AACf,CAAC;AAED,MAAM,MAAM,GAAG,CAAC,GAAS,EAAU,EAAE,CAAC,GAAG,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC;AACxD,MAAM,MAAM,GAAG,CAAC,GAAS,EAAU,EAAE,CAAC,GAAG,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC;AAExD,+GAA+G;AAC/G,SAAS,SAAS,CAAC,KAAyB,EAAE,SAAiB;IAC7D,MAAM,MAAM,GAAG,CAAC,GAAG,KAAK,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC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",
|
|
4
|
-
"version": "0.68.
|
|
4
|
+
"version": "0.68.10",
|
|
5
5
|
"description": "The text of a PDF page or an image, with the text, the confidence and the box of each line and word: the text layer of the page first (pdftotext), then OCR with a local engine (tesseract).",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"ocr",
|
|
@@ -81,8 +81,9 @@
|
|
|
81
81
|
},
|
|
82
82
|
"dependencies": {
|
|
83
83
|
"@mark1russell7/layout-contract": "^0.11.1",
|
|
84
|
-
"@
|
|
85
|
-
"@procedures/
|
|
84
|
+
"@mark1russell7/rigor-statistics": "^0.2.19",
|
|
85
|
+
"@procedures/client": "0.68.10",
|
|
86
|
+
"@procedures/shell": "0.68.10",
|
|
86
87
|
"zod": "^4.6.5"
|
|
87
88
|
},
|
|
88
89
|
"scripts": {
|
package/src/parse.ts
CHANGED
package/src/programs.ts
CHANGED
|
@@ -10,6 +10,14 @@ import { posix, win32 } from "node:path";
|
|
|
10
10
|
/** The environment of the search. */
|
|
11
11
|
export type ProgramEnv = Readonly<Record<string, string | undefined>>;
|
|
12
12
|
|
|
13
|
+
/** A different program with the same name, for example the `pdftotext` of Xpdf. */
|
|
14
|
+
export interface OtherProgram {
|
|
15
|
+
/** The name of the other program, for the error, for example `Xpdf`. */
|
|
16
|
+
readonly name: string;
|
|
17
|
+
/** This function gives `true` when the output of a failed run comes from the other program. */
|
|
18
|
+
readonly matches: (output: string) => boolean;
|
|
19
|
+
}
|
|
20
|
+
|
|
13
21
|
/** The description of a program for the search. */
|
|
14
22
|
export interface ProgramSpec {
|
|
15
23
|
/** The name of the program, without `.exe`, for example `tesseract`. */
|
|
@@ -22,6 +30,11 @@ export interface ProgramSpec {
|
|
|
22
30
|
readonly installed: (platform: NodeJS.Platform, env: ProgramEnv) => readonly string[];
|
|
23
31
|
/** The text that tells how to install the program, for the error. */
|
|
24
32
|
readonly install: string;
|
|
33
|
+
/**
|
|
34
|
+
* The different programs with the same name. When a run fails and its output comes from one of
|
|
35
|
+
* them, the runner throws an `OcrToolMissingError`, not the error of the run.
|
|
36
|
+
*/
|
|
37
|
+
readonly others?: readonly OtherProgram[] | undefined;
|
|
25
38
|
}
|
|
26
39
|
|
|
27
40
|
/** The options of the search. Each one has a default from the host. */
|
|
@@ -58,6 +71,16 @@ export const TESSERACT: ProgramSpec = {
|
|
|
58
71
|
install: "the package tesseract-ocr on Linux, brew install tesseract on macOS, or the Tesseract installer on Windows",
|
|
59
72
|
};
|
|
60
73
|
|
|
74
|
+
/**
|
|
75
|
+
* The programs of Xpdf have the names of the programs of Poppler, but not their options: for
|
|
76
|
+
* example, the `pdftotext` of Xpdf has no `-bbox-layout`. Git for Windows puts Xpdf on the `PATH`.
|
|
77
|
+
* The usage text of Xpdf names Glyph & Cog. The usage text of Poppler names Glyph & Cog and Poppler.
|
|
78
|
+
*/
|
|
79
|
+
export const XPDF: OtherProgram = {
|
|
80
|
+
name: "Xpdf",
|
|
81
|
+
matches: (output) => /Glyph & Cog/.test(output) && !/poppler/i.test(output),
|
|
82
|
+
};
|
|
83
|
+
|
|
61
84
|
function poppler(name: string, env: string): ProgramSpec {
|
|
62
85
|
return {
|
|
63
86
|
name,
|
|
@@ -68,6 +91,7 @@ function poppler(name: string, env: string): ProgramSpec {
|
|
|
68
91
|
return platform === "darwin" ? HOMEBREW_FOLDERS.map((folder) => posix.join(folder, name)) : [];
|
|
69
92
|
},
|
|
70
93
|
install: "the package poppler-utils on Linux, brew install poppler on macOS, or a Poppler build on Windows",
|
|
94
|
+
others: [XPDF],
|
|
71
95
|
};
|
|
72
96
|
}
|
|
73
97
|
|
|
@@ -88,14 +112,26 @@ export class OcrToolMissingError extends Error {
|
|
|
88
112
|
readonly tool: string;
|
|
89
113
|
/** The paths that the search examined, in order. */
|
|
90
114
|
readonly searched: readonly string[];
|
|
115
|
+
/** The name of the different program that the search found, for example `Xpdf`, or `undefined`. */
|
|
116
|
+
readonly other: string | undefined;
|
|
91
117
|
|
|
92
118
|
override readonly name: string = "OcrToolMissingError";
|
|
93
|
-
|
|
119
|
+
/**
|
|
120
|
+
* The `cause` is the error of a start that failed. The `other` is the name of a different
|
|
121
|
+
* program with the same name at `searched[0]`, for example `Xpdf`.
|
|
122
|
+
*/
|
|
123
|
+
constructor(spec: ProgramSpec, searched: readonly string[], cause?: string, other?: string) {
|
|
94
124
|
const variables = [...spec.env, ...(spec.folderEnv ?? [])].join(" or ");
|
|
95
|
-
const
|
|
96
|
-
|
|
125
|
+
const fix = `Install ${spec.install}, or set ${variables}.`;
|
|
126
|
+
if (other !== undefined) {
|
|
127
|
+
super(`The program ${spec.name} at ${searched[0] ?? "the path of the search"} is the program of ${other}, and this package cannot use it. ${fix}`);
|
|
128
|
+
} else {
|
|
129
|
+
const reason = cause === undefined ? `The search examined ${searched.length} paths.` : `It did not start: ${cause}.`;
|
|
130
|
+
super(`The program ${spec.name} is not on this host. ${reason} ${fix}`);
|
|
131
|
+
}
|
|
97
132
|
this.tool = spec.name;
|
|
98
133
|
this.searched = searched;
|
|
134
|
+
this.other = other;
|
|
99
135
|
}
|
|
100
136
|
}
|
|
101
137
|
|
package/src/run.ts
CHANGED
|
@@ -63,6 +63,8 @@ export function createOcrRunner(options: OcrHostOptions = {}): OcrRunner {
|
|
|
63
63
|
const result = await tool.call(args, { timeout }, ctx);
|
|
64
64
|
if (notFound(result)) throw new OcrToolMissingError(spec, [input.command], result.stderr.trim());
|
|
65
65
|
if (result.exitCode !== 0) {
|
|
66
|
+
const other = spec.others?.find((program) => program.matches(`${result.stderr}\n${result.stdout}`));
|
|
67
|
+
if (other !== undefined) throw new OcrToolMissingError(spec, [input.command], undefined, other.name);
|
|
66
68
|
const text = result.stderr.trim() || result.stdout.trim();
|
|
67
69
|
throw new CliToolError(`${spec.name} failed (exit code ${result.exitCode}): ${text}`.trim(), result.exitCode, result.stderr);
|
|
68
70
|
}
|
package/src/table.ts
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The rows and the columns of a table, from the boxes of its words. This file starts no process and
|
|
3
|
+
* reads no file.
|
|
4
|
+
*
|
|
5
|
+
* OCR gives words and lines, not cells. A scan of a table needs its cells, for example to read
|
|
6
|
+
* one column of each row. The method uses only the boxes:
|
|
7
|
+
*
|
|
8
|
+
* 1. Two words are in one row when the vertical centers of their boxes are near.
|
|
9
|
+
* 2. In a row, a wide gap between two words starts a new cell.
|
|
10
|
+
* 3. A column starts at an x position where many rows start a cell.
|
|
11
|
+
* 4. Each word goes to the column that holds the center of its box.
|
|
12
|
+
*
|
|
13
|
+
* The lengths of the options are shares of the median height of the words, so one set of options
|
|
14
|
+
* works for pixels and for points.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { median } from "@mark1russell7/rigor-statistics";
|
|
18
|
+
import { unionBox, type OcrText, type OcrWord, type Rect } from "./text.js";
|
|
19
|
+
|
|
20
|
+
/** The options of `tableOfText`. Each length is a share of the median height of the words. */
|
|
21
|
+
export interface OcrTableOptions {
|
|
22
|
+
/** Two words are in one row when their vertical centers differ by less than this share. The default is 0.7. */
|
|
23
|
+
readonly rowTolerance?: number | undefined;
|
|
24
|
+
/** In a row, a gap of more than this share between two words starts a new cell. The default is 0.75. */
|
|
25
|
+
readonly cellGap?: number | undefined;
|
|
26
|
+
/** Two starts of cells are in one column when they differ by no more than this share. The default is 1. */
|
|
27
|
+
readonly columnTolerance?: number | undefined;
|
|
28
|
+
/** A column needs starts of cells in this share of the rows, from 0 to 1. The default is 0.25. */
|
|
29
|
+
readonly minShare?: number | undefined;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** One cell: its words, their text with one space between words, the mean confidence and the union of the boxes. */
|
|
33
|
+
export interface OcrTableCell {
|
|
34
|
+
readonly text: string;
|
|
35
|
+
readonly confidence: number;
|
|
36
|
+
readonly box: Rect;
|
|
37
|
+
readonly words: readonly OcrWord[];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** One row: the union of the boxes of its words, and one cell for each column. A column with no word in the row has `null`. */
|
|
41
|
+
export interface OcrTableRow {
|
|
42
|
+
readonly box: Rect;
|
|
43
|
+
readonly cells: readonly (OcrTableCell | null)[];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** A table: the left edge of each column, from left to right, and the rows from top to bottom. */
|
|
47
|
+
export interface OcrTable {
|
|
48
|
+
readonly columns: readonly number[];
|
|
49
|
+
readonly rows: readonly OcrTableRow[];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** This function gives the value of an option, or its default. It refuses a value that is not finite or less than 0. */
|
|
53
|
+
function option(name: string, value: number | undefined, fallback: number): number {
|
|
54
|
+
if (value === undefined) return fallback;
|
|
55
|
+
if (!Number.isFinite(value) || value < 0) throw new RangeError(`tableOfText: the option ${name} is ${value}. It is a finite number of 0 or more.`);
|
|
56
|
+
return value;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const center = (box: Rect): number => box.x + box.w / 2;
|
|
60
|
+
const middle = (box: Rect): number => box.y + box.h / 2;
|
|
61
|
+
|
|
62
|
+
/** This function groups the words into rows, from top to bottom. Each row has its words from left to right. */
|
|
63
|
+
function groupRows(words: readonly OcrWord[], tolerance: number): OcrWord[][] {
|
|
64
|
+
const sorted = [...words].sort((a, b) => middle(a.box) - middle(b.box) || a.box.x - b.box.x);
|
|
65
|
+
const rows: { mean: number; words: OcrWord[] }[] = [];
|
|
66
|
+
for (const word of sorted) {
|
|
67
|
+
const row = rows[rows.length - 1];
|
|
68
|
+
const value = middle(word.box);
|
|
69
|
+
if (row !== undefined && Math.abs(value - row.mean) < tolerance) {
|
|
70
|
+
row.words.push(word);
|
|
71
|
+
row.mean += (value - row.mean) / row.words.length;
|
|
72
|
+
} else rows.push({ mean: value, words: [word] });
|
|
73
|
+
}
|
|
74
|
+
return rows.map((row) => row.words.sort((a, b) => a.box.x - b.box.x));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** One start of a cell: the left edge and the right edge of the cell, and the index of its row. */
|
|
78
|
+
interface CellStart {
|
|
79
|
+
readonly x: number;
|
|
80
|
+
readonly end: number;
|
|
81
|
+
readonly row: number;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* This function gives the left edge of each column.
|
|
86
|
+
*
|
|
87
|
+
* - Each start of a cell goes to a cluster of starts that are near.
|
|
88
|
+
* - A column of numbers is often right-aligned. Then its starts are far apart, and they make
|
|
89
|
+
* two clusters or more. Two clusters next to each other can be one column.
|
|
90
|
+
* This is true when the median right edges of their cells are near, and no row has a cell in each cluster.
|
|
91
|
+
* Also, the left edge of the two clusters must be to the right of each cell of the column before them.
|
|
92
|
+
* Thus a long cell that starts inside the column before does not move the edge of the next column.
|
|
93
|
+
* - A cluster with starts in less than `minShare` of the rows is not a column.
|
|
94
|
+
*/
|
|
95
|
+
function findColumns(rows: readonly OcrWord[][], gap: number, tolerance: number, minShare: number): number[] {
|
|
96
|
+
const starts: CellStart[] = [];
|
|
97
|
+
rows.forEach((words, row) => {
|
|
98
|
+
let first = 0;
|
|
99
|
+
words.forEach((word, index) => {
|
|
100
|
+
const next = words[index + 1];
|
|
101
|
+
if (next === undefined || next.box.x - (word.box.x + word.box.w) > gap) {
|
|
102
|
+
const start = words[first] as OcrWord;
|
|
103
|
+
starts.push({ x: start.box.x, end: word.box.x + word.box.w, row });
|
|
104
|
+
first = index + 1;
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
starts.sort((a, b) => a.x - b.x);
|
|
109
|
+
const clusters: CellStart[][] = [];
|
|
110
|
+
for (const start of starts) {
|
|
111
|
+
const cluster = clusters[clusters.length - 1];
|
|
112
|
+
const last = cluster?.[cluster.length - 1];
|
|
113
|
+
if (cluster !== undefined && last !== undefined && start.x - last.x <= tolerance) cluster.push(start);
|
|
114
|
+
else clusters.push([start]);
|
|
115
|
+
}
|
|
116
|
+
const merged: CellStart[][] = [];
|
|
117
|
+
for (const cluster of clusters) {
|
|
118
|
+
const before = merged[merged.length - 1];
|
|
119
|
+
const rowsBefore = new Set(before?.map((start) => start.row));
|
|
120
|
+
const previous = merged[merged.length - 2];
|
|
121
|
+
const aligned =
|
|
122
|
+
before !== undefined &&
|
|
123
|
+
(previous === undefined || Math.min(...before.map((start) => start.x)) >= Math.max(...previous.map((start) => start.end))) &&
|
|
124
|
+
Math.abs(median(before.map((start) => start.end)) - median(cluster.map((start) => start.end))) <= tolerance &&
|
|
125
|
+
cluster.every((start) => !rowsBefore.has(start.row));
|
|
126
|
+
if (aligned) before.push(...cluster);
|
|
127
|
+
else merged.push([...cluster]);
|
|
128
|
+
}
|
|
129
|
+
return merged
|
|
130
|
+
.filter((cluster) => new Set(cluster.map((start) => start.row)).size >= minShare * rows.length)
|
|
131
|
+
.map((cluster) => Math.min(...cluster.map((start) => start.x)));
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** This function gives the index of the column that holds the x position: the last column with its left edge at or before it. */
|
|
135
|
+
function columnOf(columns: readonly number[], x: number): number {
|
|
136
|
+
let index = 0;
|
|
137
|
+
columns.forEach((edge, column) => {
|
|
138
|
+
if (edge <= x) index = column;
|
|
139
|
+
});
|
|
140
|
+
return index;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** This function makes a cell from its words, in their order from left to right. */
|
|
144
|
+
function makeCell(words: readonly OcrWord[]): OcrTableCell {
|
|
145
|
+
return {
|
|
146
|
+
text: words.map((word) => word.text).join(" "),
|
|
147
|
+
confidence: words.reduce((sum, word) => sum + word.confidence, 0) / words.length,
|
|
148
|
+
box: unionBox(words.map((word) => word.box)),
|
|
149
|
+
words,
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* This function gives the rows and the columns of a table from the boxes of its words. Give the
|
|
155
|
+
* words of one table only: a title or a note above the table becomes a row too.
|
|
156
|
+
*
|
|
157
|
+
* - A row has one cell for each column. A cell is `null` when the row has no word in its column.
|
|
158
|
+
* - A column that holds few starts of cells (less than `minShare` of the rows) is not a column. Its
|
|
159
|
+
* words go to the column at their left.
|
|
160
|
+
* - A word with an empty text is not a word of the table.
|
|
161
|
+
*
|
|
162
|
+
* @param source - A text from the OCR, or its words
|
|
163
|
+
* @param options - The tolerances, as shares of the median height of the words
|
|
164
|
+
* @throws RangeError when an option is not a finite number of 0 or more
|
|
165
|
+
*/
|
|
166
|
+
export function tableOfText(source: OcrText | readonly OcrWord[], options: OcrTableOptions = {}): OcrTable {
|
|
167
|
+
const all = "lines" in source ? source.lines.flatMap((line) => line.words) : source;
|
|
168
|
+
const words = all.filter((word) => word.text.trim().length > 0);
|
|
169
|
+
const rowTolerance = option("rowTolerance", options.rowTolerance, 0.7);
|
|
170
|
+
const cellGap = option("cellGap", options.cellGap, 0.75);
|
|
171
|
+
const columnTolerance = option("columnTolerance", options.columnTolerance, 1);
|
|
172
|
+
const minShare = option("minShare", options.minShare, 0.25);
|
|
173
|
+
if (words.length === 0) return { columns: [], rows: [] };
|
|
174
|
+
const heights = words.map((word) => word.box.h).sort((a, b) => a - b);
|
|
175
|
+
const height = heights[Math.floor(heights.length / 2)] as number;
|
|
176
|
+
const rows = groupRows(words, rowTolerance * height);
|
|
177
|
+
const found = findColumns(rows, cellGap * height, columnTolerance * height, minShare);
|
|
178
|
+
// With no column that passes minShare, the table has one column, so that each word stays in a cell
|
|
179
|
+
const columns = found.length > 0 ? found : [Math.min(...words.map((word) => word.box.x))];
|
|
180
|
+
return {
|
|
181
|
+
columns,
|
|
182
|
+
rows: rows.map((row) => {
|
|
183
|
+
const cells: OcrWord[][] = columns.map(() => []);
|
|
184
|
+
for (const word of row) (cells[columnOf(columns, center(word.box))] as OcrWord[]).push(word);
|
|
185
|
+
return { box: unionBox(row.map((word) => word.box)), cells: cells.map((cell) => (cell.length === 0 ? null : makeCell(cell))) };
|
|
186
|
+
}),
|
|
187
|
+
};
|
|
188
|
+
}
|