-
Notifications
You must be signed in to change notification settings - Fork 5
/
ZoneStream.java
127 lines (107 loc) · 2.67 KB
/
ZoneStream.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
package org.genericsystem.cv.docPattern;
import java.util.List;
import java.util.stream.Collectors;
import org.opencv.core.Rect;
public class ZoneStream {
private Rect rect;
private List<String> labels;
private String type;
private List<String> authorizedValues;
public static final String typeAttribute = "ATTRIBUTE";
public static final String typeString = "STRING";
public static final String typeFloat = "FLOAT";
public static final String typeInteger = "INTEGER";
public ZoneStream(Rect rect, List<String> labels) {
super();
this.rect = rect;
this.labels = labels;
this.init();
}
public Rect getRect() {
return rect;
}
public void setRect(Rect rect) {
this.rect = rect;
}
public List<String> getLabels() {
return labels;
}
public void setLabels(List<String> labels) {
this.labels = labels;
}
public String getType() {
return this.type;
}
public List<String> getAuthorizedValues() {
return this.authorizedValues;
}
public void init() {
List<String> possibleValues = this.labels.stream().distinct().collect(Collectors.toList());
if (possibleValues.size() == 1)
this.type = typeAttribute;
else {
boolean isNumber = true;
for (String s : possibleValues) {
try {
float f = Float.parseFloat(s);
} catch (NumberFormatException e) {
isNumber = false;
break;
}
}
boolean isInteger = false;
if (isNumber) {
isInteger = true;
for (String s : possibleValues) {
try {
int i = Integer.parseInt(s);
} catch (NumberFormatException e) {
isInteger = false;
break;
}
}
}
if (isInteger)
this.type = typeInteger;
else if (isNumber)
this.type = typeFloat;
else
this.type = typeString;
}
if (possibleValues.size() < 0.5 * labels.size()) {
this.authorizedValues = possibleValues;
}
}
public boolean isLeft(ZoneStream zs) {
if (this.rect.br().x < zs.rect.x)
return true;
else
return false;
}
public boolean isAbove(ZoneStream zs) {
if (this.rect.br().y < zs.rect.y)
return true;
else
return false;
}
public double distance(ZoneStream zs) {
double dx = zs.rect.x - this.rect.br().x;
double dy = zs.rect.y - this.rect.br().y;
return Math.pow(dx, 2) + Math.pow(dy, 2);
}
public String longestCommonPrefix(String a, String b) {
int minLength = Math.min(a.length(), b.length());
for (int i = 0; i < minLength; i++) {
if (a.charAt(i) != b.charAt(i)) {
return a.substring(0, i);
}
}
return a.substring(0, minLength);
}
public String greatestCommonPrefix() {
String longest = labels.get(0);
for (int l = 1; l < labels.size() - 1; l++)
longest = longestCommonPrefix(longest, labels.get(l));
return longest;
}
}