Skip to content
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

Fix infinite loop in table detection #326

Merged
merged 1 commit into from
May 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
@@ -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
@@ -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));
Original file line number Diff line number Diff line change
@@ -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();
Loading