2
votes

I'm calculating the model-estimation of LDA with Mallet in Java and am looking for the term-topic-matrix.

Calculating the model and getting the topic-document-matrix goes well:

ParallelTopicModel model = ...;     //... estimating the model
int numTopics = model.getNumTopics();
int numDocs = model.getData().size();

// Getting the topic-probabilities
double[][] tmDist = new double[numDocs][];
for (int i = 0; i < numTopics; i++) {
        tmDist[i] = model.getTopicProbabilities(i);
}

And now I'm only able to get the top n words:

Object[][] topWords = model.getTopWords(5);
for(int i = 0; i < topWords.length; i++){
    for(int j = 0; j < topWords[i].length; j++){
        System.out.print(topWords[i][j] + " ");
    }
    System.out.println();
}

The only answers regarding that problem I only found questions/answers for this problem are regarding the command line version of Mallet.

1

1 Answers

-1
votes

This piece of code will give you the topic assignment of all the words for a particular document.

for (int topic = 0; topic < numTopics; topic++) {
            Iterator<IDSorter> iterator = topicSortedWords.get(topic).iterator();
            out = new Formatter(new StringBuilder(), Locale.US);
            out.format("%d\t%.3f\t", topic, model.getTopicProbabilities(docID)[topic]);
            int rank = 0;
            while (iterator.hasNext() && rank < 5) {
                IDSorter idCountPair = iterator.next();
                out.format("%s (%.3f) ", dataAlphabet.lookupObject(idCountPair.getID()), idCountPair.getWeight());
                rank++;
            }
            System.out.println(out);
        }

        System.out.println("\n");