diff --git a/src/index.ts b/src/index.ts index 7bb1cec6b..a8731bd9c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,8 +10,10 @@ export { Page, } from "./parsing/common"; export { + InferenceFile, InferenceResponse, JobResponse, + RawText, } from "./parsing/v2"; export { InputSource, diff --git a/src/parsing/v2/inferenceResult.ts b/src/parsing/v2/inferenceResult.ts index 92900f536..26a2528eb 100644 --- a/src/parsing/v2/inferenceResult.ts +++ b/src/parsing/v2/inferenceResult.ts @@ -1,12 +1,14 @@ import { InferenceFields } from "./field/inferenceFields"; import { InferenceResultOptions } from "./inferenceResultOptions"; import { StringDict } from "../common"; +import { RawText } from "./rawText"; export class InferenceResult { /** * Fields contained in the inference. */ public fields: InferenceFields; + public rawText?: RawText; /** * Potential options retrieved alongside the inference. @@ -15,8 +17,8 @@ export class InferenceResult { constructor(serverResponse: StringDict) { this.fields = new InferenceFields(serverResponse["fields"]); - if (serverResponse["options"]) { - this.options = new InferenceResultOptions(serverResponse["options"]); + if (serverResponse["raw_text"]) { + this.rawText = new RawText(serverResponse["raw_text"]); } } diff --git a/src/parsing/v2/rawText.ts b/src/parsing/v2/rawText.ts index 0a5c73932..a804834fc 100644 --- a/src/parsing/v2/rawText.ts +++ b/src/parsing/v2/rawText.ts @@ -1,21 +1,12 @@ import { StringDict } from "../common"; - +import { RawTextPage } from "./rawTextPage"; export class RawText { - /** - * The page number the text was found on. - */ - public page: number; - /** - * The text content found on the page. - */ - public content: string; + pages: Array; - /** - * @param serverResponse JSON response from the server. - */ constructor(serverResponse: StringDict) { - this.page = serverResponse["page"]; - this.content = serverResponse["content"]; + this.pages = serverResponse["pages"] ? serverResponse["pages"].map( + (rawTextPage: StringDict) => new RawTextPage(rawTextPage) + ) : []; } } diff --git a/src/parsing/v2/rawTextPage.ts b/src/parsing/v2/rawTextPage.ts new file mode 100644 index 000000000..df5c3e76a --- /dev/null +++ b/src/parsing/v2/rawTextPage.ts @@ -0,0 +1,15 @@ +import { StringDict } from "../common"; + +export class RawTextPage { + /** + * The text content found on the page. + */ + public content: string; + + /** + * @param serverResponse JSON response from the server. + */ + constructor(serverResponse: StringDict) { + this.content = serverResponse["content"]; + } +} diff --git a/tests/data b/tests/data index f0175f0ee..f6eb112b6 160000 --- a/tests/data +++ b/tests/data @@ -1 +1 @@ -Subproject commit f0175f0ee644b57b409e6ad7e1c030f28fbe57ef +Subproject commit f6eb112b6b5bd95b3f591b839b6c4920e5ffe80c diff --git a/tests/parsing/v2/inference.spec.ts b/tests/parsing/v2/inference.spec.ts index c5567cc4d..039314250 100644 --- a/tests/parsing/v2/inference.spec.ts +++ b/tests/parsing/v2/inference.spec.ts @@ -1,6 +1,6 @@ import { expect } from "chai"; import path from "node:path"; -import { LocalResponse, InferenceResponse } from "../../../src"; +import { LocalResponse, InferenceResponse, RawText } from "../../../src"; import { FieldConfidence, ListField, ObjectField, SimpleField } from "../../../src/parsing/v2/field"; import { promises as fs } from "node:fs"; import { Polygon } from "../../../src/geometry"; @@ -236,22 +236,19 @@ describe("inference", async () => { }); }); - describe("options", async () => { - it("raw texts should be exposed", async () => { + describe("raw text", async () => { + it("raw text should be exposed", async () => { const response = await loadV2Inference(rawTextPath); - const opts = response.inference.result.options; + const rawText = response.inference.result.rawText; - expect(opts).to.not.be.undefined; - const rawTexts = - (opts as any).rawTexts ?? (opts as any).getRawTexts?.() ?? []; + expect(rawText).to.be.instanceOf(RawText); - expect(rawTexts).to.be.an("array").and.have.lengthOf(2); + const pages = rawText?.pages; + if (pages === undefined) throw new Error("pages is undefined"); - const first = rawTexts[0]; - expect(first.page).to.eq(0); - expect(first.content).to.eq( - "This is the raw text of the first page..." - ); + expect(pages).to.be.an("array").and.have.lengthOf(2); + const first = pages[0]; + expect(first.content).to.eq("This is the raw text of the first page..."); }); });