-
Notifications
You must be signed in to change notification settings - Fork 5
/
Scores.java
174 lines (154 loc) · 4.97 KB
/
Scores.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package org.genericsystem.cv;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import org.genericsystem.api.core.Snapshot;
import org.genericsystem.common.Generic;
import org.genericsystem.cv.comparator.ZoneRealValue;
import org.genericsystem.cv.model.Doc;
import org.genericsystem.cv.model.DocClass;
import org.genericsystem.cv.model.ImgFilter;
import org.genericsystem.cv.model.ZoneGeneric;
import org.genericsystem.cv.model.ZoneText;
import org.genericsystem.cv.model.Doc.DocInstance;
import org.genericsystem.cv.model.DocClass.DocClassInstance;
import org.genericsystem.cv.model.ZoneText.ZoneTextInstance;
import org.genericsystem.kernel.Engine;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
//TODO compute score / bestText on every add ?
public class Scores {
private static final ObjectMapper mapper = new ObjectMapper();
private static final String basePath = "classes/id-fr-front";
private final Map<String, Integer> ocrs = new HashMap<>();
private final List<String> ocrs2 = new ArrayList<>();
private final Map<String, String> ocrResults = new HashMap<>();
private String filename;
private int zone;
private Integer minLevenshtein;
private String realText;
public void put(String s) {
Integer count = ocrs.get(s);
ocrs.put(s, 1 + (count != null ? count : 0));
ocrs2.add(s);
}
public void put(String filtername, String ocr) {
ocrResults.put(filtername, ocr);
}
/**
* Compute the Levenshtein distance of all OCR text.
*
* The Map {@link #ocrResults}, which contains the filtername as a key and
* the OCR text as the value, is analyzed. The {@link #minLevenshtein} value
* is also set with the minimum distance found.
*
* @return A Map containing the filtername as the key, and the corresponding
* Levenshtein distance as the value
*/
public Map<String, Integer> getResultsMap() {
Map<String, Integer> results = new HashMap<>();
int shorterDistance = Integer.MAX_VALUE;
for (Entry<String, String> entry : ocrResults.entrySet()) {
int dist = 0;
for (Entry<String, String> entry2 : ocrResults.entrySet()) {
dist += Levenshtein.distance(entry.getValue(), entry2.getValue());
}
results.put(entry.getKey(), dist);
if (dist < shorterDistance) {
shorterDistance = dist;
}
}
minLevenshtein = shorterDistance;
return results;
}
public Map<String, Integer> getSupervisedResultsMap() {
Map<String, Integer> results = new HashMap<>();
/*
* If the text value is readble, compare with each OCR text and store the Levenshtein distance.
* For convenience, all spaces are removed
*/
if (this.realText != null && !this.realText.isEmpty() ) {
System.out.println("Trained data found! Using supervised training");
for (Entry<String, String> entry : ocrResults.entrySet()) {
int dist = Levenshtein.distance(entry.getValue().replaceAll("[ .,]", "").trim(),
this.realText.replaceAll("[ .,]", "").trim());
results.put(entry.getKey(), dist);
}
} else {
System.out.println("Unable to find the zone! Using unsupervised training");
return getResultsMap();
}
return results;
}
public Integer getMinLevenshtein() {
return minLevenshtein;
}
public double getBestScore() {
String bestText = "";
int bestScore = 0;
int allOcrs = 0;
for (Entry<String, Integer> entry : ocrs.entrySet()) {
allOcrs += entry.getValue();
if (bestScore < entry.getValue()) {
bestScore = entry.getValue();
bestText = entry.getKey();
}
}
return Integer.valueOf(bestScore).doubleValue() / allOcrs;
}
// Best text = most occurrence
public String getBestText() {
String bestText = "";
int bestScore = 0;
int allOcrs = 0;
for (Entry<String, Integer> entry : ocrs.entrySet()) {
allOcrs += entry.getValue();
if (bestScore < entry.getValue()) {
bestScore = entry.getValue();
bestText = entry.getKey();
}
}
return bestText;
}
// Best text = shorter Levenshtein distance
public String getBestText2() {
String bestText = "";
int shorterDistance = Integer.MAX_VALUE;
for (String key : ocrs2) {
if (!"".equals(key)) {
int d = 0;
for (String key2 : ocrs2) {
// System.out.println("key2 : " + key2 + " Levenshtein : " +
// Levenshtein.distance(key, key2));
d += Levenshtein.distance(key, key2);
}
// System.out.println("key : " + key + " somme distance : " +
// d);
if (d < shorterDistance) {
bestText = key;
shorterDistance = d;
}
}
}
// System.out.println("best text: " + bestText);
// System.out.println("shorter distance: " + shorterDistance);
return bestText;
}
public void setFilename(String filename) {
this.filename = filename;
}
public void setZone(int zone) {
this.zone = zone;
}
public String getRealText() {
return realText;
}
public void setRealText(String realText) {
this.realText = realText;
}
}