Skip to content

Commit

Permalink
Fix infinite loop in table detection
Browse files Browse the repository at this point in the history
  • Loading branch information
MaximPlusov committed May 2, 2024
1 parent 3726528 commit ff06d95
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.verapdf.wcag.algorithms.semanticalgorithms.tables;

import java.util.Objects;

public class TableClusterGap {
private TableCluster link;
private double gap;
Expand All @@ -24,4 +26,22 @@ public void setGap(double gap) {
public double getGap() {
return gap;
}

@Override
public int hashCode() {
return Objects.hash(link.hashCode(), gap);
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!super.equals(o)) {
return false;
}
TableClusterGap that = (TableClusterGap) o;
return Objects.equals(gap, that.gap) &&
Objects.equals(link.getId(), that.getLink().getId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ private List<TableCluster> mergeInitialClusters(List<List<TableCluster>> cluster
if (j < clusterRow.size() - 1) {
TableCluster nextCluster = clusterRow.get(j + 1);

//TODO: this gap could be negative, for example, if clusters intersect
double gap = nextCluster.getLeftX() - cluster.getRightX();
cluster.getFirstRow().setRightGap(new TableClusterGap(nextCluster, gap));
nextCluster.getFirstRow().setLeftGap(new TableClusterGap(cluster, gap));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,27 @@ public static boolean isWeakCluster(TableCluster cluster, List<TableCluster> hea
if (cluster.getHeader() != null) {
return false;
}

Set<TableClusterGap> visitedGaps = new HashSet<>();
TableClusterGap gap = cluster.getMinLeftGap();
while (gap != null && gap.getLink().getHeader() == null) {
if (visitedGaps.contains(gap)) {
gap = null;
break;
}
visitedGaps.add(gap);
gap = gap.getLink().getMinLeftGap();
}
TableCluster leftHeader = (gap == null) ? null : gap.getLink().getHeader();

visitedGaps.clear();

gap = cluster.getMinRightGap();
while (gap != null && gap.getLink().getHeader() == null) {
if (visitedGaps.contains(gap)) {
gap = null;
break;
}
visitedGaps.add(gap);
gap = gap.getLink().getMinRightGap();
}
TableCluster rightHeader = (gap == null) ? null : gap.getLink().getHeader();
Expand Down

0 comments on commit ff06d95

Please sign in to comment.