diff --git a/src/v1/parsing/common/ocrPage.ts b/src/v1/parsing/common/ocrPage.ts index fc782cba9..c81ab3dfc 100644 --- a/src/v1/parsing/common/ocrPage.ts +++ b/src/v1/parsing/common/ocrPage.ts @@ -1,7 +1,6 @@ import { compareOnX, compareOnY, - getCentroid, isPointInPolygonY, } from "@/geometry/index.js"; import { Word } from "@/v1/parsing/standard/index.js"; @@ -16,8 +15,8 @@ export class OcrPage { constructor(rawPrediction: StringDict) { const allWords: Word[] = []; if (rawPrediction["all_words"]) { - rawPrediction["all_words"].forEach((word: Word) => { - allWords.push(word); + rawPrediction["all_words"].forEach((word: StringDict) => { + allWords.push(new Word(word)); }); } this.allWords = allWords.sort((word1, word2) => @@ -27,11 +26,11 @@ export class OcrPage { #areWordsOnSameLine(currentWord: Word, nextWord: Word) { const currentInNext = isPointInPolygonY( - getCentroid(currentWord.polygon), + currentWord.polygon.getCentroid(), nextWord.polygon ); const nextInCurrent = isPointInPolygonY( - getCentroid(nextWord.polygon), + nextWord.polygon.getCentroid(), currentWord.polygon ); return nextInCurrent || currentInNext; diff --git a/src/v1/parsing/standard/index.ts b/src/v1/parsing/standard/index.ts index 19a4b41a9..ad09b1e2e 100644 --- a/src/v1/parsing/standard/index.ts +++ b/src/v1/parsing/standard/index.ts @@ -12,4 +12,4 @@ export { Taxes, TaxField } from "./tax.js"; export { StringField } from "./text.js"; export { PaymentDetailsField } from "./paymentDetails.js"; export { PositionField } from "./position.js"; -export type { Word } from "./word.js"; +export { Word } from "./word.js"; diff --git a/src/v1/parsing/standard/word.ts b/src/v1/parsing/standard/word.ts index cb093b0ae..6babe8b92 100644 --- a/src/v1/parsing/standard/word.ts +++ b/src/v1/parsing/standard/word.ts @@ -1,11 +1,18 @@ -import * as geometry from "@/geometry/index.js"; +import { Polygon } from "@/geometry/index.js"; +import { StringDict } from "@/parsing/index.js"; -export type Word = { +export class Word { /** * Contains the relative vertices coordinates (points) of a polygon containing * the field in the document. */ - polygon: geometry.Polygon; + polygon: Polygon; text: string; confidence: number; + + constructor(rawPrediction: StringDict) { + this.polygon = new Polygon(...rawPrediction["polygon"]); + this.text = rawPrediction["text"]; + this.confidence = rawPrediction["confidence"]; + } };