tmtoolkit: Text mining and topic modeling toolkit
tmtoolkit is a set of tools for text mining and topic modeling with Python developed especially for the use in the social sciences. It aims for easy installation, extensive documentation and a clear programming interface while offering good performance on large datasets by the means of vectorized operations (via NumPy) and parallel computation (using Python’s multiprocessing module). It combines several known and well-tested packages such as SpaCy and SciPy.
At the moment, tmtoolkit focuses on methods around the Bag-of-words model, but word vectors (word embeddings) can also be generated.
The documentation for tmtoolkit is available on tmtoolkit.readthedocs.org and the GitHub code repository is on github.com/WZBSocialScienceCenter/tmtoolkit.
Features
Text preprocessing
tmtoolkit implements or provides convenient wrappers for several preprocessing methods, including:
tokenization and part-of-speech (POS) tagging (via SpaCy)
extensive pattern matching capabilities (exact matching, regular expressions or “glob” patterns) to be used in many methods of the package, e.g. for filtering on token, document or document label level, or for keywords-in-context (KWIC)
adding and managing custom token metadata
accessing word vectors (word embeddings)
generating n-grams
generating sparse document-term matrices
expanding compound words and “gluing” of specified subsequent tokens, e.g.
["White", "House"]
becomes["White_House"]
All text preprocessing methods can operate in parallel to speed up computations with large datasets.
Topic modeling
model computation in parallel for different copora and/or parameter sets
support for lda, scikit-learn and gensim topic modeling backends
evaluation of topic models (e.g. in order to an optimal number of topics for a given dataset) using several implemented metrics:
model coherence (Mimno et al. 2011) or with metrics implemented in Gensim)
KL divergence method (Arun et al. 2010)
probability of held-out documents (Wallach et al. 2009)
pair-wise cosine distance method (Cao Juan et al. 2009)
harmonic mean method (Griffiths, Steyvers 2004)
the loglikelihood or perplexity methods natively implemented in lda, sklearn or gensim
common statistics for topic models such as word saliency and distinctiveness (Chuang et al. 2012), topic-word relevance (Sievert and Shirley 2014)
export estimated document-topic and topic-word distributions to Excel
visualize topic-word distributions and document-topic distributions as word clouds or heatmaps
model coherence (Mimno et al. 2011) for individual topics
integrate PyLDAVis to visualize results
Other features
Limits
all languages are supported, for which SpaCy language models are available
all data must reside in memory, i.e. no streaming of large data from the hard disk (which for example Gensim supports)
Built-in datasets
Currently tmtoolkit comes with the following built-in datasets which can be loaded via
tmtoolkit.corpus.Corpus.from_builtin_corpus()
:
“en-NewsArticles”: News Articles (Dai, Tianru, 2017, “News Articles”, https://doi.org/10.7910/DVN/GMFCTR, Harvard Dataverse, V1)
random samples from ParlSpeech V2 (Rauh, Christian; Schwalbach, Jan, 2020, “The ParlSpeech V2 data set: Full-text corpora of 6.3 million parliamentary speeches in the key legislative chambers of nine representative democracies”, https://doi.org/10.7910/DVN/L4OAKN, Harvard Dataverse) for different languages:
“de-parlspeech-v2-sample-bundestag”
“en-parlspeech-v2-sample-houseofcommons”
“es-parlspeech-v2-sample-congreso”
“nl-parlspeech-v2-sample-tweedekamer”
About this documentation
This documentation guides you in several chapters from installing tmtoolkit to its specific use cases and shows some examples with built-in corpora and other datasets. All “hands on” chapters from Getting started to Topic modeling are generated from Jupyter Notebooks. If you want to follow along using these notebooks, you can download them from the GitHub repository.
There are also a few other examples as plain Python scripts available in the examples folder of the GitHub repository.
License
Code licensed under Apache License 2.0. See LICENSE file.
- Installation
- Getting started
- Working with text corpora
- Text preprocessing
- Parallel processing with the TMPreproc class
- Loading example data
- Optional: enabling logging output
- Creating a
TMPreproc
object - Accessing tokens, vocabulary and other important properties
- Part-of-Speech (POS) tagging
- Aside: TMPreproc as “state machine”
- Lemmatization and term normalization
- Keywords-in-context (KWIC) and general filtering methods
- Working with token metadata
- Generating n-grams
- Generating a sparse document-term matrix (DTM)
- Serialization: Saving and loading
TMPreproc
objects
- Functional API
- Parallel processing with the TMPreproc class
- Working with the Bag-of-Words representation
- Topic modeling
- API
- tmtoolkit.bow
- tmtoolkit.corpus
- tmtoolkit.preprocess
- tmtoolkit.topicmod
- Evaluation metrics for Topic Modeling
- Printing, importing and exporting topic model results
- Statistics for topic models and BoW matrices
- Parallel model fitting and evaluation with lda
- Parallel model fitting and evaluation with scikit-learn
- Parallel model fitting and evaluation with Gensim
- Visualize topic models and topic model evaluation results
- Base classes for parallel model fitting and evaluation
- tmtoolkit.utils
- Version history
- 0.10.0 - 2020-08-03
- 0.9.0 - 2019-12-20
- 0.8.0 - 2019-02-05
- 0.7.3 - 2018-09-17 (last release to support Python 2.7)
- 0.7.2 - 2018-07-23
- 0.7.1 - 2018-06-18
- 0.7.0 - 2018-06-18
- 0.6.3 - 2018-06-01
- 0.6.2 - 2018-04-27
- 0.6.1 - 2018-04-27
- 0.6.0 - 2018-04-25
- 0.5.0 - 2018-02-13
- 0.4.2 - 2018-02-06
- 0.4.1 - 2018-01-24
- 0.4.0 - 2018-01-18