Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ export {
Page,
} from "./parsing/common";
export {
InferenceFile,
InferenceResponse,
JobResponse,
RawText,
} from "./parsing/v2";
export {
InputSource,
Expand Down
6 changes: 4 additions & 2 deletions src/parsing/v2/inferenceResult.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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"]);
}
}

Expand Down
19 changes: 5 additions & 14 deletions src/parsing/v2/rawText.ts
Original file line number Diff line number Diff line change
@@ -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<RawTextPage>;

/**
* @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)
) : [];
}
}
15 changes: 15 additions & 0 deletions src/parsing/v2/rawTextPage.ts
Original file line number Diff line number Diff line change
@@ -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"];
}
}
23 changes: 10 additions & 13 deletions tests/parsing/v2/inference.spec.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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...");
});
});

Expand Down
Loading