Sub topic 8
Judging Topics: Coherence and Manual Inspection
Assume you know Python, tokenisation, stop-word removal, bag-of-words counts, and the basic idea of probability distributions. This chapter also assumes that you can train an LDA or BERTopic model. Evaluation tells you whether the discovered topics are useful, interpretable, and suitable for your task.
Topic coherence
Topic coherence measures how often the top words in a topic are related in the source texts or in an external corpus. A topic containing government, election, vote, and policy will usually score better than one containing unrelated words.
Several coherence measures exist:
| Measure | Main signal | Typical use |
|---|---|---|
| `c_v` | Word co-occurrence and semantic similarity | Human-readable topic comparisons |
| `u_mass` | Co-occurrence within the training corpus | Fast, model-development checks |
| `c_npmi` | Normalised word association | Detailed comparisons with enough text |
The score is useful for comparing models trained on the same corpus. It is not a universal quality score. A high score can reward common words, while a lower score can represent a rare but valuable topic.
from gensim import corpora
from gensim.models import LdaModel, CoherenceModel
docs = [
["goal", "match", "player", "team", "season"],
["election", "vote", "party", "government", "policy"],
["team", "coach", "league", "season", "win"],
["minister", "government", "vote", "law", "policy"],
]
dictionary = corpora.Dictionary(docs)
corpus = [dictionary.doc2bow(doc) for doc in docs]
model = LdaModel(
corpus=corpus,
id2word=dictionary,
num_topics=2,
passes=10,
random_state=42,
)
coherence = CoherenceModel(
model=model,
texts=docs,
dictionary=dictionary,
coherence="c_v",
).get_coherence()
print("c_v coherence:", coherence)
for topic_id, words in model.print_topics(num_words=5):
print(topic_id, words)Manual inspection
Read the top words for every topic and give each topic a short label. Then inspect documents with the highest probability for that topic. This checks whether the words form a meaningful theme and whether the theme appears in real documents.
For LDA, a document can belong to several topics. Inspect both single-topic documents and mixed documents. For BERTopic, inspect representative documents and the words produced by c-TF-IDF. Look for topics that are duplicates, overly broad, dominated by names, or made mostly of generic terms.
Useful checks include:
- Can you describe the topic without listing its words?
- Do its top words commonly occur together?
- Do representative documents match the label?
- Are important topics missing?
- Are two topics expressing the same theme?
- Does the topic support the intended task?
Compare several topic counts or model settings. Record coherence, topic labels, duplicate topics, and representative documents. Prefer a model whose topics are stable and useful, not merely one with a slightly better numeric score.
Exercise
Why should you inspect representative documents after calculating topic coherence?
Show answer
Coherence evaluates relationships among top words, but representative documents show whether those words describe a real and useful theme in context.