diff --git a/src/input/localResponse.ts b/src/input/localResponse.ts index 8e0cad2e..4b278f8e 100644 --- a/src/input/localResponse.ts +++ b/src/input/localResponse.ts @@ -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."); } } } diff --git a/src/parsing/v2/field/baseField.ts b/src/parsing/v2/field/baseField.ts index ec4d19e6..7d1f1eca 100644 --- a/src/parsing/v2/field/baseField.ts +++ b/src/parsing/v2/field/baseField.ts @@ -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 | 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) : ""; + }); + } } } diff --git a/src/parsing/v2/field/dynamicField.ts b/src/parsing/v2/field/dynamicField.ts deleted file mode 100644 index ce74119d..00000000 --- a/src/parsing/v2/field/dynamicField.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { BaseField } from "./baseField"; -import { FieldLocation } from "./fieldLocation"; -import { StringDict } from "../../common"; - - -export class DynamicField extends BaseField { - public locations: Array | undefined; - - constructor(rawResponse: StringDict, indentLevel = 0) { - super(rawResponse, indentLevel); - if (rawResponse["locations"]) { - this.locations = rawResponse["locations"].map((location: StringDict | undefined) => { - return location ? new FieldLocation(location) : ""; - }); - } - } -} diff --git a/src/parsing/v2/field/fieldLocation.ts b/src/parsing/v2/field/fieldLocation.ts index dbaf6433..16a0fb74 100644 --- a/src/parsing/v2/field/fieldLocation.ts +++ b/src/parsing/v2/field/fieldLocation.ts @@ -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 { diff --git a/src/parsing/v2/field/objectField.ts b/src/parsing/v2/field/objectField.ts index c82d7840..83170424 100644 --- a/src/parsing/v2/field/objectField.ts +++ b/src/parsing/v2/field/objectField.ts @@ -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) { diff --git a/src/parsing/v2/field/simpleField.ts b/src/parsing/v2/field/simpleField.ts index a57f4aec..e88da47c 100644 --- a/src/parsing/v2/field/simpleField.ts +++ b/src/parsing/v2/field/simpleField.ts @@ -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) { diff --git a/tests/data b/tests/data index 632af76d..f0175f0e 160000 --- a/tests/data +++ b/tests/data @@ -1 +1 @@ -Subproject commit 632af76d6eefe551cdeb2b7fa2f325cecec1b66f +Subproject commit f0175f0ee644b57b409e6ad7e1c030f28fbe57ef diff --git a/tests/parsing/v2/inference.spec.ts b/tests/parsing/v2/inference.spec.ts index ed5754f1..56f4b34d 100644 --- a/tests/parsing/v2/inference.spec.ts +++ b/tests/parsing/v2/inference.spec.ts @@ -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"); @@ -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"); @@ -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); + }); });