forked from muhimasri/aws-textract-helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
145 lines (137 loc) · 3.51 KB
/
index.js
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
exports.createForm = (data, config) => {
const {
keyMap,
valueMap,
blockMap
} = getKeyValueMap(data.Blocks)
// Get Key Value relationship
const trimChars = config ? config.trimChars : undefined
return getKeyValueRelationship(keyMap, valueMap, blockMap, trimChars)
}
exports.createTables = (data) => {
const blocks = data.Blocks
const blocksMap = {}
const tableBlocks = []
for (const block of blocks) {
blocksMap[block.Id] = block
if (block.BlockType === 'TABLE') {
tableBlocks.push(block)
}
}
const tableSets = []
for (const table of tableBlocks) {
tableSets.push(getRowsColumnsMap(table, blocksMap))
}
return tableSets
}
function getKeyValueMap (blocks) {
// get key and value maps
const keyMap = {}
const valueMap = {}
const blockMap = {}
try {
for (const block of blocks) {
const blockId = block.Id
blockMap[blockId] = block
if (block.BlockType === 'KEY_VALUE_SET') {
if (block.EntityTypes.includes('KEY')) { keyMap[blockId] = block } else { valueMap[blockId] = block }
}
}
} catch (err) {
console.log(err)
}
return {
keyMap,
valueMap,
blockMap
}
}
function getKeyValueRelationship (keyMap, valueMap, blockMap, trimChars) {
const kvs = {}
try {
for (const keyBlock in keyMap) {
const valueBlock = findValueBlock(keyMap[keyBlock], valueMap)
let key = getText(keyMap[keyBlock], blockMap)
const val = getText(valueBlock, blockMap)
if (key.indexOf('.') > -1) {
key = key.split('.').join('')
}
if (trimChars) {
for (let i = 0; i < trimChars.length; i++) {
if (trimChars[i] === ' ') {
key = key.trim()
} else {
key = key.replace(trimChars[i], '')
}
}
}
kvs[key] = val
}
} catch (err) {
console.error(err)
}
return kvs
}
function getText (result, blocksMap) {
let text = ''
try {
if (result.Relationships) {
for (const relationship of result.Relationships) {
if (relationship.Type === 'CHILD') {
for (const childId of relationship.Ids) {
const word = blocksMap[childId]
if (word.BlockType === 'WORD') {
text = text + word.Text + ' '
}
if (word.BlockType === 'SELECTION_ELEMENT') {
if (word.SelectionStatus === 'SELECTED') {
text = text + 'X '
}
}
}
}
}
}
} catch (err) {
console.error(err)
}
return text
}
function findValueBlock (keyBlock, valueMap) {
let valueBlock = null
try {
for (const relationship of keyBlock.Relationships) {
if (relationship.Type === 'VALUE') {
for (const valueId of relationship.Ids) {
valueBlock = valueMap[valueId]
}
}
}
} catch (err) {
console.error(err)
}
return valueBlock
}
function getRowsColumnsMap (tableResult, blocksMap) {
const rows = {}
try {
for (const relationship of tableResult.Relationships) {
if (relationship.Type === 'CHILD') {
for (const childId of relationship.Ids) {
const cell = blocksMap[childId]
if (cell.BlockType === 'CELL') {
const rowIndex = cell.RowIndex
const colIndex = cell.ColumnIndex
if (!rows[rowIndex]) {
rows[rowIndex] = {}
}
rows[rowIndex][colIndex] = getText(cell, blocksMap)
}
}
}
}
} catch (err) {
console.error(err)
}
return rows
}