Skip to content

Commit 9ce072c

Browse files
authored
♻️ update raw text to new format (#381)
1 parent e352409 commit 9ce072c

File tree

6 files changed

+37
-30
lines changed

6 files changed

+37
-30
lines changed

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ export {
1010
Page,
1111
} from "./parsing/common";
1212
export {
13+
InferenceFile,
1314
InferenceResponse,
1415
JobResponse,
16+
RawText,
1517
} from "./parsing/v2";
1618
export {
1719
InputSource,

src/parsing/v2/inferenceResult.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { InferenceFields } from "./field/inferenceFields";
22
import { InferenceResultOptions } from "./inferenceResultOptions";
33
import { StringDict } from "../common";
4+
import { RawText } from "./rawText";
45

56
export class InferenceResult {
67
/**
78
* Fields contained in the inference.
89
*/
910
public fields: InferenceFields;
11+
public rawText?: RawText;
1012

1113
/**
1214
* Potential options retrieved alongside the inference.
@@ -15,8 +17,8 @@ export class InferenceResult {
1517

1618
constructor(serverResponse: StringDict) {
1719
this.fields = new InferenceFields(serverResponse["fields"]);
18-
if (serverResponse["options"]) {
19-
this.options = new InferenceResultOptions(serverResponse["options"]);
20+
if (serverResponse["raw_text"]) {
21+
this.rawText = new RawText(serverResponse["raw_text"]);
2022
}
2123
}
2224

src/parsing/v2/rawText.ts

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,12 @@
11
import { StringDict } from "../common";
2-
2+
import { RawTextPage } from "./rawTextPage";
33

44
export class RawText {
5-
/**
6-
* The page number the text was found on.
7-
*/
8-
public page: number;
9-
/**
10-
* The text content found on the page.
11-
*/
12-
public content: string;
5+
pages: Array<RawTextPage>;
136

14-
/**
15-
* @param serverResponse JSON response from the server.
16-
*/
177
constructor(serverResponse: StringDict) {
18-
this.page = serverResponse["page"];
19-
this.content = serverResponse["content"];
8+
this.pages = serverResponse["pages"] ? serverResponse["pages"].map(
9+
(rawTextPage: StringDict) => new RawTextPage(rawTextPage)
10+
) : [];
2011
}
2112
}

src/parsing/v2/rawTextPage.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { StringDict } from "../common";
2+
3+
export class RawTextPage {
4+
/**
5+
* The text content found on the page.
6+
*/
7+
public content: string;
8+
9+
/**
10+
* @param serverResponse JSON response from the server.
11+
*/
12+
constructor(serverResponse: StringDict) {
13+
this.content = serverResponse["content"];
14+
}
15+
}

tests/parsing/v2/inference.spec.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { expect } from "chai";
22
import path from "node:path";
3-
import { LocalResponse, InferenceResponse } from "../../../src";
3+
import { LocalResponse, InferenceResponse, RawText } from "../../../src";
44
import { FieldConfidence, ListField, ObjectField, SimpleField } from "../../../src/parsing/v2/field";
55
import { promises as fs } from "node:fs";
66
import { Polygon } from "../../../src/geometry";
@@ -236,22 +236,19 @@ describe("inference", async () => {
236236
});
237237
});
238238

239-
describe("options", async () => {
240-
it("raw texts should be exposed", async () => {
239+
describe("raw text", async () => {
240+
it("raw text should be exposed", async () => {
241241
const response = await loadV2Inference(rawTextPath);
242-
const opts = response.inference.result.options;
242+
const rawText = response.inference.result.rawText;
243243

244-
expect(opts).to.not.be.undefined;
245-
const rawTexts =
246-
(opts as any).rawTexts ?? (opts as any).getRawTexts?.() ?? [];
244+
expect(rawText).to.be.instanceOf(RawText);
247245

248-
expect(rawTexts).to.be.an("array").and.have.lengthOf(2);
246+
const pages = rawText?.pages;
247+
if (pages === undefined) throw new Error("pages is undefined");
249248

250-
const first = rawTexts[0];
251-
expect(first.page).to.eq(0);
252-
expect(first.content).to.eq(
253-
"This is the raw text of the first page..."
254-
);
249+
expect(pages).to.be.an("array").and.have.lengthOf(2);
250+
const first = pages[0];
251+
expect(first.content).to.eq("This is the raw text of the first page...");
255252
});
256253
});
257254

0 commit comments

Comments
 (0)