-
Notifications
You must be signed in to change notification settings - Fork 242
/
RecursiveXYCut.cs
402 lines (353 loc) · 15.9 KB
/
RecursiveXYCut.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
namespace UglyToad.PdfPig.DocumentLayoutAnalysis.PageSegmenter
{
using Content;
using Core;
using System;
using System.Collections.Generic;
using System.Linq;
using UglyToad.PdfPig.Geometry;
/// <summary>
/// The recursive X-Y cut is a top-down page segmentation technique that decomposes a document
/// recursively into a set of rectangular blocks. This implementation leverages bounding boxes.
/// https://en.wikipedia.org/wiki/Recursive_X-Y_cut
/// <para>See 'Recursive X-Y Cut using Bounding Boxes of Connected Components' by Jaekyu Ha, Robert M.Haralick and Ihsin T. Phillips</para>
/// </summary>
public class RecursiveXYCut : IPageSegmenter
{
private readonly RecursiveXYCutOptions options;
/// <summary>
/// Create an instance of Recursive X-Y Cut page segmenter, <see cref="RecursiveXYCut"/>.
/// </summary>
public static RecursiveXYCut Instance { get; } = new RecursiveXYCut();
/// <summary>
/// Create an instance of Recursive X-Y Cut page segmenter using default options values.
/// </summary>
public RecursiveXYCut() : this(new RecursiveXYCutOptions())
{
}
/// <summary>
/// Create an instance of Recursive X-Y Cut page segmenter using options values.
/// </summary>
/// <param name="options">The <see cref="RecursiveXYCutOptions"/> to use.</param>
/// <exception cref="ArgumentException"></exception>
public RecursiveXYCut(RecursiveXYCutOptions options)
{
this.options = options ?? throw new ArgumentNullException(nameof(options));
}
/// <summary>
/// Get the blocks.
/// </summary>
/// <param name="words">The page's words to segment into <see cref="TextBlock"/>s.</param>
/// <returns>The <see cref="TextBlock"/>s generated by the Recursive X-Y cut method.</returns>
public IReadOnlyList<TextBlock> GetBlocks(IEnumerable<Word> words)
{
if (words?.Any() != true)
{
return Array.Empty<TextBlock>();
}
return GetBlocks(words,
options.MinimumWidth,
options.DominantFontWidthFunc,
options.DominantFontHeightFunc,
options.WordSeparator,
options.LineSeparator);
}
/// <summary>
/// Get the blocks.
/// </summary>
/// <param name="words">The words in the page.</param>
/// <param name="minimumWidth">The minimum width for a block.</param>
/// <param name="dominantFontWidthFunc">The function that determines the dominant font width.</param>
/// <param name="dominantFontHeightFunc">The function that determines the dominant font height.</param>
/// <param name="wordSeparator"></param>
/// <param name="lineSeparator"></param>
private IReadOnlyList<TextBlock> GetBlocks(IEnumerable<Word> words, double minimumWidth,
Func<IEnumerable<Letter>, double> dominantFontWidthFunc,
Func<IEnumerable<Letter>, double> dominantFontHeightFunc,
string wordSeparator, string lineSeparator)
{
// Filter out white spaces
words = words.Where(w => !string.IsNullOrWhiteSpace(w.Text));
if (!words.Any())
{
return Array.Empty<TextBlock>();
}
XYLeaf root = new XYLeaf(words); // Create a root node.
XYNode node = VerticalCut(root, minimumWidth, dominantFontWidthFunc, dominantFontHeightFunc);
if (node.IsLeaf)
{
return new List<TextBlock> { new TextBlock((node as XYLeaf).GetLines(wordSeparator), lineSeparator) };
}
else
{
var leaves = node.GetLeaves();
if (leaves.Count > 0)
{
return leaves.ConvertAll(l => new TextBlock(l.GetLines(wordSeparator), lineSeparator));
}
}
return new List<TextBlock>();
}
private XYNode VerticalCut(XYLeaf leaf, double minimumWidth,
Func<IEnumerable<Letter>, double> dominantFontWidthFunc,
Func<IEnumerable<Letter>, double> dominantFontHeightFunc, int level = 0)
{
// Order words left to right
var words = leaf.Words.OrderBy(w => w.BoundingBox.Normalise().Left).ToArray();
if (words.Length == 0)
{
return new XYNode(null);
}
// Create new leaf with non-whitespace words.
leaf = new XYLeaf(words);
if (leaf.CountWords() <= 1 || leaf.BoundingBox.Width <= minimumWidth)
{
// We stop cutting if
// - only one word remains
// - width is too small
return leaf;
}
// Determine dominant font width
double dominantFontWidth = dominantFontWidthFunc(words.SelectMany(x => x.Letters));
List<Projection> projectionProfile = new List<Projection>();
var firstWordBound = words[0].BoundingBox.Normalise();
Projection currentProjection = new Projection(firstWordBound.Left, firstWordBound.Right);
int wordsCount = words.Length;
for (int i = 1; i < wordsCount; i++)
{
var currentWordBound = words[i].BoundingBox.Normalise();
if (currentProjection.Contains(currentWordBound.Left) || currentProjection.Contains(currentWordBound.Right))
{
// It is overlapping
if (currentWordBound.Left >= currentProjection.LowerBound
&& currentWordBound.Left <= currentProjection.UpperBound
&& currentWordBound.Right > currentProjection.UpperBound)
{
// |____|
// |____|
// |_______| <- updated
currentProjection.UpperBound = currentWordBound.Right;
}
// We ignore the following cases:
// |____|
// |____| (not possible because of OrderBy)
//
// |____|
//|___________| (not possible because of OrderBy)
//
// |____|
// |_|
}
else
{
// No overlap
if (currentWordBound.Left - currentProjection.UpperBound <= dominantFontWidth)
{
// If gap too small -> don't cut
// |____| |____|
currentProjection.UpperBound = currentWordBound.Right;
}
else if (currentProjection.UpperBound - currentProjection.LowerBound < minimumWidth)
{
// Still too small
currentProjection.UpperBound = currentWordBound.Right;
}
else
{
// If gap big enough -> cut!
// |____| | |____|
if (i != wordsCount - 1) // Will always add the last one after
{
projectionProfile.Add(currentProjection);
currentProjection = new Projection(currentWordBound.Left, currentWordBound.Right);
}
}
}
if (i == wordsCount - 1)
{
projectionProfile.Add(currentProjection);
}
}
var newLeavesEnums = projectionProfile.Select(p => leaf.Words.Where(w =>
{
// Get words that are contained in each projection profiles
var normalisedBB = w.BoundingBox.Normalise();
return normalisedBB.Left >= p.LowerBound && normalisedBB.Right <= p.UpperBound;
}));
var newLeaves = newLeavesEnums.Where(e => e.Any()).Select(e => new XYLeaf(e));
var newNodes = newLeaves.Select(l => HorizontalCut(l, minimumWidth,
dominantFontWidthFunc, dominantFontHeightFunc, level)).ToList();
var lost = leaf.Words.Except(newLeavesEnums.SelectMany(x => x)).Where(x => !string.IsNullOrWhiteSpace(x.Text)).ToList();
if (lost.Count > 0)
{
newNodes.AddRange(lost.Select(w => new XYLeaf(w)));
}
return new XYNode(newNodes);
}
private XYNode HorizontalCut(XYLeaf leaf, double minimumWidth,
Func<IEnumerable<Letter>, double> dominantFontWidthFunc,
Func<IEnumerable<Letter>, double> dominantFontHeightFunc, int level = 0)
{
// Order words bottom to top
var words = leaf.Words.OrderBy(w => w.BoundingBox.Normalise().Bottom).ToArray();
if (words.Length == 0)
{
return new XYNode(null);
}
// Create new leaf with non-whitespace words.
leaf = new XYLeaf(words);
if (leaf.CountWords() <= 1)
{
// We stop cutting if
// - only one word remains
return leaf;
}
// Determine dominant font height
double dominantFontHeight = dominantFontHeightFunc(words.SelectMany(x => x.Letters));
List<Projection> projectionProfile = new List<Projection>();
var firstWordBound = words[0].BoundingBox.Normalise();
Projection currentProjection = new Projection(firstWordBound.Bottom, firstWordBound.Top);
int wordsCount = words.Length;
for (int i = 1; i < wordsCount; i++)
{
var currentWordBound = words[i].BoundingBox.Normalise();
if (currentProjection.Contains(currentWordBound.Bottom) || currentProjection.Contains(currentWordBound.Top))
{
// It is overlapping
if (currentWordBound.Bottom >= currentProjection.LowerBound
&& currentWordBound.Bottom <= currentProjection.UpperBound
&& currentWordBound.Top > currentProjection.UpperBound)
{
currentProjection.UpperBound = currentWordBound.Top;
}
}
else
{
// No overlap
if (currentWordBound.Bottom - currentProjection.UpperBound <= dominantFontHeight)
{
// If gap too small -> don't cut
// |____| |____|
currentProjection.UpperBound = currentWordBound.Top;
}
else
{
// If gap big enough -> cut!
// |____| | |____|
if (i != wordsCount - 1) // Will always add the last one after
{
projectionProfile.Add(currentProjection);
currentProjection = new Projection(currentWordBound.Bottom, currentWordBound.Top);
}
}
}
if (i == wordsCount - 1)
{
projectionProfile.Add(currentProjection);
}
}
if (projectionProfile.Count == 1)
{
if (level >= 1)
{
return leaf;
}
else
{
level++;
}
}
var newLeavesEnums = projectionProfile.Select(p => leaf.Words.Where(w =>
{
// Get words that are contained in each projection profiles
var normalisedBB = w.BoundingBox.Normalise();
return normalisedBB.Bottom >= p.LowerBound && normalisedBB.Top <= p.UpperBound;
}));
var newLeaves = newLeavesEnums.Where(e => e.Any()).Select(e => new XYLeaf(e));
var newNodes = newLeaves.Select(l => VerticalCut(l, minimumWidth,
dominantFontWidthFunc, dominantFontHeightFunc, level)).ToList();
var lost = leaf.Words.Except(newLeavesEnums.SelectMany(x => x)).Where(x => !string.IsNullOrWhiteSpace(x.Text)).ToList();
if (lost.Count > 0)
{
newNodes.AddRange(lost.Select(w => new XYLeaf(w)));
}
return new XYNode(newNodes);
}
private struct Projection
{
public double UpperBound { get; set; }
public double LowerBound { get; set; }
public Projection(double lowerBound, double upperBound)
{
UpperBound = upperBound;
LowerBound = lowerBound;
}
/// <summary>
/// Returns true if the value is greater or equal to the lower bound and smaller or equal to the upper bound.
/// </summary>
/// <param name="value">The value to test.</param>
public bool Contains(double value)
{
return value >= LowerBound && value <= UpperBound;
}
}
/// <summary>
/// Recursive X-Y cut page segmenter options.
/// </summary>
public class RecursiveXYCutOptions : IPageSegmenterOptions
{
/// <summary>
/// <inheritdoc/>
/// Default value is -1.
/// </summary>
public int MaxDegreeOfParallelism { get; set; } = -1;
/// <summary>
/// <inheritdoc/>
/// <para>Default value is ' ' (space).</para>
/// </summary>
public string WordSeparator { get; set; } = " ";
/// <summary>
/// <inheritdoc/>
/// <para>Default value is '\n' (new line).</para>
/// </summary>
public string LineSeparator { get; set; } = "\n";
/// <summary>
/// The minimum width for a block.
/// <para>Default value is 1.</para>
/// </summary>
public double MinimumWidth { get; set; } = 1;
/// <summary>
/// The function that determines the dominant font width.
/// <para>Default value is the mode of the block's letters width.
/// If the mode is not available, the average is used.</para>
/// </summary>
public Func<IEnumerable<Letter>, double> DominantFontWidthFunc { get; set; } =
(letters) =>
{
var widths = letters.Select(x => Math.Max(Math.Round(x.Width, 3), Math.Round(x.GlyphRectangle.Width, 3)));
var mode = widths.Mode();
if (double.IsNaN(mode) || mode == 0)
{
mode = widths.Average();
}
return mode;
};
/// <summary>
/// The function that determines the dominant font height.
/// <para>Default value is the mode of the block's letters height times 1.5.
/// If the mode is not available, the average is used.</para>
/// </summary>
public Func<IEnumerable<Letter>, double> DominantFontHeightFunc { get; set; } =
(letters) =>
{
var heights = letters.Select(x => Math.Round(x.GlyphRectangle.Height, 3));
var mode = heights.Mode();
if (double.IsNaN(mode) || mode == 0)
{
mode = heights.Average();
}
return mode * 1.5;
};
}
}
}