Skip to main content

Calculate topic specificity for LDA, LSA & HDP.

Project description

Topic Specificity

Topic Specificity is a measure for evaluating the concentration and distinctness of topics within a document corpus. It quantifies how well each topic stands out from the background noise by comparing document-level topic weights against a corpus-wide threshold. A higher specificity score indicates a topic that is more clearly defined and less diffuse across documents.

This package implements the specificity measure described in:

Yuan, M., Lin, P., Rashidi, L., & Zobel, J. (2023). “Assessment of the Quality of Topic Models for Information Retrieval Applications.” Proceedings of the 9th ACM SIGIR International Conference on the Theory of Information Retrieval (ICTIR ’23), 265–274. DOI: 10.1145/3578337.3605118


Installation

You can install this package locally in editable (development) mode:

pip install -e .

Or, once it is published on PyPI, install it directly:

pip install topic-specificity

Usage Examples

Below are some quick examples showing how to calculate topic specificity scores and Z-scores.

1. Calculate Specificity for All Topics

from topic_specificity import calculate_specificity_for_all_topics
from gensim.models.ldamodel import LdaModel
from gensim.corpora.dictionary import Dictionary

# — assume you have preprocessed texts —
texts = [["human", "interface", "computer"], ["survey", "user", "computer", "system"]]
dictionary = Dictionary(texts)
corpus = [dictionary.doc2bow(text) for text in texts]

# Train a simple LDA model
lda = LdaModel(corpus=corpus, id2word=dictionary, num_topics=2, random_state=42)

# Compute specificity scores
spec_scores = calculate_specificity_for_all_topics(
    model=lda,
    corpus=corpus,
    mode='lda',
    threshold_mode='gmm',         # options: 'median', 'percentile', 'gmm'
    specificity_mode='sqrt'       # options: 'diff', 'sqrt'
)

print("Specificity scores:", spec_scores)

2. Calculate Z-Scores (Effect Sizes)

from topic_specificity import calculate_Zi_scores

# Using the same LDA model & corpus as above:
z_scores = calculate_Zi_scores(
    model=lda,
    corpus=corpus,
    mode='lda'
)

print("Z-scores per topic:", z_scores)

3. LSA or HDP Models

The same functions work for LSA or HDP--just change the mode:

from gensim.models.lsimodel import LsiModel
from gensim.models.hdpmodel import HdpModel

# Example for LSA:
lsa = LsiModel(corpus=corpus, id2word=dictionary, num_topics=2)
lsa_scores = calculate_specificity_for_all_topics(lsa, corpus, mode='lsa',
                                                  threshold_mode='median',
                                                  specificity_mode='diff')
print("LSA specificity:", lsa_scores)

# Example for HDP:
hdp = HdpModel(corpus=corpus, id2word=dictionary)
hdp_scores = calculate_specificity_for_all_topics(hdp, corpus, mode='hdp',
                                                  threshold_mode='percentile',
                                                  specificity_mode='sqrt')
print("HDP specificity:", hdp_scores)

Citation

If you use topic_specificity in your work, please cite:

@inproceedings{yuan2023assessment,
  author    = {Yuan, Meng and Lin, Pauline and Rashidi, Lida and Zobel, Justin},
  title     = {Assessment of the Quality of Topic Models for Information Retrieval Applications},
  booktitle = {Proceedings of the 9th ACM SIGIR International Conference on the Theory of Information Retrieval (ICTIR ’23)},
  year      = {2023},
  location  = {Taipei, Taiwan},
  pages     = {265--274},
  doi       = {10.1145/3578337.3605118},
}

DOI: https://doi.org/10.1145/3578337.3605118

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

topic_specificity-0.1.1.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

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

topic_specificity-0.1.1-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file topic_specificity-0.1.1.tar.gz.

File metadata

  • Download URL: topic_specificity-0.1.1.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for topic_specificity-0.1.1.tar.gz
Algorithm Hash digest
SHA256 ef43d3c91aa0511e2ab9928c386a1b195e3bb8a3c47b133f33e680c42f0f7764
MD5 110e1d7c98dc9e6202f558dd189d434b
BLAKE2b-256 60a3cbf787b750bacea68991a19104922be80a295d9e4acb5ec80d0b396bf873

See more details on using hashes here.

File details

Details for the file topic_specificity-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for topic_specificity-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b55db80afa391f3bfef90b33ffc4309b8629f36ff36c5b43a5cad455b7a6a9d8
MD5 f703e8dc2a7f356e08bf248ce4aa564f
BLAKE2b-256 78066d69c2084e3acde3ae9ca64224181cfdacf33fe3286d1d23cd03f71acbcd

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