-
Notifications
You must be signed in to change notification settings - Fork 95
/
TextractExtensions.cs
536 lines (469 loc) · 14.4 KB
/
TextractExtensions.cs
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
using System;
using System.Collections.Generic;
namespace Amazon.Textract.Model {
public class Word {
public Word(Block block, List<Block> blocks) {
this.Block = block;
this.Confidence = block.Confidence;
this.Geometry = block.Geometry;
this.Id = block.Id;
this.Text = block == null ? string.Empty : block.Text;
}
public Block Block { get; set; }
public float Confidence { get; set; }
public Geometry Geometry { get; set; }
public string Id { get; set; }
public string Text { get; set; }
public override string ToString() {
return Text;
}
}
public class TextractDocument {
private List<Block> blockMap = new List<Block>();
List<List<Block>> documentPages = new List<List<Block>>();
public TextractDocument(GetDocumentAnalysisResponse response) {
this.Pages = new List<Page>();
this.ResponsePages = new List<GetDocumentAnalysisResponse>();
this.ResponsePages.Add(response);
this.ParseDocumentPagesAndBlockMap();
this.Parse();
}
private void ParseDocumentPagesAndBlockMap() {
List<Block> documentPage = null;
this.ResponsePages.ForEach(page => {
page.Blocks.ForEach(block => {
this.blockMap.Add(block);
if(block.BlockType == "PAGE") {
if(documentPage != null) {
this.documentPages.Add(documentPage);
}
documentPage = new List<Block>();
documentPage.Add(block);
} else {
documentPage.Add(block);
}
});
});
if(documentPage != null) {
this.documentPages.Add(documentPage);
}
}
private void Parse() {
this.documentPages.ForEach(documentPage => {
var page = new Page(documentPage, this.blockMap);
this.Pages.Add(page);
});
}
public Block GetBlockById(string blockId) {
return this.blockMap.Find(x => x.Id == blockId);
}
public List<GetDocumentAnalysisResponse> ResponsePages { get; set; }
public List<Page> Pages { get; set; }
public List<List<Block>> PageBlocks {
get {
return this.documentPages;
}
}
}
public class Table {
public Table(Block block, List<Block> blocks) {
this.Block = block;
this.Confidence = block.Confidence;
this.Geometry = block.Geometry;
this.Id = block.Id;
this.Rows = new List<Row>();
var ri = 1;
var row = new Row();
var relationships = block.Relationships;
if(relationships != null && relationships.Count > 0) {
relationships.ForEach(r => {
if(r.Type == "CHILD") {
r.Ids.ForEach(id => {
var cell = new Cell(blocks.Find(b => b.Id == id), blocks);
if(cell.RowIndex > ri) {
this.Rows.Add(row);
row = new Row();
ri = cell.RowIndex;
}
row.Cells.Add(cell);
});
if(row != null && row.Cells.Count > 0)
this.Rows.Add(row);
}
});
}
}
public List<Row> Rows { get; set; }
public Block Block { get; set; }
public float Confidence { get; set; }
public Geometry Geometry { get; set; }
public string Id { get; set; }
public override string ToString() {
var result = new List<string>();
result.Add(string.Format("Table{0}===={0}", Environment.NewLine));
this.Rows.ForEach(r => {
result.Add(string.Format("Row{0}===={0}{1}{0}", Environment.NewLine, r));
});
return string.Join("", result);
}
}
public class SelectionElement {
public SelectionElement(Block block, List<Block> blocks) {
this.Confidence = block.Confidence;
this.Geometry = block.Geometry;
this.Id = block.Id;
this.SelectionStatus = block.SelectionStatus;
}
public float Confidence { get; set; }
public Geometry Geometry { get; set; }
public string Id { get; set; }
public string SelectionStatus { get; set; }
}
public class Row {
public Row() {
this.Cells = new List<Cell>();
}
public List<Cell> Cells { get; set; }
public override string ToString() {
var result = new List<string>();
this.Cells.ForEach(c => {
result.Add(string.Format("[{0}]", c));
});
return string.Join("", result);
}
}
public class Page {
public Page(List<Block> blocks, List<Block> blockMap) {
this.Blocks = blocks;
this.Text = string.Empty;
this.Lines = new List<Line>();
this.Form = new Form();
this.Tables = new List<Table>();
this.Content = new List<dynamic>();
blocks.ForEach(b => {
if(b.BlockType == "PAGE") {
this.Geometry = new NewGeometry(b.Geometry);
this.Id = b.Id;
} else if(b.BlockType == "LINE") {
var l = new Line(b, blockMap);
this.Lines.Add(l);
this.Content.Add(l);
this.Text = this.Text + l.Text + Environment.NewLine;
} else if(b.BlockType == "TABLE") {
var t = new Table(b, blockMap);
this.Tables.Add(t);
this.Content.Add(t);
} else if(b.BlockType == "KEY_VALUE_SET") {
if(b.EntityTypes.Contains("KEY")) {
var f = new Field(b, blockMap);
if(f.Key != null) {
this.Form.AddField(f);
this.Content.Add(f);
}
}
}
});
}
public List<IndexedText> GetLinesInReadingOrder() {
var lines = new List<IndexedText>();
var columns = new List<Column>();
this.Lines.ForEach(line => {
var columnFound = false;
for(var index = 0; index < columns.Count; index++) {
var column = columns[index];
var bb = line.Geometry.BoundingBox;
var bbLeft = bb.Left;
var bbRight = bb.Left + bb.Width;
var bbCentre = bb.Left + (bb.Width / 2);
var columnCentre = column.Left + (column.Right / 2);
if((bbCentre > column.Left && bbCentre < column.Right) || (columnCentre > bbLeft && columnCentre < bbRight)) {
lines.Add(new IndexedText { ColumnIndex = index, Text = line.Text });
columnFound = true;
break;
}
}
if(!columnFound) {
var bb = line.Geometry.BoundingBox;
columns.Add(new Column { Left = bb.Left, Right = bb.Left + bb.Width });
lines.Add(new IndexedText { ColumnIndex = columns.Count - 1, Text = line.Text });
}
});
lines.FindAll(line => line.ColumnIndex == 0).ForEach(line => Console.WriteLine(line));
return lines;
}
public string GetTextInReadingOrder() {
var lines = this.GetLinesInReadingOrder();
var text = string.Empty;
lines.ForEach(line => {
text = text + line.Text + "\n";
});
return text;
}
public List<Block> Blocks { get; set; }
public string Text { get; set; }
public List<Line> Lines { get; set; }
public Form Form { get; set; }
public List<Table> Tables { get; set; }
public List<dynamic> Content { get; set; }
public Geometry Geometry { get; set; }
public string Id { get; set; }
public override string ToString() {
var result = new List<string>();
result.Add(string.Format("Page{0}===={0}", Environment.NewLine));
this.Content.ForEach(c => {
result.Add(string.Format("{1}{0}", Environment.NewLine, c));
});
return string.Join("", result);
}
public class Column {
public float Left { get; set; }
public float Right { get; set; }
public override string ToString() {
return string.Format("Left: {0}, Right :{1}", this.Left, this.Right);
}
}
public class IndexedText {
public int ColumnIndex { get; set; }
public string Text { get; set; }
public override string ToString() {
return string.Format("[{0}] {1}", this.ColumnIndex, this.Text);
}
}
}
public class NewGeometry : Geometry {
public NewGeometry(Geometry geometry) : base() {
this.BoundingBox = geometry.BoundingBox;
this.Polygon = geometry.Polygon;
var bb = new NewBoundingBox(this.BoundingBox.Width, this.BoundingBox.Height, this.BoundingBox.Left, this.BoundingBox.Top);
var pgs = new List<Point>();
Polygon.ForEach(pg => pgs.Add(new Point {
X = pg.X,
Y = pg.Y
}));
BoundingBox = bb;
Polygon = pgs;
}
public override string ToString() {
return string.Format("BoundingBox: {0}{1}", BoundingBox, Environment.NewLine);
}
}
public class NewBoundingBox : BoundingBox {
public NewBoundingBox(float width, float height, float left, float top) : base() {
this.Width = width;
this.Height = height;
this.Left = left;
this.Top = top;
}
public override string ToString() {
return string.Format("width: {0}, height: {1}, left: {2}, top: {3}", Width, Height, Left, Top);
}
}
public class Line {
public Line(Block block, List<Block> blocks) {
this.Block = block;
this.Confidence = block.Confidence;
this.Geometry = block.Geometry;
this.Id = block.Id;
this.Text = block == null ? string.Empty : block.Text;
this.Words = new List<Word>();
var relationships = block.Relationships;
if(relationships != null && relationships.Count > 0) {
relationships.ForEach(r => {
if(r.Type == "CHILD") {
r.Ids.ForEach(id => {
this.Words.Add(new Word(blocks.Find(b => b.BlockType == "WORD" && b.Id == id), blocks));
});
}
});
}
}
public float Confidence { get; set; }
public Geometry Geometry { get; set; }
public string Id { get; set; }
public List<Word> Words { get; set; }
public string Text { get; set; }
public Block Block { get; set; }
public override string ToString() {
return string.Format(@"
Line{0}===={0}
{1} {0}
Words{0}----{0}
{2}{0}
----
", Environment.NewLine, this.Text, string.Join(", ", this.Words));
}
}
public class Form {
public List<Field> Fields { get; set; }
private Dictionary<string, Field> fieldMap;
public Form() {
this.Fields = new List<Field>();
this.fieldMap = new Dictionary<string, Field>();
}
public void AddField(Field field) {
this.Fields.Add(field);
this.fieldMap.Add(field.Key.ToString(), field);
}
public Field GetFieldByKey(string key) {
return this.fieldMap.GetValueOrDefault(key);
}
public List<Field> SearchFieldsByKey(string key) {
return this.Fields.FindAll(f => f.Key.ToString().ToLower().Contains(key.ToLower()));
}
public override string ToString() {
return string.Join("\n", this.Fields);
}
}
public class FieldValue {
public FieldValue(Block block, List<string> children, List<Block> blocks) {
this.Block = block;
this.Confidence = block.Confidence;
this.Geometry = block.Geometry;
this.Id = block.Id;
this.Text = string.Empty;
this.Content = new List<dynamic>();
var words = new List<string>();
if(children != null && children.Count > 0) {
children.ForEach(c => {
var wordBlock = blocks.Find(b => b.Id == c);
if(wordBlock.BlockType == "WORD") {
var w = new Word(wordBlock, blocks);
this.Content.Add(w);
words.Add(w.Text);
} else if(wordBlock.BlockType == "SELECTION_ELEMENT") {
var selection = new SelectionElement(wordBlock, blocks);
this.Content.Add(selection);
words.Add(selection.SelectionStatus);
}
});
}
if(words.Count > 0) {
this.Text = string.Join(" ", words);
}
}
public List<dynamic> Content { get; set; }
public Block Block { get; set; }
public float Confidence { get; set; }
public Geometry Geometry { get; set; }
public string Id { get; set; }
public string Text { get; set; }
public override string ToString() {
return Text;
}
}
public class FieldKey {
public FieldKey(Block block, List<string> children, List<Block> blocks) {
this.Block = block;
this.Confidence = block.Confidence;
this.Geometry = block.Geometry;
this.Id = block.Id;
this.Text = string.Empty;
this.Content = new List<dynamic>();
var words = new List<string>();
if(children != null && children.Count > 0) {
children.ForEach(c => {
var wordBlock = blocks.Find(b => b.Id == c);
if(wordBlock.BlockType == "WORD") {
var w = new Word(wordBlock, blocks);
this.Content.Add(w);
words.Add(w.Text);
}
});
}
if(words.Count > 0) {
this.Text = string.Join(" ", words);
}
}
public List<dynamic> Content { get; set; }
public Block Block { get; set; }
public float Confidence { get; set; }
public Geometry Geometry { get; set; }
public string Id { get; set; }
public string Text { get; set; }
public override string ToString() {
return Text;
}
}
public class Field {
public Field(Block block, List<Block> blocks) {
var relationships = block.Relationships;
if(relationships != null && relationships.Count > 0) {
relationships.ForEach(r => {
if(r.Type == "CHILD") {
this.Key = new FieldKey(block, r.Ids, blocks);
} else if(r.Type == "VALUE") {
r.Ids.ForEach(id => {
var v = blocks.Find(b => b.Id == id);
if(v.EntityTypes.Contains("VALUE")) {
var vr = v.Relationships;
if(vr != null && vr.Count > 0) {
vr.ForEach(vc => {
if(vc.Type == "CHILD") {
this.Value = new FieldValue(v, vc.Ids, blocks);
}
});
}
}
});
}
});
}
}
public FieldKey Key { get; set; }
public FieldValue Value { get; set; }
public override string ToString() {
var k = this.Key == null ? string.Empty : this.Key.ToString();
var v = this.Value == null ? string.Empty : this.Value.ToString();
return string.Format(@"
{0}Field{0}===={0}
Key: {1}, Value: {2}
", Environment.NewLine, k, v);
}
}
public class Cell {
public Cell(Block block, List<Block> blocks) {
this.Block = block;
this.ColumnIndex = block.ColumnIndex;
this.ColumnSpan = block.ColumnSpan;
this.Confidence = block.Confidence;
this.Content = new List<dynamic>();
this.Geometry = block.Geometry;
this.Id = block.Id;
this.RowIndex = block.RowIndex;
this.RowSpan = block.RowSpan;
this.Text = string.Empty;
var relationships = block.Relationships;
if(relationships != null && relationships.Count > 0) {
relationships.ForEach(r => {
if(r.Type == "CHILD") {
r.Ids.ForEach(id => {
var rb = blocks.Find(b => b.Id == id);
if(rb.BlockType == "WORD") {
var w = new Word(rb, blocks);
this.Content.Add(w);
this.Text = this.Text + w.Text + " ";
} else if(rb.BlockType == "SELECTION_ELEMENT") {
var se = new SelectionElement(rb, blocks);
this.Content.Add(se);
this.Text = this.Text + se.SelectionStatus + ", ";
}
});
}
});
}
}
public int RowIndex { get; set; }
public int RowSpan { get; set; }
public int ColumnIndex { get; set; }
public int ColumnSpan { get; set; }
public List<dynamic> Content { get; set; }
public Block Block { get; set; }
public float Confidence { get; set; }
public Geometry Geometry { get; set; }
public string Id { get; set; }
public string Text { get; set; }
public override string ToString() {
return this.Text;
}
}
}