-
Notifications
You must be signed in to change notification settings - Fork 311
Document Layout Analysis - Text edges extractor #50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
83889cf
Document Layout Analysis - Text edges extractor
9694b1f
Update TextEdgesExtractor.cs
BobLd 85d5bb7
Adding enum EdgeType
e19b030
Updating woth comments
7de6de3
Updating with comments
801ea3b
Modified PublicApiScannerTests
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Text; | ||
|
|
||
| namespace UglyToad.PdfPig.DocumentLayoutAnalysis | ||
| { | ||
| class Docstrum | ||
| { | ||
| } | ||
| } |
109 changes: 109 additions & 0 deletions
109
src/UglyToad.PdfPig/DocumentLayoutAnalysis/TextEdgesExtractor.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| using System; | ||
| using System.Collections.Concurrent; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Threading.Tasks; | ||
| using UglyToad.PdfPig.Content; | ||
| using UglyToad.PdfPig.Geometry; | ||
|
|
||
| namespace UglyToad.PdfPig.DocumentLayoutAnalysis | ||
| { | ||
| /// <summary> | ||
| /// Text edges extractor. Text edges are where words have either their BoundingBox's left, right or mid coordinates aligned on the same vertical line. | ||
| /// <para>Useful to detect text columns, tables, justified text, lists, etc.</para> | ||
| /// </summary> | ||
| public static class TextEdgesExtractor | ||
| { | ||
| /// <summary> | ||
| /// Functions used to define left, middle and right edges. | ||
| /// </summary> | ||
| private static readonly Tuple<EdgeType, Func<PdfRectangle, decimal>>[] edgesFuncs = new Tuple<EdgeType, Func<PdfRectangle, decimal>>[] | ||
| { | ||
| Tuple.Create<EdgeType, Func<PdfRectangle, decimal>>(EdgeType.Left, x => Math.Round(x.Left, 0)), // use BoundingBox's left coordinate | ||
| Tuple.Create<EdgeType, Func<PdfRectangle, decimal>>(EdgeType.Mid, x => Math.Round(x.Left + x.Width / 2, 0)), // use BoundingBox's mid coordinate | ||
| Tuple.Create<EdgeType, Func<PdfRectangle, decimal>>(EdgeType.Right, x => Math.Round(x.Right, 0)) // use BoundingBox's right coordinate | ||
| }; | ||
|
|
||
| /// <summary> | ||
| /// Get the text edges. | ||
| /// </summary> | ||
| /// <param name="pageWords">The words in the page.</param> | ||
| /// <param name="minimumElements">The minimum number of elements to define a text edge.</param> | ||
| public static IReadOnlyDictionary<EdgeType, List<PdfLine>> GetEdges(IEnumerable<Word> pageWords, int minimumElements = 4) | ||
| { | ||
| if (minimumElements < 0) | ||
| { | ||
| throw new ArgumentException("TextEdgesExtractor.GetEdges(): The minimum number of elements should be positive.", "minimumElements"); | ||
| } | ||
|
|
||
| var cleanWords = pageWords.Where(x => !string.IsNullOrWhiteSpace(x.Text.Trim())); | ||
|
|
||
| ConcurrentDictionary<EdgeType, List<PdfLine>> dictionary = new ConcurrentDictionary<EdgeType, List<PdfLine>>(); | ||
|
|
||
| Parallel.ForEach(edgesFuncs, f => | ||
| { | ||
| dictionary.TryAdd(f.Item1, GetVerticalEdges(cleanWords, f.Item2, minimumElements)); | ||
| }); | ||
| return dictionary.ToDictionary(x => x.Key, x => x.Value); | ||
| } | ||
|
|
||
| private static List<PdfLine> GetVerticalEdges(IEnumerable<Word> pageWords, Func<PdfRectangle, decimal> func, int minimumElements) | ||
| { | ||
| Dictionary<decimal, List<Word>> edges = pageWords.GroupBy(x => func(x.BoundingBox)) | ||
| .Where(x => x.Count() >= minimumElements).ToDictionary(gdc => gdc.Key, gdc => gdc.ToList()); | ||
| Dictionary<decimal, List<List<Word>>> cleanEdges = new Dictionary<decimal, List<List<Word>>>(); | ||
|
|
||
| foreach (var edge in edges) | ||
| { | ||
| var sortedEdges = edge.Value.OrderBy(x => x.BoundingBox.Bottom).ToList(); | ||
| cleanEdges.Add(edge.Key, new List<List<Word>>()); | ||
|
|
||
| var cuttings = pageWords.Except(edge.Value) // remove selected words | ||
| // words that cut the vertical line | ||
| .Where(x => x.BoundingBox.Left < edge.Key && x.BoundingBox.Right > edge.Key) | ||
| // and that are within the boundaries of the edge | ||
| .Where(k => k.BoundingBox.Bottom > edge.Value.Min(z => z.BoundingBox.Bottom) | ||
| && k.BoundingBox.Top < edge.Value.Max(z => z.BoundingBox.Top)) | ||
| .OrderBy(x => x.BoundingBox.Bottom).ToList(); | ||
|
|
||
| if (cuttings.Count > 0) | ||
| { | ||
| foreach (var cut in cuttings) | ||
| { | ||
| var group1 = sortedEdges.Where(x => x.BoundingBox.Top < cut.BoundingBox.Bottom).ToList(); | ||
| if (group1.Count >= minimumElements) cleanEdges[edge.Key].Add(group1); | ||
| sortedEdges = sortedEdges.Except(group1).ToList(); | ||
| } | ||
| if (sortedEdges.Count >= minimumElements) cleanEdges[edge.Key].Add(sortedEdges); | ||
| } | ||
| else | ||
| { | ||
| cleanEdges[edge.Key].Add(sortedEdges); | ||
| } | ||
| } | ||
|
|
||
| return cleanEdges.SelectMany(x => x.Value.Select(y => new PdfLine(x.Key, y.Min(w => w.BoundingBox.Bottom), x.Key, y.Max(w => w.BoundingBox.Top)))).ToList(); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// The type of text edge. | ||
| /// </summary> | ||
| public enum EdgeType | ||
| { | ||
| /// <summary> | ||
| /// Text edges where words have their BoundingBox's left coordinate aligned on the same vertical line. | ||
| /// </summary> | ||
| Left = 0, | ||
|
|
||
| /// <summary> | ||
| /// Text edges where words have their BoundingBox's mid coordinate aligned on the same vertical line. | ||
| /// </summary> | ||
| Mid = 1, | ||
|
|
||
| /// <summary> | ||
| /// Text edges where words have their BoundingBox's right coordinate aligned on the same vertical line. | ||
| /// </summary> | ||
| Right = 2 | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.