-
Notifications
You must be signed in to change notification settings - Fork 5
/
Scores.java
147 lines (132 loc) · 4.3 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
package org.genericsystem.cv;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.genericsystem.cv.utils.Levenshtein;
//TODO compute score / bestText on every add ?
public class Scores {
private final Map<String, Integer> ocrs = new HashMap<>();
private final List<String> ocrs2 = new ArrayList<>();
private final Map<String, String> ocrResults = new HashMap<>();
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
* total 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;
}
/**
* Compute the supervised scores for a given OCR result.
*
* The Map {@link #ocrResults}, which contains the filtername as a key and
* the OCR text as the value, is analyzed. A new map is created, containing
* the filtername as key and the Levenshtein distance as value.
*
* @return A Map containing the filtername as the key, and the corresponding
* Levenshtein distance (compared to the real value) as the value
*/
public Map<String, Integer> getSupervisedResultsMap() {
Map<String, Integer> results = new HashMap<>();
// If the text value is readable, 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() {
int bestScore = 0;
int allOcrs = 0;
for (Entry<String, Integer> entry : ocrs.entrySet()) {
allOcrs += entry.getValue();
if (bestScore < entry.getValue()) {
bestScore = entry.getValue();
}
}
return Integer.valueOf(bestScore).doubleValue() / allOcrs;
}
// Best text = most occurrence
public String getBestText() {
String bestText = "";
int bestScore = 0;
for (Entry<String, Integer> entry : ocrs.entrySet()) {
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 String getRealText() {
return realText;
}
public void setRealText(String realText) {
this.realText = realText;
}
}