2

我正在用 Java 中的Mallet计算LDA的模型估计,并正在寻找term-topic-matrix

计算模型并获得主题文档矩阵顺利:

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);
}

现在我只能得到前n 个单词:

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();
}

关于这个问题的唯一答案我只找到了这个问题的问题/答案是关于 Mallet 的命令行版本。

4

1 回答 1

-1

这段代码将为您提供特定文档的所有单词的主题分配。

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");
于 2015-01-15T14:03:55.640 回答