Edges.java
4.28 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/**
*
*/
package is2.parser;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map.Entry;
/**
* @author Dr. Bernd Bohnet, 13.05.2009;
*
*
*/
public final class Edges {
private static short[][][] edges;
private static HashMap<Short, Integer> labelCount = new HashMap<Short, Integer>();
private static HashMap<String, Integer> slabelCount = new HashMap<String, Integer>();
static short[] def = new short[1];
private Edges() {
}
/**
* @param length
*/
public static void init(int length) {
edges = new short[length][length][];
}
public static void findDefault() {
int best = 0;
for (Entry<Short, Integer> e : labelCount.entrySet()) {
if (best < e.getValue()) {
best = e.getValue();
def[0] = e.getKey();
}
}
// labelCount=null;
// String[] types = new String[mf.getFeatureCounter().get(PipeGen.REL)];
// for (Entry<String, Integer> e :
// MFO.getFeatureSet().get(PipeGen.REL).entrySet()) types[e.getValue()]
// = e.getKey();
is2.util.DB.println("set default label to " + def[0] + " ");
// System.out.println("found default "+def[0]);
}
final static public void put(int pos1, int pos2, short label) {
putD(pos1, pos2, label);
// putD(pos2, pos1,!dir, label);
}
final static public void putD(int pos1, int pos2, short label) {
Integer lc = labelCount.get(label);
if (lc == null)
labelCount.put(label, 1);
else
labelCount.put(label, lc + 1);
String key = pos1 + "-" + pos2 + label;
Integer lcs = slabelCount.get(key);
if (lcs == null)
slabelCount.put(key, 1);
else
slabelCount.put(key, lcs + 1);
if (edges[pos1][pos2] == null) {
edges[pos1][pos2] = new short[1];
edges[pos1][pos2][0] = label;
// edgesh[pos1][pos2][dir?0:1] = new TIntHashSet(2);
// edgesh[pos1][pos2][dir?0:1].add(label);
} else {
short labels[] = edges[pos1][pos2];
for (short l : labels) {
// contains label already?
if (l == label)
return;
}
short[] nlabels = new short[labels.length + 1];
System.arraycopy(labels, 0, nlabels, 0, labels.length);
nlabels[labels.length] = label;
edges[pos1][pos2] = nlabels;
// edgesh[pos1][pos2][dir?0:1].add(label);
}
}
final static public short[] get(int pos1, int pos2) {
if (pos1 < 0 || pos2 < 0 || edges[pos1][pos2] == null)
return def;
return edges[pos1][pos2];
}
/**
* @param dis
*/
static public void write(DataOutputStream d) throws IOException {
int len = edges.length;
d.writeShort(len);
for (int p1 = 0; p1 < len; p1++) {
for (int p2 = 0; p2 < len; p2++) {
if (edges[p1][p2] == null)
d.writeShort(0);
else {
d.writeShort(edges[p1][p2].length);
for (int l = 0; l < edges[p1][p2].length; l++) {
d.writeShort(edges[p1][p2][l]);
}
}
}
}
d.writeShort(def[0]);
}
/**
* @param dis
*/
public static void read(DataInputStream d) throws IOException {
int len = d.readShort();
edges = new short[len][len][];
for (int p1 = 0; p1 < len; p1++) {
for (int p2 = 0; p2 < len; p2++) {
int ll = d.readShort();
if (ll == 0) {
edges[p1][p2] = null;
} else {
edges[p1][p2] = new short[ll];
for (int l = 0; l < ll; l++) {
edges[p1][p2][l] = d.readShort();
}
}
}
}
def[0] = d.readShort();
}
public static void print() {
for(int i = 0; i < edges.length; ++i)
for(int j = 0; j < edges[i].length; ++j)
if(edges[i][j] != null)
System.out.println("edges[" + i + "][" + j + "] = " + Arrays.toString(edges[i][j]));
assert def.length == 0;
System.out.println("def = [" + def[0] + "]");
}
public static class C implements Comparator<Short> {
public C() {
super();
}
String _key;
public C(String key) {
super();
_key = key;
}
/*
* (non-Javadoc)
*
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
*/
@Override
public int compare(Short l1, Short l2) {
// int c1 = labelCount.get(l1);
// int c2 = labelCount.get(l2);
// if (true) return c1==c2?0:c1>c2?-1:1;
int x1 = slabelCount.get(_key + l1.shortValue());
int x2 = slabelCount.get(_key + l2.shortValue());
// System.out.println(x1+" "+x2);
return x1 == x2 ? 0 : x1 > x2 ? -1 : 1;
}
}
}