@scribelabsai/amazon-trp
Version:
Amazon Textract Response Parser library for Node.
81 lines • 2.54 kB
JavaScript
import { Geometry } from '../Geometry';
import { SelectionElement } from '../SelectionElement';
import { Word } from '../Word';
export class FieldKey {
constructor(block, children, blockMap) {
this.block = block;
this.confidence = block.Confidence;
this.geometry = new Geometry(block.Geometry);
this.id = block.Id;
this.content = [];
const t = [];
children.forEach((id) => {
const b = blockMap[id];
if (b?.BlockType === 'WORD') {
const w = new Word(b);
this.content.push(w);
t.push(w.text);
}
});
this.text = t.join(' ');
}
toString() {
return this.text;
}
}
export class FieldValue {
constructor(block, children, blockMap) {
this.block = block;
this.confidence = block.Confidence;
this.geometry = new Geometry(block.Geometry);
this.id = block.Id;
this.text = '';
this.content = [];
const t = [];
children.forEach((id) => {
const b = blockMap[id];
if (b?.BlockType === 'WORD') {
const w = new Word(b);
this.content.push(w);
t.push(w.text);
}
else if (b?.BlockType === 'SELECTION_ELEMENT') {
const se = new SelectionElement(b);
this.content.push(se);
this.text = se.selectionStatus;
}
});
if (t.length > 0) {
this.text = t.join(' ');
}
}
toString() {
return this.text;
}
}
export class Field {
constructor(block, blockMap) {
block.Relationships.forEach((rs) => {
if (rs.Type === 'CHILD') {
this.key = new FieldKey(block, rs.Ids, blockMap);
}
else if (rs.Type === 'VALUE') {
rs.Ids.forEach((id) => {
const b = blockMap[id];
if (b.EntityTypes.includes('VALUE')) {
b.Relationships.forEach((rs2) => {
if (rs2.Type === 'CHILD') {
this.value = new FieldValue(b, rs2.Ids, blockMap);
}
});
}
});
}
});
}
toString() {
const kv = `Key: ${this.key}\nValue: ${this.value}`;
return `\nField\n==========\n${kv}`;
}
}
//# sourceMappingURL=Field.js.map