-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathabbyy2csv.py
executable file
·404 lines (332 loc) · 14.1 KB
/
abbyy2csv.py
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#!/usr/bin/env python3
#
# Copyright (C) 2014 Elliott Sales de Andrade
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of version 3 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from __future__ import (division, print_function)
import argparse
import csv
import logging
from lxml import etree
import numpy as np
from sklearn import cluster
ABBYY_NS = 'http://www.abbyy.com/FineReader_xml/FineReader10-schema-v1.xml'
PAGE = etree.QName(ABBYY_NS, 'page').text
TEXT = etree.QName(ABBYY_NS, 'text').text
LINE = etree.QName(ABBYY_NS, 'line').text
CHAR_PARAMS = etree.QName(ABBYY_NS, 'charParams').text
class TextObject:
def __init__(self, baseline, left, top, right, bottom):
self.baseline = baseline
self.left = left
self.top = top
self.right = right
self.bottom = bottom
self.text = ''
class Processor:
def __init__(self, input, output, verbose,
row_algorithm, row_params, col_algorithm, col_params):
self.input = input
self.output = output
self.logger = logging.getLogger(__name__)
if verbose:
self.logger.setLevel(logging.DEBUG)
else:
self.logger.setLevel(logging.WARNING)
self.row_algorithm = row_algorithm
self.row_params = row_params
self.col_algorithm = col_algorithm
self.col_params = col_params
self.pages = 0
self.total_lines = 0
def run(self):
self.logger.debug('Reading file %s ...' % (self.input.name, ))
content = etree.parse(self.input)
if self.output:
self.writer = csv.writer(self.output)
for elem in content.iter(PAGE):
self.processPage(elem)
self.logger.info('Processed %d pages ...' % (self.pages, ))
self.logger.info('Processed %d lines ...' % (self.total_lines, ))
def processResults(self, lines):
'''
Process results and place them in CSV.
Override this method to do other things with the results.
'''
for row in sorted(lines):
textrow = row[:5] + [obj.text if obj else None for obj in row[5:]]
self.writer.writerow(textrow)
def analyzeCoverPage(self, objs):
'''
Analyze a portrait page, which is probably a cover.
'''
self.logger.debug('Processing cover page ...')
lines = [[self.pages + 1, None, None, None, None] + objs]
return lines
def analyzePage(self, objs):
'''
Analyze a normal page and produce lines of cells`.
'''
self.logger.debug('Processing normal page ...')
rows, num_rows, fuzzy_rows = self.getSortedRowClusters(objs)
cols, num_cols, fuzzy_cols = self.getSortedColumnClusters(objs)
leftover_objs = set(objs)
self.logger.debug(' Unique rows & columns: %d %d' % (
num_rows, num_cols))
if fuzzy_rows:
self.logger.warning(' Row results fuzzy; '
'check nothing is missing.')
if fuzzy_cols:
self.logger.warning(' Column results fuzzy; '
'check nothing is missing.')
lines = []
for index in rows:
line_objs = [x for j, x in enumerate(objs) if j in index]
line_cols = np.take(cols, index)
left = min(x.left for x in line_objs)
top = min(x.top for x in line_objs)
right = max(x.right for x in line_objs)
bottom = max(x.bottom for x in line_objs)
line = [self.pages + 1, top, left, bottom, right]
for col, obj in zip(line_cols, line_objs):
if col == -1:
continue
while len(line) < col + 5:
line.append(None)
line.append(obj)
leftover_objs.remove(obj)
lines.append(line)
for obj in leftover_objs:
lines.append([self.pages + 1,
obj.top, obj.left, obj.bottom, obj.right,
obj])
return lines
def getSortedRowClusters(self, objs):
'''
Determine row clusters and their order.
Clusters that create rows are determined by the user-specified
algorithm. They are then sorted by location, and lists of indices for
each cluster are returned in order.
'''
if self.row_algorithm == 'affinity':
algorithm = cluster.AffinityPropagation(**self.row_params)
elif self.row_algorithm == 'DBSCAN':
algorithm = cluster.DBSCAN(**self.row_params)
elif self.row_algorithm == 'MeanShift':
algorithm = cluster.MeanShift(**self.row_params)
Y = np.array([[y.baseline] for y in objs], dtype=np.float64)
rows = algorithm.fit_predict(Y)
if self.row_algorithm == 'affinity':
# Here, samples are the found location, so just sort directly.
row_set = set(rows)
def ordered_clusters():
# ABBYY coordinates are bottom-to-top, so reverse list.
for i in sorted(row_set, reverse=True):
yield np.where(rows == i)[0]
return ordered_clusters(), len(row_set), False
elif self.row_algorithm == 'DBSCAN':
# Here, samples are labelled, so go back and find the original
# locations.
fuzzy = -1 in rows
num_clusters = len(set(rows)) - (1 if fuzzy else 0)
clusters = []
cluster_centres = np.empty(num_clusters)
for i in range(num_clusters):
index = np.where(rows == i)
clusters.append(index[0])
cluster_centres[i] = np.mean(np.take(Y, index))
ordered_clusters = (clust for centre, clust in
sorted(zip(cluster_centres, clusters)))
return ordered_clusters, num_clusters, fuzzy
elif self.row_algorithm == 'MeanShift':
# Here, samples are labelled, but cluster locations are provided.
fuzzy = -1 in rows
num_clusters = len(set(rows)) - (1 if fuzzy else 0)
clusters = []
for i in range(num_clusters):
index = np.where(rows == i)
clusters.append(index[0])
ordered_clusters = (clust for centre, clust in
sorted(zip(algorithm.cluster_centers_,
clusters)))
return ordered_clusters, num_clusters, fuzzy
def getSortedColumnClusters(self, objs):
'''
Determine column clusters and their order.
Clusters that create columns are determined by the user-specified
algorithm. They are then sorted by location, and the indices are
returned.
'''
if self.col_algorithm == 'affinity':
algorithm = cluster.AffinityPropagation(**self.col_params)
elif self.col_algorithm == 'DBSCAN':
algorithm = cluster.DBSCAN(**self.col_params)
elif self.col_algorithm == 'MeanShift':
algorithm = cluster.MeanShift(**self.col_params)
X = np.array([[x.left] for x in objs], dtype=np.float64)
cols = algorithm.fit_predict(X)
if self.col_algorithm == 'affinity':
# Here, samples are the found location, so just sort directly.
sorted_locations = sorted(set(cols))
num_clusters = len(sorted_locations)
sorted_col_indices = np.empty(len(objs))
for i, loc in enumerate(sorted_locations):
index = np.where(cols == loc)[0]
sorted_col_indices[index] = i
fuzzy = False
elif self.col_algorithm == 'DBSCAN':
# Here, samples are labelled, so go back and find the original
# locations.
fuzzy = -1 in cols
num_clusters = len(set(cols)) - (1 if fuzzy else 0)
cluster_centres = np.empty(num_clusters)
clusters = []
for i in range(num_clusters):
index = np.where(cols == i)
clusters.append(index[0])
cluster_centres[i] = np.mean(np.take(X, index))
indices = np.argsort(cluster_centres)
sorted_col_indices = -np.ones(len(objs))
for i, j in enumerate(indices):
index = clusters[j]
sorted_col_indices[index] = i
elif self.col_algorithm == 'MeanShift':
# Here, samples are labelled, but cluster locations are provided.
fuzzy = -1 in cols
num_clusters = len(set(cols)) - (1 if fuzzy else 0)
clusters = []
for i in range(num_clusters):
index = np.where(cols == i)[0]
clusters.append(index)
indices = np.argsort(algorithm.cluster_centers_)
sorted_col_indices = -np.ones(len(objs))
for i, j in enumerate(indices):
index = clusters[j]
sorted_col_indices[index] = i
return sorted_col_indices, num_clusters, fuzzy
def processPage(self, page):
'''
Process a page and output results to CSV file.
'''
self.width = int(page.get('width'))
self.height = int(page.get('height'))
self.resolution = int(page.get('resolution'))
page_objs = []
for elem in page.iter(TEXT):
text_objs = self.processText(elem)
if text_objs:
page_objs += text_objs
if self.height > self.width:
# Portrait page, probably cover
lines = self.analyzeCoverPage(page_objs)
else:
lines = self.analyzePage(page_objs)
if lines:
self.processResults(lines)
self.logger.info(' Max columns: %d' % (max(len(x) for x in lines)))
self.logger.info(' New rows: %d' % (len(lines)))
else:
self.logger.info(' Max columns: 0')
self.logger.info(' New rows: 0')
self.pages += 1
self.total_lines += len(lines)
def processText(self, text):
'''
Process a text block.
'''
orientation = text.get('orientation')
mirrored = text.get('mirrored') == 'true'
inverted = text.get('inverted') == 'true'
if mirrored or inverted:
return
if orientation is not None and orientation != 'Normal':
return
text_objs = []
for elem in text.iter(LINE):
obj = self.processLine(elem)
text_objs.append(obj)
return text_objs
def processLine(self, line):
'''
Process a line of text.
'''
baseline = int(line.get('baseline'))
left = int(line.get('l'))
top = int(line.get('t'))
right = int(line.get('r'))
bottom = int(line.get('b'))
obj = TextObject(baseline, left, top, right, bottom)
for elem in line.iter(CHAR_PARAMS):
obj.text += elem.text
return obj
class Main:
def __init__(self):
parser = argparse.ArgumentParser(
description='Convert ABBYY XML files to CSV.')
parser.add_argument('input', type=argparse.FileType('rb'),
help='Input XML file')
parser.add_argument('output', type=argparse.FileType('w'),
help='Output CSV file')
parser.add_argument('-v', '--verbose', action='store_true',
help='Be verbose.')
parser.add_argument('--row-algorithm', '-r', default='affinity',
choices=['affinity', 'DBSCAN', 'MeanShift'],
help='Algorithm to use for row clustering.')
parser.add_argument('--col-algorithm', '-c', default='affinity',
choices=['affinity', 'DBSCAN', 'MeanShift'],
help='Algorithm to use for column clustering.')
parser.add_argument('--row-params', '-rp',
help='Parameters to use in row algorithm.')
parser.add_argument('--col-params', '-cp',
help='Parameters to use in column algorithm.')
args = parser.parse_args()
logging.basicConfig(level=logging.DEBUG if args.verbose else None)
row_algorithm = args.row_algorithm
row_params = self.parseAlgParams('row',
args.row_algorithm,
args.row_params)
col_algorithm = args.col_algorithm
col_params = self.parseAlgParams('column',
args.col_algorithm,
args.col_params)
self.processor = Processor(args.input, args.output, args.verbose,
row_algorithm, row_params,
col_algorithm, col_params)
def parseAlgParams(self, kind, algorithm, arg_params):
'''
Parse user-specified parameters for a clustering algorithm.
'''
params = {}
if arg_params:
for p in arg_params.split(','):
key, val = p.split('=')
try:
val = int(val)
except ValueError:
val = float(val)
except ValueError:
pass
params[key] = val
msg = 'Using %s algorithm for %ss with ' % (algorithm, kind)
if params:
msg += ','.join('%s=%s' % (key, params[key]) for key in params)
else:
msg += 'default parameters.'
logging.info(msg)
return params
def run(self):
self.processor.run()
if __name__ == '__main__':
m = Main()
m.run()