-
Notifications
You must be signed in to change notification settings - Fork 4
/
page.ts
72 lines (68 loc) · 2.25 KB
/
page.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { CropperExtra } from "./extras/cropperExtra";
import { ExtraField, Extras } from "./extras/extras";
import { OrientationField } from "./orientation";
import { Prediction } from "./prediction";
import { StringDict } from "./stringDict";
/**
* Page prediction wrapper class. Holds the results of a parsed document's page.
* Holds a `Prediction` that's either a document-level Prediction, or inherits from one.
* @typeParam T an extension of an `Prediction`. Mandatory in order to properly create a page-level prediction.
*/
export class Page<T extends Prediction> {
/** The page's index (identifier). */
id: number;
/** The page's orientation */
orientation?: OrientationField;
/** A page-level prediction. Can either be specific to pages or identical to the document prediction. */
prediction: T;
/** Potential `Extras` fields sent back along with the prediction. */
extras?: Extras;
/**
* @param predictionType constructor signature for an inference.
* @param rawPrediction raw http response.
* @param pageId the page's index (identifier).
* @param orientation the page's orientation.
*/
constructor(
predictionType: new (rawPrediction: StringDict, pageId: number) => T,
rawPrediction: StringDict,
pageId: number,
orientation?: StringDict
) {
if (pageId !== undefined && orientation !== undefined) {
this.orientation = new OrientationField({
prediction: orientation,
pageId: pageId,
});
} else {
orientation = undefined;
}
this.id = pageId;
this.prediction = new predictionType(rawPrediction["prediction"], pageId);
if (
rawPrediction["extras"] &&
Object.keys(rawPrediction["extras"].length > 0)
) {
const extras: Record<string, ExtraField> = {};
Object.entries(rawPrediction["extras"]).forEach(
([extraKey, extraValue]: [string, any]) => {
switch (extraKey) {
case "cropper":
extras["cropper"] = new CropperExtra(extraValue as StringDict);
}
}
);
this.extras = new Extras(extras);
}
}
/**
* Default string representation.
*/
toString() {
const title = `Page ${this.id}`;
return `${title}
${"-".repeat(title.length)}
${this.prediction.toString()}
`;
}
}