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 directly using pip:

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.2.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.2-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: topic_specificity-0.1.2.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.2.tar.gz
Algorithm Hash digest
SHA256 1e3ff01448de88c3f8dce75ed9e6e8df72c362f90de182c994726b6ce4446674
MD5 36eddbbe8b56a7641560e7b7aead82b9
BLAKE2b-256 8ccac4a8a2c209103c978bf44d3861f7dce89e208cf0b1a1feeab56b7aab5390

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for topic_specificity-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f64bb428736bb96d3c365a8f79f837ef3ad35eb6b2f6a6940b4f6700021b9a35
MD5 14796c61e08f5c3e4f9cfd536093509b
BLAKE2b-256 505dd9d82f8fbc872c06978a85ac36edb6af2b957b368ed54123902ef4cca35e

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