Skip to content

Commit 49b931b

Browse files
♻️ remove unused DynamicField & put location data into BaseField (#374)
1 parent e8bdc71 commit 49b931b

File tree

8 files changed

+53
-28
lines changed

8 files changed

+53
-28
lines changed

src/input/localResponse.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export class LocalResponse {
9797
try {
9898
return new responseClass(this.asDict());
9999
} catch {
100-
throw new MindeeError("Invalid class specified for deserialization.");
100+
throw new MindeeError("Invalid response provided.");
101101
}
102102
}
103103
}

src/parsing/v2/field/baseField.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
import { FieldConfidence } from "./fieldConfidence";
22
import { StringDict } from "../../common";
3+
import { FieldLocation } from "./fieldLocation";
34

45
export abstract class BaseField {
56
protected _indentLevel: number;
67
public confidence: FieldConfidence | undefined;
8+
public locations: Array<FieldLocation> | undefined;
79

810
protected constructor(rawResponse: StringDict, indentLevel = 0) {
911
this._indentLevel = indentLevel;
10-
if (rawResponse["confidence"] !== undefined) {
12+
if ("confidence" in rawResponse && rawResponse["confidence"] !== null) {
1113
this.confidence = rawResponse["confidence"] as FieldConfidence;
1214
}
15+
if ("locations" in rawResponse && rawResponse["locations"]) {
16+
this.locations = rawResponse["locations"].map((location: StringDict | undefined) => {
17+
return location ? new FieldLocation(location) : "";
18+
});
19+
}
1320
}
1421
}

src/parsing/v2/field/dynamicField.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/parsing/v2/field/fieldLocation.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ export class FieldLocation {
1212
readonly page: number | undefined;
1313

1414
constructor(serverResponse: StringDict) {
15-
this.polygon = new Polygon(serverResponse["polygon"]);
16-
this.page = "number" in serverResponse &&
17-
typeof serverResponse["page"] === "number" ? serverResponse["page"] : undefined;
15+
console.log("paj???", serverResponse["page"]);
16+
this.polygon = serverResponse["polygon"] as Polygon;
17+
this.page = "page" in serverResponse ? serverResponse["page"] : undefined;
1818
}
1919

2020
toString(): string {

src/parsing/v2/field/objectField.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { InferenceFields } from "./inferenceFields";
22
import { StringDict } from "../../common";
3-
import { DynamicField } from "./dynamicField";
3+
import { BaseField } from "./baseField";
44

5-
export class ObjectField extends DynamicField {
5+
export class ObjectField extends BaseField {
66
readonly fields: InferenceFields;
77

88
constructor(serverResponse: StringDict, indentLevel = 0) {

src/parsing/v2/field/simpleField.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { StringDict } from "../../common";
2-
import { DynamicField } from "./dynamicField";
2+
import { BaseField } from "./baseField";
33

4-
export class SimpleField extends DynamicField {
4+
export class SimpleField extends BaseField {
55
readonly value: string | number | boolean | null;
66

77
constructor(serverResponse: StringDict, indentLevel = 0) {

tests/data

tests/parsing/v2/inference.spec.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import { expect } from "chai";
22
import path from "node:path";
33
import { InferenceResponse } from "../../../src/parsing/v2";
44
import { LocalResponse } from "../../../src";
5-
import { ListField, ObjectField, SimpleField } from "../../../src/parsing/v2/field";
5+
import { FieldConfidence, ListField, ObjectField, SimpleField } from "../../../src/parsing/v2/field";
66
import { promises as fs } from "node:fs";
7+
import { Polygon } from "../../../src/geometry";
78

89
const resourcesPath = path.join(__dirname, "..", "..", "data");
910
const v2DataDir = path.join(resourcesPath, "v2");
@@ -12,6 +13,7 @@ const inferencePath = path.join(v2DataDir, "inference");
1213
const deepNestedFieldPath = path.join(inferencePath, "deep_nested_fields.json");
1314
const standardFieldPath = path.join(inferencePath, "standard_field_types.json");
1415
const standardFieldRstPath = path.join(inferencePath, "standard_field_types.rst");
16+
const locationFieldPath = path.join(findocPath, "complete_with_coordinates.json");
1517
const rawTextPath = path.join(inferencePath, "raw_texts.json");
1618
const blankPath = path.join(findocPath, "blank.json");
1719
const completePath = path.join(findocPath, "complete.json");
@@ -208,4 +210,37 @@ describe("inference", async () => {
208210
expect(response.inference.toString()).to.be.eq(rstString);
209211
}).timeout(10000);
210212
});
213+
214+
describe("field locations and confidence", async () => {
215+
it("to be properly exposed", async () => {
216+
const response = await loadV2Inference(locationFieldPath);
217+
218+
expect(response.inference).to.not.be.null;
219+
220+
const dateField = response.inference.result.fields.get("date") as SimpleField;
221+
expect(dateField.locations).to.exist;
222+
expect(dateField.locations![0]).to.exist;
223+
expect(dateField.locations![0].page).to.equal(0);
224+
225+
const polygon: Polygon = dateField.locations![0].polygon!;
226+
227+
expect(polygon[0].length).to.equal(2);
228+
229+
expect(polygon[0][0]).to.equal(0.948979073166918);
230+
expect(polygon[0][1]).to.equal(0.23097924535067715);
231+
232+
expect(polygon[1][0]).to.equal(0.85422);
233+
expect(polygon[1][1]).to.equal(0.230072);
234+
235+
expect(polygon[2][0]).to.equal(0.8540899268330819);
236+
expect(polygon[2][1]).to.equal(0.24365775464932288);
237+
238+
expect(polygon[3][0]).to.equal(0.948849);
239+
expect(polygon[3][1]).to.equal(0.244565);
240+
241+
expect(dateField.confidence).to.equal(FieldConfidence.medium);
242+
expect(String(dateField.confidence)).to.equal("Medium");
243+
244+
}).timeout(10000);
245+
});
211246
});

0 commit comments

Comments
 (0)