A Java package for the LDA and DMM topic models
In this technical report, we present jLDADMM---an easy-to-use Java toolkit for conventional topic models. jLDADMM is released to provide alternatives for topic modeling on normal or short texts. It provides implementations of the Latent Dirichlet Allocation topic model and the one-topic-per-document Dirichlet Multinomial Mixture model (i.e. mixture of unigrams), using collapsed Gibbs sampling. In addition, jLDADMM supplies a document clustering evaluation to compare topic models. jLDADMM is open-source and available to download at: https://github.com/datquocnguyen/jLDADMMREAD FULL TEXT VIEW PDF
A Java package for the LDA and DMM topic models
jLDADMM: A Java package for the LDA and DMM topic models
Topic modeling algorithms are statistical methodologies “for analyzing documents, where a document is viewed as a collection of words, and the words in the document are viewed as being generated by an underlying set of topics” (Jordan and Mitchell, 2015).111In fact, topic models are also used for other kinds of data (Blei, 2012). However, in this report we discuss topic modeling in the context of text analysis. The probabilistic topic model Latent Dirichlet Allocation (LDA) Blei et al. (2003) is the most widely used model to discover latent topics in document collections. However, as shown in Tang et al. (2014), LDA obtains poor performance when the data presents extreme properties (e.g., very short or very few documents). That is, applying topic models to short documents, such as Tweets or instant messages, is challenging because of data sparsity and the limited contexts in such texts. One approach is to combine short texts into long pseudo-documents before training LDA (Hong and Davison, 2010; Weng et al., 2010; Mehrotra et al., 2013; Bicalho et al., 2017). Another approach is to assume that there is only one topic per document (Nigam et al., 2000; Zhao et al., 2011; Yin and Wang, 2014; Surian et al., 2016), such as in the mixture of unigrams Dirichlet Multinomial Mixture (DMM) model (Yin and Wang, 2014).
We present in this technical report jLDADMM—a Java package for the LDA and DMM topic models. jLDADMM is released to provide alternative choices for topic modeling on normal or short texts. jLDADMM provides implementations of the LDA topic model Blei et al. (2003) and the one-topic-per-document DMM Nigam et al. (2000), using the collapsed Gibbs sampling algorithms for inference as described in Griffiths and Steyvers (2004) and Yin and Wang (2014), respectively. Furthermore, jLDADMM supplies a document clustering evaluation to compare topic models, using two common metrics of Purity and normalized mutual information (NMI) Manning et al. (2008).
Our design goal is to make jLDADMM simple to setup and run. All jLDADMM components are packaged into a single file .jar. Therefore, users do not have to install external dependencies. Users can run jLDADMM from either the command-line or the Java API. The next sections will detail the usage of jLDADMM in command line, while examples of using the API are available at https://github.com/datquocnguyen/jLDADMM.
Please cite jLDADMM when it is used to produce published results or incorporated into other software. Bug reports, comments and suggestions about jLDADMM are highly appreciated. As a free open-source package, jLDADMM is distributed on an ”AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
This section describes the usage of jLDADMM in command line or terminal, using a pre-compiled file named jLDADMM.jar. Here, it is supposed that Java is already set to run in command line or terminal (e.g. adding Java to the environment variable path in Windows OS).
Users can find the pre-compiled file jLDADMM.jar and source codes in folders jar and src, respectively. Users can also recompile the source codes by simply running ant (it is also expected that ant is already installed). In addition, users can find input examples in folder test .
File format of input corpus: Similar to file corpus.txt in folder test, jLDADMM assumes that each line in the input corpus file represents a document. Here, a document is a sequence of words/tokens separated by white space characters. The users should preprocess the input corpus before training the LDA or DMM topic models, for example: down-casing, removing non-alphabetic characters and stop-words, removing words shorter than 3 characters and words appearing less than a certain times.
Now, we can train LDA or DMM by executing:
$ java [-Xmx1G] -jar jar/jLDADMM.jar -model <LDA_or_DMM> -corpus
<Input_corpus_file_path> [-ntopics <int>] [-alpha <double>] [-beta <double>] [-niters <int>] [-twords <int>] [-name <String>] [-sstep <int>]
where parameters in [ ] are optional.
-model: Specify the topic model LDA or DMM
-corpus: Specify the path to the input corpus file.
-ntopics <int>: Specify the number of topics. The default value is 20.
-niters <int>: Specify the number of Gibbs sampling iterations. The default value is 2000.
: Specify the number of the most probable topical words. The default value is 20.
-name <String>: Specify a name to the topic modeling experiment. The default value is “model”.
-sstep <int>: Specify a step to save the sampling outputs. The default value is 0 (i.e. only saving the output from the last sample).
$ java -jar jar/jLDADMM.jar -model LDA -corpus test/corpus.txt -name testLDA
The output files are saved in the same folder containing the input corpus file, in this case: the folder test. We have output files of testLDA.theta, testLDA.phi, testLDA.topWords, testLDA.topicAssignments and testLDA.paras, referring to the document-to-topic distributions, topic-to-word distributions, top topical words, topic assignments and model parameters, respectively.
Similarly, we perform:
$ java -jar jar/jLDADMM.jar -model DMM -corpus test/corpus.txt -beta 0.1 -name testDMM
Output files testDMM.theta, testDMM.phi, testDMM.topWords, testDMM.topicAssignments and testDMM.paras are also in folder test.
To infer topics on a new/unseen corpus using a pre-trained LDA/DMM topic model, we perform:
$ java -jar jar/jLDADMM.jar -model <LDAinf_or_DMMinf> -paras
<Hyperparameter_file_path> -corpus <Unseen_corpus_file_path> [-niters <int>] [-twords <int>] [-name <String>] [-sstep <int>]
-paras: Specify the path to the hyper-parameter file produced by the pre-trained LDA/DMM topic model.
$ java -jar jar/jLDADMM.jar -model LDAinf -paras test/testLDA.paras -corpus test/unseenTest.txt -niters 100 -name testLDAinf
$ java -jar jar/jLDADMM.jar -model DMMinf -paras test/testDMM.paras -corpus test/unseenTest.txt -niters 100 -name testDMMinf
We treat each topic as a cluster, and we assign every document the topic with the highest probability given the document Lu et al. (2011). To get the Purity and NMI clustering scores, we perform:
$ java -jar jar/jLDADMM.jar -model Eval -label <Golden_label_file_path> -dir <Directory_path> -prob <Document-topic-prob/Suffix>
-label: Specify the path to the ground truth label file. Each line in this label file contains the golden label of the corresponding document in the input corpus. See files corpus.LABEL and corpus.txt in folder test.
-dir: Specify the path to the directory containing document-to-topic distribution files.
-prob: Specify a document-to-topic distribution file or a group of document-to-topic distribution files in the specified directory.
$ java -jar jar/jLDADMM.jar -model Eval -label test/corpus.LABEL -dir test -prob testLDA.theta
$ java -jar jar/jLDADMM.jar -model Eval -label test/corpus.LABEL -dir test -prob testDMM.theta
The above commands will produce the clustering scores for files testLDA.theta and testDMM.theta in folder test, separately.
The following command:
$ java -jar jar/jLDADMM.jar -model Eval -label test/corpus.LABEL -dir test -prob theta
will produce the clustering scores for all document-to-topic distribution files with their names ending in theta. In this case, they are testLDA.theta and testDMM.theta. It also provides the mean and standard deviation of the scores.
Blei, D. M., Ng, A. Y., Jordan, M. I., 2003. Latent Dirichlet Allocation. Journal of Machine Learning Research 3, 993–1022.