lib/lda-inference.c in ealdent-lda-ruby-0.1.6 vs lib/lda-inference.c in ealdent-lda-ruby-0.1.7

- old
+ new

@@ -835,31 +835,48 @@ } return arr; } + +/* + * Compute the phi values by running inference after the initial EM run has been completed. + * + * Returns a 3D matrix: <tt>num_docs x length x num_topics</tt>. + */ static VALUE wrap_get_phi(VALUE self) { if (!model_loaded) return Qnil; - VALUE arr; - int i = 0, j = 0; + VALUE arr = rb_ary_new2(last_corpus->num_docs); + int i = 0, j = 0, k = 0; + int max_length = max_corpus_length(last_corpus); - - arr = rb_ary_new2(max_length); - for (i = 0; i < max_length; i++) { - VALUE arr2 = rb_ary_new2(last_model->num_topics); - for (j = 0; j < last_model->num_topics; j++) { - rb_ary_store(arr2, j, rb_float_new(last_phi[i][j])); + for (i = 0; i < last_corpus->num_docs; i++) { + VALUE arr1 = rb_ary_new2(last_corpus->docs[i].length); + + lda_inference(&(last_corpus->docs[i]), last_model, last_gamma[i], last_phi); + + for (j = 0; j < last_corpus->docs[i].length; j++) { + VALUE arr2 = rb_ary_new2(last_model->num_topics); + + for (k = 0; k < last_model->num_topics; k++) { + rb_ary_store(arr2, k, rb_float_new(last_phi[j][k])); + } + + rb_ary_store(arr1, j, arr2); } - rb_ary_store(arr, i, arr2); + + rb_ary_store(arr, i, arr1); } return arr; } + + /* * Get the beta matrix after the model has been run. */ static VALUE wrap_get_model_beta(VALUE self) { if (!model_loaded) @@ -961,10 +978,10 @@ rb_define_method(rb_cLda, "verbose=", wrap_set_verbosity, 1); // retrieve model and gamma rb_define_method(rb_cLda, "beta", wrap_get_model_beta, 0); rb_define_method(rb_cLda, "gamma", wrap_get_gamma, 0); - rb_define_method(rb_cLda, "phi", wrap_get_phi, 0); + rb_define_method(rb_cLda, "compute_phi", wrap_get_phi, 0); rb_define_method(rb_cLda, "model", wrap_get_model_settings, 0); } #endif \ No newline at end of file