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: 1 addition & 1 deletion src/input/localResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export class LocalResponse {
try {
return new responseClass(this.asDict());
} catch {
throw new MindeeError("Invalid class specified for deserialization.");
throw new MindeeError("Invalid response provided.");
}
}
}
9 changes: 8 additions & 1 deletion src/parsing/v2/field/baseField.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import { FieldConfidence } from "./fieldConfidence";
import { StringDict } from "../../common";
import { FieldLocation } from "./fieldLocation";

export abstract class BaseField {
protected _indentLevel: number;
public confidence: FieldConfidence | undefined;
public locations: Array<FieldLocation> | undefined;

protected constructor(rawResponse: StringDict, indentLevel = 0) {
this._indentLevel = indentLevel;
if (rawResponse["confidence"] !== undefined) {
if ("confidence" in rawResponse && rawResponse["confidence"] !== null) {
this.confidence = rawResponse["confidence"] as FieldConfidence;
}
if ("locations" in rawResponse && rawResponse["locations"]) {
this.locations = rawResponse["locations"].map((location: StringDict | undefined) => {
return location ? new FieldLocation(location) : "";
});
}
}
}
17 changes: 0 additions & 17 deletions src/parsing/v2/field/dynamicField.ts

This file was deleted.

6 changes: 3 additions & 3 deletions src/parsing/v2/field/fieldLocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ export class FieldLocation {
readonly page: number | undefined;

constructor(serverResponse: StringDict) {
this.polygon = new Polygon(serverResponse["polygon"]);
this.page = "number" in serverResponse &&
typeof serverResponse["page"] === "number" ? serverResponse["page"] : undefined;
console.log("paj???", serverResponse["page"]);
this.polygon = serverResponse["polygon"] as Polygon;
this.page = "page" in serverResponse ? serverResponse["page"] : undefined;
}

toString(): string {
Expand Down
4 changes: 2 additions & 2 deletions src/parsing/v2/field/objectField.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { InferenceFields } from "./inferenceFields";
import { StringDict } from "../../common";
import { DynamicField } from "./dynamicField";
import { BaseField } from "./baseField";

export class ObjectField extends DynamicField {
export class ObjectField extends BaseField {
readonly fields: InferenceFields;

constructor(serverResponse: StringDict, indentLevel = 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/parsing/v2/field/simpleField.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { StringDict } from "../../common";
import { DynamicField } from "./dynamicField";
import { BaseField } from "./baseField";

export class SimpleField extends DynamicField {
export class SimpleField extends BaseField {
readonly value: string | number | boolean | null;

constructor(serverResponse: StringDict, indentLevel = 0) {
Expand Down
2 changes: 1 addition & 1 deletion tests/data
37 changes: 36 additions & 1 deletion tests/parsing/v2/inference.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { expect } from "chai";
import path from "node:path";
import { InferenceResponse } from "../../../src/parsing/v2";
import { LocalResponse } from "../../../src";
import { ListField, ObjectField, SimpleField } from "../../../src/parsing/v2/field";
import { FieldConfidence, ListField, ObjectField, SimpleField } from "../../../src/parsing/v2/field";
import { promises as fs } from "node:fs";
import { Polygon } from "../../../src/geometry";

const resourcesPath = path.join(__dirname, "..", "..", "data");
const v2DataDir = path.join(resourcesPath, "v2");
Expand All @@ -12,6 +13,7 @@ const inferencePath = path.join(v2DataDir, "inference");
const deepNestedFieldPath = path.join(inferencePath, "deep_nested_fields.json");
const standardFieldPath = path.join(inferencePath, "standard_field_types.json");
const standardFieldRstPath = path.join(inferencePath, "standard_field_types.rst");
const locationFieldPath = path.join(findocPath, "complete_with_coordinates.json");
const rawTextPath = path.join(inferencePath, "raw_texts.json");
const blankPath = path.join(findocPath, "blank.json");
const completePath = path.join(findocPath, "complete.json");
Expand Down Expand Up @@ -208,4 +210,37 @@ describe("inference", async () => {
expect(response.inference.toString()).to.be.eq(rstString);
}).timeout(10000);
});

describe("field locations and confidence", async () => {
it("to be properly exposed", async () => {
const response = await loadV2Inference(locationFieldPath);

expect(response.inference).to.not.be.null;

const dateField = response.inference.result.fields.get("date") as SimpleField;
expect(dateField.locations).to.exist;
expect(dateField.locations![0]).to.exist;
expect(dateField.locations![0].page).to.equal(0);

const polygon: Polygon = dateField.locations![0].polygon!;

expect(polygon[0].length).to.equal(2);

expect(polygon[0][0]).to.equal(0.948979073166918);
expect(polygon[0][1]).to.equal(0.23097924535067715);

expect(polygon[1][0]).to.equal(0.85422);
expect(polygon[1][1]).to.equal(0.230072);

expect(polygon[2][0]).to.equal(0.8540899268330819);
expect(polygon[2][1]).to.equal(0.24365775464932288);

expect(polygon[3][0]).to.equal(0.948849);
expect(polygon[3][1]).to.equal(0.244565);

expect(dateField.confidence).to.equal(FieldConfidence.medium);
expect(String(dateField.confidence)).to.equal("Medium");

}).timeout(10000);
});
});
Loading