Skip to content

Commit 83a39cd

Browse files
committed
♻️ v1 ocr word should be a class
1 parent 49b669f commit 83a39cd

3 files changed

Lines changed: 15 additions & 9 deletions

File tree

src/v1/parsing/common/ocrPage.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {
22
compareOnX,
33
compareOnY,
4-
getCentroid,
54
isPointInPolygonY,
65
} from "@/geometry/index.js";
76
import { Word } from "@/v1/parsing/standard/index.js";
@@ -16,8 +15,8 @@ export class OcrPage {
1615
constructor(rawPrediction: StringDict) {
1716
const allWords: Word[] = [];
1817
if (rawPrediction["all_words"]) {
19-
rawPrediction["all_words"].forEach((word: Word) => {
20-
allWords.push(word);
18+
rawPrediction["all_words"].forEach((word: StringDict) => {
19+
allWords.push(new Word(word));
2120
});
2221
}
2322
this.allWords = allWords.sort((word1, word2) =>
@@ -27,11 +26,11 @@ export class OcrPage {
2726

2827
#areWordsOnSameLine(currentWord: Word, nextWord: Word) {
2928
const currentInNext = isPointInPolygonY(
30-
getCentroid(currentWord.polygon),
29+
currentWord.polygon.getCentroid(),
3130
nextWord.polygon
3231
);
3332
const nextInCurrent = isPointInPolygonY(
34-
getCentroid(nextWord.polygon),
33+
nextWord.polygon.getCentroid(),
3534
currentWord.polygon
3635
);
3736
return nextInCurrent || currentInNext;

src/v1/parsing/standard/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ export { Taxes, TaxField } from "./tax.js";
1212
export { StringField } from "./text.js";
1313
export { PaymentDetailsField } from "./paymentDetails.js";
1414
export { PositionField } from "./position.js";
15-
export type { Word } from "./word.js";
15+
export { Word } from "./word.js";

src/v1/parsing/standard/word.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1-
import * as geometry from "@/geometry/index.js";
1+
import { Polygon } from "@/geometry/index.js";
2+
import { StringDict } from "@/parsing/index.js";
23

3-
export type Word = {
4+
export class Word {
45
/**
56
* Contains the relative vertices coordinates (points) of a polygon containing
67
* the field in the document.
78
*/
8-
polygon: geometry.Polygon;
9+
polygon: Polygon;
910
text: string;
1011
confidence: number;
12+
13+
constructor(rawPrediction: StringDict) {
14+
this.polygon = new Polygon(...rawPrediction["polygon"]);
15+
this.text = rawPrediction["text"];
16+
this.confidence = rawPrediction["confidence"];
17+
}
1118
};

0 commit comments

Comments
 (0)