Introduction to Topic Modelling in R and Python

Workshops for Ukraine

Dr. Christian S. Czymara

10/19/23

Before we begin

  • Open this Colab and run the code for “Install packages”
  • Then, open this Colab and run the code for “Install BERTopic library”

Agenda

  • What are Topic Models and what do you use them for?
  • Pre-processing
  • How do Topic Models work?
  • Advantages and limitations of Topic Models
  • Exercise: Topics in news articles

Basics

Logic of topic models

  • Topic Modeling is an algorithm for finding the most important themes (topics) in large text collections (corpus).
  • Little prior knowledge about the content needed
  • What (i.e. which topics) is written about?
  • Which topics are addressed particularly frequently?
  • Do the texts differ systematically in their content (between different persons, newspapers, over time, …)?

But first: Data preparation (preprocessing)

National anthem of Ukraine

  • First of all, the texts have to be broken down into their components (words, punctuation marks, etc.).
  • Example: The national anthem of Ukraine (translated lyrics according to Wikipedia)
Ukraine's glory and freedom have not yet perished,
Still upon us, young brothers, fate shall smile.
Our enemies shall vanish, like dew in the sun.
We too shall rule, brothers, in our country.
  • Let’s assume that each line is a single document

  • Corpus (text collection) with four documents

Vocabulary

  • The texts are divided into their components
dok1 <- "Ukraine's glory and freedom have not yet perished,"

dok2 <- "Still upon us, young brothers, fate shall smile."

dok3 <- "Our enemies shall vanish, like dew in the sun."

dok4 <- "We too shall rule, brothers, in our country."

UA_anthem <- cbind(dok1, dok2, dok3, dok4)

corp_UA_anthem <- corpus(UA_anthem)
toks_UA_anthem <- tokens(corp_UA_anthem)

Vocabulary

toks_UA_anthem
Tokens consisting of 4 documents.
text1 :
[1] "Ukraine's" "glory"     "and"       "freedom"   "have"      "not"      
[7] "yet"       "perished"  ","        

text2 :
 [1] "Still"    "upon"     "us"       ","        "young"    "brothers"
 [7] ","        "fate"     "shall"    "smile"    "."       

text3 :
 [1] "Our"     "enemies" "shall"   "vanish"  ","       "like"    "dew"    
 [8] "in"      "the"     "sun"     "."      

text4 :
 [1] "We"       "too"      "shall"    "rule"     ","        "brothers"
 [7] ","        "in"       "our"      "country"  "."       

Document-Feature-Matrix

  • What we need is a table (matrix)
  • … in which the texts (documents) are in the rows
  • … the words, characters, etc. (features) are in the columns
  • … and in the cells counts how often a feature occurs in the respective text
dfm_UA_anthem <- dfm(toks_UA_anthem)
dfm_UA_anthem
Document-feature matrix of: 4 documents, 30 features (66.67% sparse) and 0 docvars.
       features
docs    ukraine's glory and freedom have not yet perished , still
  text1         1     1   1       1    1   1   1        1 1     0
  text2         0     0   0       0    0   0   0        0 2     1
  text3         0     0   0       0    0   0   0        0 1     0
  text4         0     0   0       0    0   0   0        0 2     0
[ reached max_nfeat ... 20 more features ]

Almost done

  • Better: Keep only actual words as features (no numbers, punctuation, etc.)
toks_UA_anthem_2 <- tokens(corp_UA_anthem,
                           remove_punct = T,
                           remove_numbers = T,
                           remove_symbols = T,
                           remove_separators = T
                           )
  • Remove stop words (common words without real meaning)
toks_UA_anthem_2 <-  tokens_remove(toks_UA_anthem_2,
                                   stopwords(),
                                   case_insensitive = TRUE
                                   )

Almost done

  • Reduce words to their stem (stemming), e.g. “programming”, “programs, and”programmed” all become “program”
toks_UA_anthem_2 <- tokens_wordstem(toks_UA_anthem_2)
  • Create DFM
dfm_UA_anthem_2 <- dfm(toks_UA_anthem_2)

dfm_UA_anthem_2
Document-feature matrix of: 4 documents, 20 features (71.25% sparse) and 0 docvars.
       features
docs    ukrain glori freedom yet perish still upon us young brother
  text1      1     1       1   1      1     0    0  0     0       0
  text2      0     0       0   0      0     1    1  1     1       1
  text3      0     0       0   0      0     0    0  0     0       0
  text4      0     0       0   0      0     0    0  0     0       1
[ reached max_nfeat ... 10 more features ]

Topic Models

Assumptions

  • Each word contributes equally to the text (bag-of-words assumption)
  • Mixed membership models: Each text consists of a mixture of different topics (with different proportions)
  • Texts that discuss similar topics use similar words
  • In other words, certain topics contain some words more than others

The algorithm

  • Two steps:
  • Finding out which words occur together
  • Checking how these words are distributed among the texts
  • Unsupervised machine learning, since no other info needs to be present

The algorithm

  • Crucial question: How many topics should be found?
  • Iterative process designed to maximize two goals simultaneously:
  • Words that occur together frequently are more likely to belong to the same topic
  • Words in the same document are more likely to belong to the same topic

Topic Models in action