format and tidy up alto export autogenerated code. tidy up docstrum

This commit is contained in:
Eliot Jones
2019-10-14 18:30:18 +01:00
parent e2c9db8d50
commit d68bd88824
53 changed files with 3114 additions and 5378 deletions

View File

@@ -11,6 +11,67 @@ namespace UglyToad.PdfPig.DocumentLayoutAnalysis
/// </summary>
internal class ClusteringAlgorithms
{
/// <summary>
/// Algorithm to group elements via transitive closure, using nearest neighbours and maximum distance.
/// https://en.wikipedia.org/wiki/Transitive_closure
/// </summary>
/// <typeparam name="T">Letter, Word, TextLine, etc.</typeparam>
/// <param name="elements">List of elements to group.</param>
/// <param name="distMeasure">The distance measure between two points.</param>
/// <param name="maxDistanceFunction">The function that determines the maximum distance between two points in the same cluster.</param>
/// <param name="pivotPoint">The pivot's point to use for pairing, e.g. BottomLeft, TopLeft.</param>
/// <param name="candidatesPoint">The candidates' point to use for pairing, e.g. BottomLeft, TopLeft.</param>
/// <param name="filterPivot">Filter to apply to the pivot point. If false, point will not be paired at all, e.g. is white space.</param>
/// <param name="filterFinal">Filter to apply to both the pivot and the paired point. If false, point will not be paired at all, e.g. pivot and paired point have same font.</param>
internal static IEnumerable<HashSet<int>> SimpleTransitiveClosure<T>(List<T> elements,
Func<PdfPoint, PdfPoint, double> distMeasure,
Func<T, T, double> maxDistanceFunction,
Func<T, PdfPoint> pivotPoint, Func<T, PdfPoint> candidatesPoint,
Func<T, bool> filterPivot, Func<T, T, bool> filterFinal)
{
/*************************************************************************************
* Algorithm steps
* 1. Find nearest neighbours indexes (done in parallel)
* Iterate every point (pivot) and put its nearest neighbour's index in an array
* e.g. if nearest neighbour of point i is point j, then indexes[i] = j.
* Only conciders a neighbour if it is within the maximum distance.
* If not within the maximum distance, index will be set to -1.
* Each element has only one connected neighbour.
* NB: Given the possible asymmetry in the relationship, it is possible
* that if indexes[i] = j then indexes[j] != i.
*
* 2. Group indexes
* Group indexes if share neighbours in common - Transitive closure
* e.g. if we have indexes[i] = j, indexes[j] = k, indexes[m] = n and indexes[n] = -1
* (i,j,k) will form a group and (m,n) will form another group.
*************************************************************************************/
int[] indexes = Enumerable.Repeat((int)-1, elements.Count).ToArray();
var candidatesPoints = elements.Select(candidatesPoint).ToList();
// 1. Find nearest neighbours indexes
Parallel.For(0, elements.Count, e =>
{
var pivot = elements[e];
if (filterPivot(pivot))
{
int index = pivotPoint(pivot).FindIndexNearest(candidatesPoints, distMeasure, out double dist);
var paired = elements[index];
if (filterFinal(pivot, paired) && dist < maxDistanceFunction(pivot, paired))
{
indexes[e] = index;
}
}
});
// 2. Group indexes
var groupedIndexes = GroupIndexes(indexes);
return groupedIndexes;
}
/// <summary>
/// Algorithm to group elements via transitive closure, using nearest neighbours and maximum distance.
/// https://en.wikipedia.org/wiki/Transitive_closure
@@ -47,7 +108,7 @@ namespace UglyToad.PdfPig.DocumentLayoutAnalysis
*************************************************************************************/
int[] indexes = Enumerable.Repeat((int)-1, elements.Length).ToArray();
var candidatesPoints = elements.Select(x => candidatesPoint(x)).ToList();
var candidatesPoints = elements.Select(candidatesPoint).ToList();
// 1. Find nearest neighbours indexes
Parallel.For(0, elements.Length, e =>