-
Notifications
You must be signed in to change notification settings - Fork 5
/
Template3.java
139 lines (113 loc) · 3.81 KB
/
Template3.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
package org.genericsystem.reinforcer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.genericsystem.reinforcer.tools.StringCompare;
import org.genericsystem.reinforcer.tools.StringCompare.SIMILARITY;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Template3 {
private final Logger logger = LoggerFactory.getLogger(Template3.class);
private final String name;
protected final List<Labels> members = new ArrayList<>();
private List<Constraint> constraints = new ArrayList<>();
private List<LabelDesc> description = new ArrayList<>();
public Template3(String name) {
this.name = name;
}
public String getName() {
return name;
}
public boolean matches(Labels entry) {
return constraints.stream().allMatch(constraint -> constraint.check(entry));
}
public void addLabels(Labels labels) {
members.add(labels);
}
public boolean contains(Labels al) {
return members.contains(al);
}
// Builds template from 2 labels.
private void buildTemplate() {
if (members.size() < 2)
throw new IllegalStateException("There must be at least two examples for the template to be built.");
Labels labels1 = members.get(0);
Labels labels2 = members.get(1);
List<Match> bestAligned = labels2.alignWith(labels1);
for (Match match : bestAligned)
if (match.source != null && match.match != null && StringCompare.similar(match.source.getText(), match.match.getText(), SIMILARITY.LEVENSHTEIN))
description.add(new LabelDesc(match.source));
description.forEach(ld -> ld.setDirection(labels2.contentDirection(ld.getLabel(), description)));
}
// Specifies the template when a new item is added.
private void reinforceTemplate(Labels absoluteLabels) {
Iterator<LabelDesc> it = description.iterator();
while (it.hasNext()) {
LabelDesc descriptor = it.next();
if (!absoluteLabels.toList().stream().map(label -> label.getText()).anyMatch(c -> StringCompare.similar(c, descriptor.getLabel().getText(), SIMILARITY.LEVENSHTEIN)))
it.remove();
}
}
public List<DetectedContent> extractData(Labels labels) {
Labels ref = new Labels();
description.forEach(ld -> ref.addLabel(ld.getLabel()));
List<Match> matched = ref.alignWith(labels);
Labels aligned = new Labels();
matched.forEach(match -> {
if (match.match != null)
aligned.addLabel(match.match);
});
List<DetectedContent> result = new ArrayList<>();
for (Match match : matched)
if (match.source != null && match.match != null) {
LabelDesc ld = description.stream().filter(ref.getTest.apply(match.source)).findFirst().orElse(null);
if (ld != null) {
Label target = aligned.getDirectNeighbor(match.match, ld.direction);
if (target != null)
result.add(new DetectedContent(match.source.getText(), target.getText()));
}
}
return result;
}
public void reinforce(Labels absoluteLabels) {
members.add(absoluteLabels);
if (members.size() == 2)
buildTemplate();
if (members.size() > 2)
reinforceTemplate(absoluteLabels);
}
@Override
public String toString() {
return "\nTemplate " + name + "\nConstraints: " + constraints;
}
public static class Match {
Label source;
Label match;
public Match(Label source, Label match) {
this.source = source;
this.match = match;
}
@Override
public String toString() {
return "\n{ Match between " + source + " and " + match + " }";
}
}
public static class LabelDesc {
private Label label;
// Direction to look in for corresponding content.
private Direction direction;
public LabelDesc(Label label) {
this.label = label;
}
public void setDirection(Direction direction) {
this.direction = direction;
}
public Label getLabel() {
return label;
}
@Override
public String toString() {
return "{LabelDesc " + label.toString() + ", content direction: " + direction + "}";
}
}
}