Skip to main content

A Topic Modeling Package

Project description

Georgetown DataLab Topic Modeling package (gdtm)

A Python Package containing wrappers and classes for topic models, including Topic-Noise Discriminator (TND), (Noiseless LDA) NLDA, Guided Topic-Noise Model (GTM), dynamic topic-noise models, and Percolation-based Topic Model (PTM).

The documentation for gdtm can be found here.

Requirements and setup

to install gdtm, run
pip install gdtm

gdtm requires a version of Mallet LDA and/or the Mallet implementations of the other topic models in order to run.
You can download our stripped-down versions of Mallet LDA and the other topic model implementations in this repository. If you want the full Mallet Java package, you can find it here.

Using gdtm

There are two ways to use this package. The first is to use a topic model class, like TND. The second is to use a wrapper (based on the now deprecated Gensim Mallet wrapper), like TNDMallet. The class calls the wrapper underneath, but classes add a layer that is easier to use and interact with than that of the wrapper itself.

Wrappers would be useful when you are developing your own Mallet-based models, or when you want full control over the flow of data.
Otherwise, classes are easier to deal with. Ensembled topic models, like NLDA and its variants, are only available through a class, which calls multiple wrappers and does the ensembling in a class function.

Data set loading and structure

Data sets can be loaded in whatever way is most convenient, as long as they are passed into the gdtm functions in the same format. The format of a data set is a list of documents, where each document is a list of words.

dataset = [['doc1_word1', 'doc1, word2', ...], ['doc2_word1', 'doc2_word2', ...], ...]

We provide functions for loading data sets into our format from files. The first loads a "flat" data set.

from src.gdtm.helpers.common import load_flat_dataset

path_to_data = 'path/to/data/tweets.csv'
dataset = load_flat_dataset(path_to_data, delimiter=' ')

We also provide functions for loading and formatting temporal data sets. The following takes a file where each row is a dated document (example format: date\tw1,w2,w3,w4, where \t is a tab), loads in the data, splits the data by date into epochs, saves the "split" data set, and then loads the split data set.

from src.gdtm.helpers.common import load_dated_dataset, load_split_dataset, split_dataset_by_date, save_split_dataset, month

path_to_data = 'path/to/data/'
dataset = load_dated_dataset(path='{}dated_tweets.csv'.format(path_to_data), date_delimiter='\t', doc_delimiter=',')

split_dataset = split_dataset_by_date(dataset, epoch_function=month)
num_time_periods = len(split_dataset.keys())
save_split_dataset(path=path_to_data, file_name='split_dataset', dataset=dataset, delimiter=' ')

loaded_dataset = load_split_dataset(path=path_to_data, file_name='split_dataset', 
                                    num_time_periods=num_time_periods, delimiter=' ')

Using a class-based topic model

from src.gdtm.models import NLDA

# Set these paths to the path where you saved the Mallet implementation of each model, plus bin/mallet
tnd_path = 'path/to/mallet-tnd/bin/mallet'
lda_path = 'path/to/mallet-lda/bin/mallet'

# We pass in the paths to the java code along with the data set and whatever parameters we want to set
model = NLDA(dataset=dataset, mallet_tnd_path=tnd_path, mallet_lda_path=lda_path, 
             tnd_k=30, lda_k=30, phi=10, top_words=20)

topics = model.get_topics()
noise = model.get_noise_distribution()

Using a wrapper-based topic model

from gensim import corpora
from src.gdtm.wrappers import TNDMallet

# Set the path to the path where you saved the Mallet implementation of the model, plus bin/mallet
tnd_path = 'path/to/mallet-tnd/bin/mallet'

# Format the data set for consumption by the wrapper (this is done automatically in class-based models)
dictionary = corpora.Dictionary(dataset)
dictionary.filter_extremes()
corpus = [dictionary.doc2bow(doc) for doc in dataset]
# Pass in the path to the java code along with the data set and parameters
model = TNDMallet(tnd_path, corpus, num_topics=30, id2word=dictionary, 
                  skew=25, noise_words_max=200, iterations=1000)

topics = model.get_topics()
noise = model.load_noise_dist()

More Examples

Below, you can find examples of how to use specific models in the package.

Guided Topic-Noise Model (GTM)

from src.gdtm.models import GTM

# Set these paths to the path where you saved the Mallet implementation of each model, plus bin/mallet
tnd_path = 'path/to/mallet-tnd/bin/mallet'
gtm_path = 'path/to/mallet-gtm/bin/mallet'

seed_topics_file = 'data/seed_topics.csv'

# We pass in the paths to the java code along with the data set and whatever parameters we want to set
model = GTM(dataset=dataset, mallet_tnd_path=tnd_path, mallet_gtm_path=gtm_path, 
             tnd_k=15, gtm_k=15, phi=10, top_words=20, seed_topics_file=seed_topics_file)

Dynamic Noiseless Latent Dirichlet Allocation (DNLDA)

