ParallelExtract.java
3.96 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
package extractors;
import java.util.ArrayList;
import java.util.concurrent.Callable;
import is2.data.Cluster;
import is2.data.DataF;
import is2.data.Edges;
import is2.data.F2SF;
import is2.data.Instances;
import is2.data.Long2IntInterface;
/**
* @author Bernd Bohnet, 30.08.2009
*
* This class implements a parallel feature extractor.
*/
final public class ParallelExtract implements Callable<Object> {
// the data space of the weights for a dependency tree
final DataF d;
// the data extractor does the actual work
final Extractor extractor;
private Instances is;
private int i;
private F2SF para;
private Cluster cluster;
private Long2IntInterface li;
public ParallelExtract(Extractor e, Instances is, int i, DataF d, F2SF para, Cluster cluster,
Long2IntInterface li) {
this.is = is;
extractor = e;
this.d = d;
this.i = i;
this.para = para;
this.cluster = cluster;
this.li = li;
}
public static class DSet {
int w1, w2;
}
@Override
public Object call() {
try {
F2SF f = para;
short[] pos = is.pposs[i];
int[] forms = is.forms[i];
int[] lemmas = is.plemmas[i];
short[][] feats = is.feats[i];
int length = pos.length;
long[] svs = new long[250];
int type = extractor.getType();
while (true) {
DSet set = get();
if (set == null)
break;
int w1 = set.w1;
int w2 = set.w2;
f.clear();
int n = extractor.basic(pos, forms, w1, w2, cluster, f);
d.pl[w1][w2] = f.getScoreF();
short[] labels = Edges.get(pos[w1], pos[w2], false);
float[][] lab = d.lab[w1][w2];
extractor.firstm(is, i, w1, w2, 0, cluster, svs);
if (labels != null) {
for (int l = labels.length - 1; l >= 0; l--) {
short label = labels[l];
f.clear();
for (int k = svs.length - 1; k >= 0; k--)
if (svs[k] > 0)
f.add(li.l2i(svs[k] + label * type));
lab[label][0] = f.getScoreF();
}
}
labels = Edges.get(pos[w1], pos[w2], true);
if (labels != null) {
for (int l = labels.length - 1; l >= 0; l--) {
int label = labels[l];
f.clear();
for (int k = svs.length - 1; k >= 0; k--)
if (svs[k] > 0)
f.add(li.l2i(svs[k] + label * type));
lab[label][1] = f.getScoreF();
}
}
int s = w1 < w2 ? w1 : w2;
int e = w1 < w2 ? w2 : w1;
int sg = w1 < w2 ? w1 : 0;
int eg = w1 < w2 ? length : w1 + 1;
for (int m = s; m < e; m++) {
for (int dir = 0; dir < 2; dir++) {
labels = Edges.get(pos[w1], pos[w2], dir == 1);
float lab2[] = new float[labels.length];
int g = (m == s || e == m) ? -1 : m;
extractor.siblingm(is, i, pos, forms, lemmas, feats, w1, w2, g, 0, cluster, svs, n);
for (int l = labels.length - 1; l >= 0; l--) {
int label = labels[l];
f.clear();
for (int k = svs.length - 1; k >= 0; k--) {
if (svs[k] > 0)
f.add(li.l2i(svs[k] + label * type));
}
lab2[l] = f.score;// f.getScoreF();
}
d.sib[w1][w2][m][dir] = lab2;
}
}
for (int m = sg; m < eg; m++) {
for (int dir = 0; dir < 2; dir++) {
labels = Edges.get(pos[w1], pos[w2], dir == 1);
float[] lab2 = new float[labels.length];
int g = (m == s || e == m) ? -1 : m;
extractor.gcm(is, i, w1, w2, g, 0, cluster, svs);
for (int l = labels.length - 1; l >= 0; l--) {
int label = labels[l];
f.clear();
for (int k = svs.length - 1; k >= 0; k--) {
if (svs[k] > 0)
f.add(li.l2i(svs[k] + label * type));
}
lab2[l] = f.getScoreF();
}
d.gra[w1][w2][m][dir] = lab2;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
static ArrayList<DSet> sets = new ArrayList<DSet>();
private DSet get() {
synchronized (sets) {
if (sets.size() == 0)
return null;
return sets.remove(sets.size() - 1);
}
}
static public void add(int w1, int w2) {
DSet ds = new DSet();
ds.w1 = w1;
ds.w2 = w2;
sets.add(ds);
}
}