ealdent-lda-ruby 0.1.4 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/lda-inference.c +15 -1
- data/lib/lda-model.c +48 -1
- data/lib/lda-model.h +3 -0
- metadata +2 -2
data/lib/lda-inference.c
CHANGED
@@ -469,7 +469,21 @@ void run_quiet_em(char* start, corpus* corpus) {
|
|
469
469
|
if (strcmp(start, "seeded")==0) {
|
470
470
|
model = new_lda_model(corpus->num_terms, NTOPICS);
|
471
471
|
ss = new_lda_suffstats(model);
|
472
|
-
|
472
|
+
if (VERBOSE) {
|
473
|
+
corpus_initialize_ss(ss, model, corpus);
|
474
|
+
} else {
|
475
|
+
quiet_corpus_initialize_ss(ss, model, corpus);
|
476
|
+
}
|
477
|
+
if (VERBOSE) {
|
478
|
+
lda_mle(model, ss, 0);
|
479
|
+
} else {
|
480
|
+
quiet_lda_mle(model, ss, 0);
|
481
|
+
}
|
482
|
+
model->alpha = INITIAL_ALPHA;
|
483
|
+
} else if (strcmp(start, "fixed")==0) {
|
484
|
+
model = new_lda_model(corpus->num_terms, NTOPICS);
|
485
|
+
ss = new_lda_suffstats(model);
|
486
|
+
corpus_initialize_fixed_ss(ss, model, corpus);
|
473
487
|
if (VERBOSE) {
|
474
488
|
lda_mle(model, ss, 0);
|
475
489
|
} else {
|
data/lib/lda-model.c
CHANGED
@@ -19,6 +19,7 @@
|
|
19
19
|
|
20
20
|
#include "lda-model.h"
|
21
21
|
|
22
|
+
|
22
23
|
/*
|
23
24
|
* compute MLE lda model from sufficient statistics
|
24
25
|
*
|
@@ -70,7 +71,7 @@ void quiet_lda_mle(lda_model* model, lda_suffstats* ss, int estimate_alpha) {
|
|
70
71
|
}
|
71
72
|
if (estimate_alpha == 1)
|
72
73
|
{
|
73
|
-
model->alpha =
|
74
|
+
model->alpha = quiet_opt_alpha(ss->alpha_suffstats,
|
74
75
|
ss->num_docs,
|
75
76
|
model->num_topics);
|
76
77
|
|
@@ -168,6 +169,52 @@ void corpus_initialize_ss(lda_suffstats* ss, lda_model* model, corpus* c)
|
|
168
169
|
}
|
169
170
|
}
|
170
171
|
|
172
|
+
void quiet_corpus_initialize_ss(lda_suffstats* ss, lda_model* model, corpus* c)
|
173
|
+
{
|
174
|
+
int num_topics = model->num_topics;
|
175
|
+
int i, k, d, n;
|
176
|
+
document* doc;
|
177
|
+
|
178
|
+
for (k = 0; k < num_topics; k++)
|
179
|
+
{
|
180
|
+
for (i = 0; i < NUM_INIT; i++)
|
181
|
+
{
|
182
|
+
d = floor(myrand() * c->num_docs);
|
183
|
+
doc = &(c->docs[d]);
|
184
|
+
for (n = 0; n < doc->length; n++)
|
185
|
+
{
|
186
|
+
ss->class_word[k][doc->words[n]] += doc->counts[n];
|
187
|
+
}
|
188
|
+
}
|
189
|
+
for (n = 0; n < model->num_terms; n++)
|
190
|
+
{
|
191
|
+
ss->class_word[k][n] += 1.0;
|
192
|
+
ss->class_total[k] = ss->class_total[k] + ss->class_word[k][n];
|
193
|
+
}
|
194
|
+
}
|
195
|
+
}
|
196
|
+
|
197
|
+
|
198
|
+
/*
|
199
|
+
* Use the first num_topics documents of the corpus as the seeds. If num_topics > num_docs, results might be hairy.
|
200
|
+
*/
|
201
|
+
void corpus_initialize_fixed_ss(lda_suffstats* ss, lda_model* model, corpus* c) {
|
202
|
+
int num_topics = MIN(model->num_topics, c->num_docs);
|
203
|
+
int k, n;
|
204
|
+
document* doc;
|
205
|
+
|
206
|
+
for (k = 0; k < num_topics; k++) {
|
207
|
+
doc = &(c->docs[k]);
|
208
|
+
for (n = 0; n < doc->length; n++) {
|
209
|
+
ss->class_word[k][doc->words[n]] += doc->counts[n];
|
210
|
+
}
|
211
|
+
for (n = 0; n < model->num_terms; n++) {
|
212
|
+
ss->class_word[k][n] += 1.0;
|
213
|
+
ss->class_total[k] = ss->class_total[k] + ss->class_word[k][n];
|
214
|
+
}
|
215
|
+
}
|
216
|
+
}
|
217
|
+
|
171
218
|
/*
|
172
219
|
* allocate new lda model
|
173
220
|
*
|
data/lib/lda-model.h
CHANGED
@@ -10,12 +10,15 @@
|
|
10
10
|
|
11
11
|
#define myrand() (double) (((unsigned long) randomMT()) / 4294967296.)
|
12
12
|
#define NUM_INIT 1
|
13
|
+
#define MIN(A,B) (int)((A > B) ? (B) : (A))
|
13
14
|
|
14
15
|
void free_lda_model(lda_model*);
|
15
16
|
void save_lda_model(lda_model*, char*);
|
16
17
|
lda_model* new_lda_model(int, int);
|
17
18
|
lda_suffstats* new_lda_suffstats(lda_model* model);
|
18
19
|
void corpus_initialize_ss(lda_suffstats* ss, lda_model* model, corpus* c);
|
20
|
+
void quiet_corpus_initialize_ss(lda_suffstats* ss, lda_model* model, corpus* c);
|
21
|
+
void corpus_initialize_fixed_ss(lda_suffstats* ss, lda_model* model, corpus* c);
|
19
22
|
void random_initialize_ss(lda_suffstats* ss, lda_model* model);
|
20
23
|
void zero_initialize_ss(lda_suffstats* ss, lda_model* model);
|
21
24
|
void lda_mle(lda_model* model, lda_suffstats* ss, int estimate_alpha);
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ealdent-lda-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jason M. Adams
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2008-11-
|
13
|
+
date: 2008-11-21 00:00:00 -08:00
|
14
14
|
default_executable:
|
15
15
|
dependencies: []
|
16
16
|
|