from src.gdtm.models import dNLDA

# Set these paths to the path where you saved the Mallet implementation of each model, plus bin/mallet
tnd_path = 'path/to/mallet-dtnd/bin/mallet'
lda_path = 'path/to/mallet-dlda/bin/mallet'

# We pass in the paths to the java code along with the data set and whatever parameters we want to set
model = dNLDA(dataset=split_dataset, mallet_tnd_path=tnd_path, mallet_lda_path=lda_path, num_time_periods=num_time_periods,
             tnd_k=30, lda_k=30, phi=10, top_words=20, save_path='results/dnlda/', run=True)

# When we provide a save path to the dynamic models, topics and noise are automatically saved for each time period.

Acknowledgements

First and foremost, we would like to thank the creators of Gensim and Mallet for writing such incredible code that made our lives so much easier when it came to implementing our own fast, accurate topic models.

This work was supported by the Massive Data Institute at Georgetown University, and by the Nation Science Foundation. We would like to thank our funders.

Referencing TND and NLDA

Churchill, Rob and Singh, Lisa. 2021. Topic-Noise Models: Modeling Topic and Noise Distributions in Social Media Post Collections. International Conference on Data Mining (ICDM).
@inproceedings{churchill2021tnd,
author = {Churchill, Rob and Singh, Lisa},
title = {Topic-Noise Models: Modeling Topic and Noise Distributions in Social Media Post Collections},
booktitle = {International Conference on Data Mining (ICDM)},
year = {2021},
}

Referencing GTM

Churchill, Rob and Singh, Lisa. 2022. A Guided Topic-Noise Model for Short Texts. The Web Conference (WWW).
@inproceedings{churchill2021gtm,
author = {Churchill, Rob and Singh, Lisa},
title = {A Guided Topic-Noise Model for Short Texts},
booktitle = {The Web Conference (WWW)},
year = {2022},
}

Referencing the Dynamic Topic-Noise Models

Churchill, Rob and Singh, Lisa. 2022. Dynamic Topic-Noise Models for Social Media. Pacific-Asia Conference on Knowledge Discovery
and Data Mining (PAKDD).
@inproceedings{churchill2021gtm,
author = {Churchill, Rob and Singh, Lisa},
title = {Dynamic Topic-Noise Models for Social Media},
booktitle = {Pacific-Asia Conference on Knowledge Discovery
and Data Mining (PAKDD)},
year = {2022},
}

References

  1. A. K. McCallum, “Mallet: A machine learning for language toolkit.” 2002.
  2. David M. Blei, Andrew Y. Ng, and Michael I. Jordan. "Latent dirichlet allocation." Journal of Machine Learning Research, 3:993–1022, 3 2003.
  3. P. Bojanowski*, E. Grave*, A. Joulin, T. Mikolov, "Enriching Word Vectors with Subword Information." In: Transactions of the Association for Computational Linguistics (TACL). 2017.
  4. R. Rehurek, P. Sojka, "Gensim–python framework for vector space modelling." 2011.
  5. R. Churchill, L. Singh, "Percolation-based topic modeling for tweets." In: KDD Workshop on Issues of Sentiment Discovery and Opinion Mining (WISDOM). 2020.
  6. R. Churchill, L. Singh, "Topic-noise models: Modeling topic and noise distributions in social media post collections." In: International Conference on Data Mining (ICDM). 2021.
  7. R. Churchill, L. Singh, "Dynamic Topic-Noise Models for Social Media." In: Pacific-Asia Conference on Knowledge Discovery and Data Mining (PAKDD). 2022.
  8. R. Churchill, L. Singh, "A Guided Topic-Noise Model for Short Texts." In: The Web Conference (WWW). 2022.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gdtm-1.0.3.tar.gz (28.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gdtm-1.0.3-py3-none-any.whl (50.2 kB view details)

Uploaded Python 3

File details

Details for the file gdtm-1.0.3.tar.gz.

File metadata

  • Download URL: gdtm-1.0.3.tar.gz
  • Upload date:
  • Size: 28.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.0 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.9

File hashes

Hashes for gdtm-1.0.3.tar.gz
Algorithm Hash digest
SHA256 f3c4df036dc92feccd4ed8fe0689fa67e1eafbc13e3fb7133e499ce370d2a41a
MD5 b9ba77ac16e0584c1a786ea7c12d7f44
BLAKE2b-256 82d72f0c91155091aefc4623a5bb116381d9c03c4f54b98f7934a14d2acf3bb2

See more details on using hashes here.

File details

Details for the file gdtm-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: gdtm-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 50.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.0 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.9

File hashes

Hashes for gdtm-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 bf552cefffbff774dfc5028a984386e9a79978c97fe3ff651152dbd6eaf50cbb
MD5 c67a96dde2c027649178273cb568bf54
BLAKE2b-256 1168cb1f46e37540cbfb5eb785ccbf15f18daeaf995264471d7948430ef58b9d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page