-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLDA.java
More file actions
272 lines (215 loc) · 5.13 KB
/
LDA.java
File metadata and controls
272 lines (215 loc) · 5.13 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Random;
public class LDA extends TopicModel {
public HashMap<String,Integer> wordMap;
public HashMap<Integer,String> wordMapInv;
public int[][] docs;
public int[][] docsZ;
public int[][] docsX;
public int[][] nDZ;
public int[] nD;
public int[][] nZW;
public int[] nZ;
public int[] nBW;
public int nB;
public int[] nX;
public int D;
public int W;
public int Z;
public double beta;
public double alpha;
public double gamma0;
public double gamma1;
public LDA(int z, double a, double b, double g0, double g1) {
alpha = a;
beta = b;
gamma0 = g0;
gamma1 = g1;
Z = z;
}
public void initialize() {
System.out.println("Initializing...");
Random r = new Random();
docsZ = new int[D][];
docsX = new int[D][];
nDZ = new int[D][Z];
nD = new int[D];
nZW = new int[Z][W];
nZ = new int[Z];
nBW = new int[W];
nB = 0;
nX = new int[2];
for (int d = 0; d < D; d++) {
docsZ[d] = new int[docs[d].length];
docsX[d] = new int[docs[d].length];
for (int n = 0; n < docs[d].length; n++) {
int w = docs[d][n];
int z = r.nextInt(Z); // select random z value in {0...Z-1}
docsZ[d][n] = z;
//int x = r.nextInt(2); // select x uniformly
int x = 0;
double u = r.nextDouble(); // select random x value in {0,1}
u *= (double)(gamma0+gamma1); // from distribution given by prior
if (u > gamma0) x = 1;
docsX[d][n] = x;
// update counts
nX[x] += 1;
if (x == 0) {
nBW[w] += 1;
nB += 1;
}
else {
nDZ[d][z] += 1;
nD[d] += 1;
nZW[z][w] += 1;
nZ[z] += 1;
}
}
}
}
public void doSampling() {
for (int d = 0; d < D; d++) {
for (int n = 0; n < docs[d].length; n++) {
sample(d, n);
}
}
}
public void sample(int d, int n) {
int w = docs[d][n];
int topic = docsZ[d][n];
int level = docsX[d][n];
// decrement counts
nX[level] -= 1;
if (level == 0) {
nBW[w] -= 1;
nB -= 1;
} else {
nDZ[d][topic] -= 1;
nD[d] -= 1;
nZW[topic][w] -= 1;
nZ[topic] -= 1;
}
double alphaNorm = Z * alpha;
double betaNorm = W * beta;
// sample new value for level
double pTotal = 0.0;
double[] p = new double[Z+1];
// this will be p(x=0)
p[Z] = (nX[0] + gamma0) *
(nBW[w] + beta) / (nB + betaNorm);
pTotal += p[Z];
// sample new value for topic and level
for (int z = 0; z < Z; z++) {
p[z] = (nX[1] + gamma1) *
(nDZ[d][z] + alpha) / (nD[d] + alphaNorm) *
(nZW[z][w] + beta) / (nZ[z] + betaNorm);
pTotal += p[z];
}
Random r = new Random();
double u = r.nextDouble() * pTotal;
double v = 0.0;
for (int z = 0; z < Z+1; z++) {
v += p[z];
if (v > u) {
topic = z;
break;
}
}
if (topic == Z) level = 0;
else level = 1;
// increment counts
nX[level] += 1;
if (level == 0) {
nBW[w] += 1;
nB += 1;
} else {
nDZ[d][topic] += 1;
nD[d] += 1;
nZW[topic][w] += 1;
nZ[topic] += 1;
}
// set new assignments
docsZ[d][n] = topic;
docsX[d][n] = level;
}
public void readDocs(String filename) throws Exception {
System.out.println("Reading input...");
wordMap = new HashMap<String,Integer>();
wordMapInv = new HashMap<Integer,String>();
String s;
// Read through the file, getting the line count
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(filename));
D = 0;
while((s = br.readLine()) != null) {
D++;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
docs = new int[D][];
// Reset the reader, start reading once again
br = null;
try {
br = new BufferedReader(new FileReader(filename));
int d = 0;
while ((s = br.readLine()) != null) {
String[] tokens = s.split("\\s+");
int N = tokens.length;
docs[d] = new int[N];
for (int n = 0; n < N; n++) {
String word = tokens[n];
int key = wordMap.size();
if (!wordMap.containsKey(word)) {
wordMap.put(word, (Integer) key);
wordMapInv.put((Integer) key, word);
}
else {
key = ((Integer) wordMap.get(word)).intValue();
}
docs[d][n] = key;
}
d++;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
W = wordMap.size();
System.out.println(D+" documents");
System.out.println(W+" word types");
}
public void writeOutput(String filename) throws Exception {
System.out.println("Writing output...");
FileWriter fw = new FileWriter(filename+".assign");
BufferedWriter bw = new BufferedWriter(fw);
for (int d = 0; d < D; d++) {
for (int n = 0; n < docs[d].length; n++) {
String word = wordMapInv.get(docs[d][n]);
bw.write(word+":"+docsZ[d][n]+":"+docsX[d][n]+" ");
}
bw.newLine();
}
bw.close();
fw.close();
}
}