Last updated: Oct 26, 2026

DHG 502 syllabus

Week 7 — Topic Modeling

1. Recap

A topic model finds groups of words that tend to occur together across a collection of documents. LDA assumes that each topic is a distribution over words and each document is a mixture of topics. The model does not know what the topics are: we choose K, then name and interpret the topics ourselves. For historians the useful outputs are the topics and the topic proportions per document (for example, per year), which tell us where to read closely.

2. Exercise: topics in Mao Zedong’s Selected Works

Each text in the file starts with a title, then a date line like (一九二五年十二月一日). Footnote markers such as 〔12〕 and the numbered editorial notes were added by the editors, so we remove them. Open your repo in your AI coding assistant and paste Prompt 1.

Prompt 1

Download the raw text of https://github.com/mcjkurz/qhchina-data/blob/main/corpora/选集/毛泽东选集.txt and run pip install qhchina jieba.

Split it into one document per text. Each text starts with a title, followed by a date line such as (一九二五年十二月一日); record the title and the year (the zero may be written 〇 or ○). Remove footnotes: lines starting with 〔12〕-style markers, and such markers inside the text. Tell me how many documents you found.

Tokenize with jieba, keep words of 2+ characters, remove stopwords with qhchina's load_stopwords("zh_sim"), and drop documents with fewer than 100 tokens.

Train qhchina.analytics.topicmodels.LDAGibbsSampler (10 topics, iterations=200, random_state=42, min_word_count=5). For each topic, print the top 10 words and the 2 most typical documents (title and year).

Read the topics: which can you name? Open the typical documents of one topic and check your name against the text. Then paste Prompt 2:

Prompt 2

For each decade, compute the average topic proportion over its documents, and plot it as a stacked bar chart (one color per topic; the legend shows each topic's number and top 3 words). Make sure Chinese displays correctly (see qhchina.helpers for fonts). Save it as topics_by_decade.png.

3. Discuss

Remember: a topic is a pattern of co-occurring words, not an argument. The results depend on our choices (document unit, stopwords, K), and this is an edited, officially curated collection. Use the topics to pick texts for close reading.

Readings