-
-
Notifications
You must be signed in to change notification settings - Fork 480
Expand file tree
/
Copy pathsim-lda.R
More file actions
33 lines (24 loc) · 793 Bytes
/
sim-lda.R
File metadata and controls
33 lines (24 loc) · 793 Bytes
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
library("gtools"); # for rdirichlet
V <- 5; # words: river, stream, bank, money, loan
K <- 2; # topics: RIVER, BANK
phi <- array(NA,c(2,5));
phi[1,] = c(0.330, 0.330, 0.330, 0.005, 0.005);
phi[2,] = c(0.005, 0.005, 0.330, 0.330, 0.330);
set.seed(123)
M <- 25; # docs
avg_doc_length <- 10;
doc_length <- rpois(M,avg_doc_length);
N <- sum(doc_length);
alpha <- rep(1/K,K);
beta <- rep(1/V,V);
theta <- rdirichlet(M,alpha);
corpus <- matrix(0, nrow = M, ncol = V);
for (m in 1:M) {
for (i in 1:doc_length[m]) {
topic_id <- which(rmultinom(1,1,theta[m,]) == 1);
word_id <- which(rmultinom(1,1,phi[topic_id,]) == 1);
corpus[m, word_id] = corpus[m, word_id] + 1;
}
}
stopifnot(all(rowSums(corpus) == doc_length));
dump(c("K","V","M","corpus","alpha","beta"),"lda.data.R");