-
Notifications
You must be signed in to change notification settings - Fork 5
/
Constraint.java
145 lines (120 loc) · 4.27 KB
/
Constraint.java
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
package org.genericsystem.reinforcer;
import java.util.List;
import java.util.stream.Collectors;
import org.genericsystem.reinforcer.tools.StringCompare;
import org.genericsystem.reinforcer.tools.StringCompare.SIMILARITY;
public interface Constraint {
// Returns true iff the given labels verify the constraint.
boolean check(Labels labels);
// Verified if the labels contains a label with the correct text in the given page part.
public class PositionConstraint implements Constraint {
protected PagePart pagePart;
protected String contents;
public PositionConstraint(PagePart pagePart, String contents) {
this.pagePart = pagePart;
this.contents = contents;
}
@Override
public boolean check(Labels labels) {
return labels.stream().anyMatch(label -> labels.getPosition(label) == pagePart && label.matchesText(contents));
}
public List<Label> getMatchingLabels(Labels labels) {
return labels.stream().filter(label -> labels.getPosition(label) == pagePart && label.matchesText(contents)).collect(Collectors.toList());
}
@Override
public boolean equals(Object obj) {
if (obj == this)
return true;
if (obj == null || getClass() != obj.getClass())
return false;
PositionConstraint other = (PositionConstraint) obj;
return pagePart == other.pagePart && StringCompare.similar(contents, other.contents, SIMILARITY.LEVENSHTEIN);
}
@Override
public int hashCode() {
return pagePart.hashCode();
}
@Override
public String toString() {
return "pagePart: " + pagePart + ", contents: " + contents;
}
}
public class RelationConstraint extends PositionConstraint {
private Direction direction;
private String targetContents;
public RelationConstraint(PagePart pagePart, String sourceContents, Direction direction, String targetContents) {
super(pagePart, sourceContents);
this.direction = direction;
this.targetContents = targetContents;
}
public RelationConstraint(PagePart pagePart, String sourceContents, Direction direction) {
super(pagePart, sourceContents);
this.direction = direction;
}
@Override
public boolean check(Labels labels) {
List<Label> sources = super.getMatchingLabels(labels);
if (sources.isEmpty())
return false;
for (Label source : sources) {
List<Label> possibleTargets = labels.getNeighbors(source, direction);
if (targetContents == null)
return !possibleTargets.isEmpty();
for (Label target : possibleTargets)
if (target.matchesText(targetContents))
return true;
}
return false;
}
@Override
public boolean equals(Object obj) {
if (obj == this)
return true;
if (obj == null || getClass() != obj.getClass())
return false;
RelationConstraint other = (RelationConstraint) obj;
return pagePart == other.pagePart && StringCompare.similar(contents, other.contents, SIMILARITY.LEVENSHTEIN)
&& (targetContents == null && other.targetContents == null || StringCompare.similar(targetContents, other.targetContents, SIMILARITY.LEVENSHTEIN));
}
@Override
public String toString() {
return super.toString() + ", direction: " + direction + (targetContents == null ? "" : ", target: " + targetContents);
}
}
// True if some labels are aligned as indicated.
// Used only if the other types of constraints can’t be used.
public class AlignmentConstraint implements Constraint {
private final Alignment alignment;
private int min; // >= 2
public AlignmentConstraint(Alignment alignment, int min) {
this.alignment = alignment;
this.min = min;
}
@Override
public boolean check(Labels labels) {
List<List<Label>> grouped = labels.groupAlignedLabels(alignment);
return grouped.stream().anyMatch(group -> group.size() >= min);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((alignment == null) ? 0 : alignment.hashCode());
result = prime * result + min;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null || getClass() != obj.getClass())
return false;
AlignmentConstraint other = (AlignmentConstraint) obj;
return alignment == other.alignment && min == other.min;
}
@Override
public String toString() {
return "alignment: " + alignment + ", min: " + min;
}
}
}