-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
executable file
·369 lines (303 loc) · 10.2 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/usr/bin/env node
const R = require('ramda')
const cheerio = require('cheerio')
const stats = require('simple-statistics')
const rbush = require('rbush')
const knn = require('rbush-knn')
const defaultConfig = require('./default-config.json')
const transforms = {
intArray: (array) => array.map((str) => parseInt(str)),
stripQuotes: (str) => str.replace(/^"|"$/g, ''),
parseInt: (str) => parseInt(str),
parseFloat: (str) => parseFloat(str)
}
const transformProperties = {
bbox: transforms.intArray,
baseline: transforms.intArray,
ppageno: transforms.parseInt,
image: transforms.stripQuotes,
file: transforms.stripQuotes,
x_size: transforms.parseFloat,
x_descenders: transforms.parseFloat,
x_ascenders: transforms.parseFloat
}
function titleToProperties (title) {
return R.fromPairs(title
.split(';')
.map(R.trim())
.map(R.split(' '))
.map((array) => array.length > 2 ? [array[0], array.slice(1)] : array)
.map((kv) => transformProperties[kv[0]] ? [kv[0], transformProperties[kv[0]](kv[1])] : kv)
)
}
function mergeConfig (config) {
return Object.assign({}, defaultConfig, config)
}
function readHocr (hocr) {
const $ = cheerio.load(hocr)
return $('.ocr_page').map((index, page) => {
const lines = $('.ocr_line', page).map((index, line) => {
const properties = titleToProperties($(line).attr('title'))
const text = $(line).text().trim()
if (text.length) {
return {
properties,
text
}
}
}).get()
return {
number: index,
properties: titleToProperties($(page).attr('title')),
lines
}
}).get()
}
function detectColumns (config, page) {
const xs = page.lines.map((line) => line.properties.bbox[0])
// One indent per column, and four clusters for other stuff like page numbers and headings
const columnCount = config.columnCount
const clusterCount = columnCount * 2 + 4
if (xs.length < columnCount * config.minLinesPerColumn || clusterCount >= xs.length) {
return page
}
// Find clusters of x coordinates
const clusters = stats
.ckmeans(xs, clusterCount)
.sort((a, b) => b.length - a.length)
const columns = clusters
.slice(0, columnCount)
.map(stats.mode)
const characterWidth = config.characterWidth
const lines = page.lines.map((line) => {
const lineX = line.properties.bbox[0]
const inColumn = (columnX) => lineX >= columnX - characterWidth &&
lineX <= columnX + characterWidth
let columnIndex
columns.some((columnX, index) => {
if (inColumn(columnX)) {
columnIndex = index
return true
}
})
return Object.assign(line, {
columnIndex
})
})
return Object.assign(page, {
lines,
columns
})
}
function computeLinesPerColumn (config, page) {
const columnLines = page.lines.filter((line) => line.columnIndex !== undefined)
const linesPerColumn = R.toPairs(R.countBy(R.prop('columnIndex'), columnLines))
.map((pair) => ({
columnIndex: parseInt(pair[0]),
count: pair[1]
}))
.sort((a, b) => a.columnIndex - b.columnIndex)
.map((column) => column.count)
// TODO: maybe use linesPerColumn.some? It happens that the page has two columns,
// but the last column only contains a few lines
// Or: require every column except the last to have at least minLinesPerColumn lines?
return Object.assign(page, {
linesPerColumn,
minLinesPerColumn: linesPerColumn.length === config.columnCount &&
linesPerColumn.every((count) => count > config.minLinesPerColumn)
})
}
function indexLinePositions (page) {
const tree = rbush(page.lines.length)
tree.load(page.lines.map((line, index) => ({
minX: line.properties.bbox[0],
minY: line.properties.bbox[1],
maxX: line.properties.bbox[0],
maxY: line.properties.bbox[1],
index
})))
return tree
}
function connectIndentedLines (page) {
if (!page.minLinesPerColumn) {
return page
}
const tree = indexLinePositions(page)
page.lines.forEach((line, lineIndex) => {
const lineX = line.properties.bbox[0]
const lineY = line.properties.bbox[1]
if (line.columnIndex === undefined) {
const neighbors = knn(tree, lineX, lineY, 1, (item) => {
const knnLine = page.lines[item.index]
const knnLineX = knnLine.properties.bbox[0]
const knnLineY = knnLine.properties.bbox[1]
return lineIndex !== item.index && knnLine.columnIndex !== undefined &&
knnLineX <= lineX && knnLineY <= lineY
})
if (neighbors.length) {
const previousLine = page.lines[neighbors[0].index]
// TODO: use map in loop, make immutable
previousLine.nextLineIndex = lineIndex
line.previousLineIndex = neighbors[0].index
}
}
})
return page
}
function constructCompleteLines (page) {
const lines = page.lines.map((line, lineIndex) => {
if (line.nextLineIndex !== undefined && line.previousLineIndex === undefined) {
let completeText = ''
let thisLine = line
while (thisLine) {
if (completeText.endsWith('-')) {
completeText = completeText.substring(0, completeText.length - 1) + thisLine.text
} else {
completeText += ` ${thisLine.text}`
}
thisLine = page.lines[thisLine.nextLineIndex]
}
return Object.assign(line, {
completeText: completeText.trim()
})
} else if (line.nextLineIndex === undefined && line.previousLineIndex === undefined && line.columnIndex !== undefined) {
return Object.assign(line, {
completeText: line.text
})
} else {
return line
}
})
return Object.assign(page, {
lines
})
}
function addBoundingBox (page) {
if (!page.properties || !page.properties.bbox) {
const maxCoordinate = (index) => page.lines
.reduce((acc, line) => {
let coordinate = 0
if (line.properties && line.properties.bbox) {
coordinate = line.properties.bbox[index]
}
return Math.max(acc, coordinate)
}, 0)
const xMax = maxCoordinate(2)
const yMax = maxCoordinate(3)
const bbox = [0, 0, xMax, yMax]
page.properties = Object.assign({}, page.properties, {
bbox
})
}
return page
}
function detectColumnsAndIndentation (hocr, config) {
config = mergeConfig(config)
return readHocr(hocr)
.map(R.curry(detectColumns)(config))
.map(R.curry(computeLinesPerColumn)(config))
.map(connectIndentedLines)
.map(constructCompleteLines)
.map(addBoundingBox)
}
if (require.main === module) {
const chalk = require('chalk')
const fs = require('fs')
const path = require('path')
const H = require('highland')
const minimist = require('minimist')
const argv = minimist(process.argv.slice(2), {
alias: {
m: 'mode'
},
default: {
mode: 'log'
}
})
const modes = [
'log',
'json',
'ndjson',
'html'
]
if (!argv._ || argv._.length !== 1 || !modes.includes(argv.mode)) {
const help = [
'usage: detect-columns <options> /path/to/file.hocr',
'',
'Options:',
' -m, --mode Choose between text logging, JSON or NDJSON — default is logging',
' -c, --config Path to configuration file (see default-config.json for example)',
' -o, --output File to write JSON/NDJSON output to — default is stdout',
'',
'Possible modes:',
' log Logs output to stdout with very nice colors',
' json Outputs a JSON file',
' ndjson Outputs NDJSON file (less data than JSON, easier to parse)',
' html Outputs HTML visualization'
]
console.log(help.join('\n'))
process.exit()
}
const hocr = fs.readFileSync(argv._[0], 'utf8')
let config = {}
if (argv.config) {
config = JSON.parse(fs.readFileSync(argv.config, 'utf8'))
}
const output = argv.output ? fs.createWriteStream(argv.output, 'utf8') : process.stdout
const pages = detectColumnsAndIndentation(hocr, config)
const data = {
config: mergeConfig(config),
pages
}
if (argv.mode === 'log') {
const logPage = (page) => {
console.log(`Page: ${page.number}`)
const properties = R.toPairs(page.properties).map((pair) => ` ${pair[0]}: ${pair[1]}`).join('\n')
console.log(chalk.gray(properties))
console.log(' X coordinates of columns:', page.columns ? page.columns.join(', ') : 'no columns found')
console.log(`Lines: ${chalk.green('text')} ${chalk.yellow('X coordinate')} ${chalk.blue('column')}`)
page.lines.forEach((line) => {
const lineX = line.properties.bbox[0]
if (line.columnIndex !== undefined) {
console.log(chalk.green(line.text), chalk.yellow(lineX), chalk.blue(line.columnIndex + 1))
if (line.nextLineIndex) {
const nextLine = page.lines[line.nextLineIndex]
const nextLineX = nextLine.properties.bbox[0]
console.log(chalk.cyan('↪ '), chalk.green(nextLine.text), chalk.yellow(nextLineX))
}
if (line.completeText && line.nextLineIndex) {
console.log(chalk.cyan('= '), chalk.gray(line.completeText))
}
} else if (line.columnIndex === undefined && line.previousLineIndex === undefined) {
console.log(chalk.red(line.text), chalk.yellow(lineX))
}
})
}
pages.forEach(logPage)
} else if (argv.mode === 'json') {
output.write(JSON.stringify(data, null, 2) + '\n')
} else if (argv.mode === 'ndjson') {
H(pages)
.map((page) => page.lines
.filter((line) => line.columnIndex !== undefined || line.previousLineIndex === undefined)
.map((line) => ({
pageNum: page.number,
bbox: line.properties.bbox,
text: line.completeText || line.text,
columnIndex: line.columnIndex
})))
.flatten()
.map(JSON.stringify)
.intersperse('\n')
.append('\n')
.pipe(output)
} else if (argv.mode === 'html') {
const doT = require('dot')
const template = fs.readFileSync(path.join(__dirname, 'visualization.template.html'), 'utf8')
doT.templateSettings.strip = false
const compiledTemplate = doT.template(template)
const html = compiledTemplate(data)
output.write(html + '\n')
}
}
module.exports = detectColumnsAndIndentation