## [1] "Loading session data. If this is the first time you are working on this session, this might take a while. Please, do not disconnect from the Internet."

We have seen in the previous session that words are linked together. But how are these words linked together? Wouldn’t it be nice if we could compare not just words but whole topics across documents in a collection? This is what the advanced technique topic modelling does. Topic Modelling is a popular technique in social and cultural analytics that summarises a collection of texts into a predefined number of topics. Have a look at http://journalofdigitalhumanities.org/2-1/topic-modeling-and-digital-humanities-by-david-m-blei/.

Topic modelling is also popular, as it requires only minimal text organisation. Computers can learn topics by themselves. There are, however, known limitations of topic models with regard to the interpretation they help with. There is no guarantee that the automatically derived topics will correspond to what people would consider to be interesting topics/themes. They may be too specific or general, identical to other topics or they may be framings of larger topics, as opposed to genuinely distinct topics. Finally (and in common with other computational analysis techniques), the performance of topic modelling depends upon the quality and structure of the data. In our case the main issue will be that we only have 2 documents, which is not a lot of data. But as topic modelling is computationally quite expensive we should not overdo things here and just concentrate on this small corpus. Please, load the topic modelling library with library(topicmodels).

library(topicmodels)

For topic modelling, we have to define the total number of topics in advance. Please, enter n<-10 to set 10 topics.

n<-10

The actual topic modelling function is called LDA (Latent Dirichlet Allocation). It is really quite complicated but well explained in the reference above. It tries to define our n topics based on the most important words in each of them. A more detailed explanation can be found at http://blog.echen.me/2011/08/22/introduction-to-latent-dirichlet-allocation/. Now run LDA with topic_model <- LDA(dtm, n, alpha=0.1, method=‘Gibbs’). This is advanced text analysis and we can ignore the arguments again. It should take a few minutes.

topic_model <- LDA(dtm, n, alpha=0.1, method='Gibbs')

We are now interested in the words/terms per topic and get those with terms(topic_model, 10). The parameter 10 indicates that we display 10 terms per topics.

terms(topic_model, 10)
##       Topic 1         Topic 2      Topic 3    Topic 4    Topic 5
##  [1,] "concentration" "jews"       "children" "vienna"   "jewish"
##  [2,] "now"           "jewish"     "years"    "dachau"   "november"
##  [3,] "buchenwald"    "german"     "wife"     "arrested" "synagogue"
##  [4,] "sachsenhausen" "germany"    "two"      "reporter" "also"
##  [5,] "camps"         "emigration" "old"      "jews"     "arrested"
##  [6,] "pol"           "will"       "taken"    "report"   "jews"
##  [7,] "death"         "can"        "died"     "herr"     "taken"
##  [8,] "june"          "gestapo"    "herr"     "jewish"   "fire"
##  [9,] "life"          "one"        "father"   "police"   "police"
## [10,] "camp"          "etc"        "november" "november" "berlin"
##       Topic 6  Topic 7    Topic 8   Topic 9     Topic 10
##  [1,] "men"    "will"     "one"     "camp"      "people"
##  [2,] "one"    "now"      "also"    "prisoners" "house"
##  [3,] "man"    "much"     "people"  "work"      "homes"
##  [4,] "oclock" "dear"     "time"    "one"       "destroyed"
##  [5,] "two"    "received" "however" "day"       "home"
##  [6,] "hours"  "holland"  "days"    "barracks"  "street"
##  [7,] "taken"  "letter"   "first"   "people"    "everything"
##  [8,] "people" "can"      "later"   "number"    "women"
##  [9,] "jews"   "know"     "course"  "prisoner"  "smashed"
## [10,] "three"  "tante"    "end"     "appell"    "men"

Anything interesting to see here? The next step would be to label the topics in order to interpret them. Please, take a moment to do just that. We can also see what the topics per document are with topics(topic_model, 10). 10 is the maximum number of topics returned.

topics(topic_model, 10)
##        1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
##  [1,]  5  3  3  5 10 10  3  6  3 10 10  3  3  2  3 10  3  3 10  2  3  3  6
##  [2,]  2  5 10 10  4  8  6  9  5  3  6 10 10  7  5  5 10  1  3  3  8  6  8
##  [3,]  8 10  6  3  2  5  5  8  4  5  8  5  8  3 10  1  2 10  5  1 10  5  2
##       24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
##  [1,]  4  6  8  5  4  2  6 10 10  2  5  1  5  7  3  5  5  5  5  5  7  1  4
##  [2,]  5  7  5 10  3  7 10  6  3  3  2  4  3  5  1  2  2  9 10 10  2  4 10
##  [3,]  8  8  2  3 10  8  5  5  5  8  7  5  6  2  2  4  6  6  2  3  8  5  2
##       47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
##  [1,] 10 10  3  6  6 10  6  9  6  2  3 10  6  6  8  5  6  5 10  5 10  6  2
##  [2,]  5  5  5  8 10  5  9  2  3  5 10  3  9  9  5  2  8  2  5 10  3  3  8
##  [3,]  9  4  2  9  3  2  8  7  9  4  5  6  8  8  2  7  9  1  2  1  6  5 10
##       70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
##  [1,]  9  5  8  9 10 10 10  4  7 10 10  8  7  2  3  3 10  3  3  3 10  8 10
##  [2,]  6  4  5  6  6  6  6  5  8  8  8  7  3  7 10  2  2  7  5  5  6  3  5
##  [3,]  4  8 10  5  5  5  3  3  1  6  6  2  6 10  7  7  5  8 10  7  8  2  8
##       93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
##  [1,]  6  4  4  4  2  4  4   3   5  10   5  10  10   7   2   2   3  10   6
##  [2,]  8  5 10  2  5  2  5   6  10   5  10   6   5   8   4  10   4   3   8
##  [3,]  4 10  5 10  4 10  2   8   2   8   2   1   6   6   8   7   6   7   2
##       112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
##  [1,]   4   6   6   6   6   4   5   4   2   7   9   4  10  10  10  10   6
##  [2,]   8   4   4   9   9   5  10   5   5   3   6   5   5   8   6   5   4
##  [3,]   9   8   3   8   8   8   9   2   4   8   2   8   8   2   8   6   8
##       129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
##  [1,]   3   3  10   6   3   6   3   4   1   3   5   4   6   6   3   4   4
##  [2,]   4   4   5   9   5   2   4   3   7   7   2   6   9   9   7   3   6
##  [3,]   8   8   8   8   4   8   6   6   5   1   1   8   8   2   8   5   9
##       146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
##  [1,]   7   6  10   9   2   6   2   3   4   4  10   9   6   9   6   5   2
##  [2,]   2   3   6   6   5   4   8   1   8   6   5   6   9   6   2   4   8
##  [3,]   3   5   8   8   4  10  10   4  10  10   3   8   8   1   8   3   4
##       163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
##  [1,]   6  10   2   9   2   6   4   2   5   6   2   8   9   9   5   3  10
##  [2,]   8   2   9   6   9   8   2   8   4   9   8   6   4   6   3   1   5
##  [3,]   3   8   6   8   1   9   8  10   3   8   9   9   8   4  10   2   8
##       180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
##  [1,]   6   2   6   6   4  10   3   4   4   3   5   3   3  10  10   6   7
##  [2,]   9   6   9   4   2   6   5   3   1  10  10   5   8   8   3   3   8
##  [3,]   8   8   8   8   5   8   4   1   7   6   2   8   5   6   2  10   9
##       197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
##  [1,]   7   3   3   7   3   7   3   3   3   3   3   2   3   3   7   3   7
##  [2,]   2   8   7   2   8   3   5   7   8   2   7   7   8   5   3   7   3
##  [3,]   3   7   5   8   4   4   2   8   1   4   4   3   2   7   8   8   2
##       214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
##  [1,]   4   3   3   3   3   3   3   3   3   3   3   5   9   6   2   1   3
##  [2,]   2   8   7   7   5   8   5   7   8  10   7   6   6   9   8   9   1
##  [3,]   3   5   4   1  10   6  10   8   5   8   8   1   4   8  10   3   5
##       231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
##  [1,]   6   9   6   8   6   2   7   3   2   3   6   6   9   5   5   6   9
##  [2,]   8   6   9   7   9   4   8   1   8   8   7   8   8   6  10   8   1
##  [3,]   9   8   8  10   8   8   3   2   7   1   3   3   6  10   8  10   6
##       248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
##  [1,]   2   2   2   5   5   2   2   2   2   2   9  10   6   8   5   5   6
##  [2,]   8   8   1   2  10   4   1   6   5   4   8   5  10   6  10  10   8
##  [3,]  10   5   5   8   1   9   5   8   4   1   6   6   3   7   1   2   3
##       265
##  [1,]   5
##  [2,]   3
##  [3,]  10
##  [ reached getOption("max.print") -- omitted 7 rows ]

Let’s move on to something different. Have you heard about the Google Ngram Viewer, which plots frequencies of words using a yearly count in sources printed between 1500 and 2008 in Google’s book corpora? It has created quite an excitement in the digital methods world. Check out http://firstmonday.org/ojs/index.php/fm/article/view/5567/5535. There are many examples and some strong believers. You can try Google’s Ngram Viewer under https://books.google.com/ngrams.

Would it not be nice to use R to download all this data and then do more advanced work with it. There is a package for that, with which we can avoid complicated API details. Load the package with library(ngramr).

library(ngramr)

Let’s see how the word use over time of hacker vs programmer compares. Run ng <- ngram(c(‘hacker’, ‘programmer’), year_start = 1950). It uses the function ngram to connect to Google and download the data since 1950.

ng  <- ngram(c('hacker', 'programmer'), year_start = 1950)

Check it out with head(ng).

head(ng)
## Phrases: hacker, programmer
## Case-sentitive: TRUE
## Corpuses: eng_2012
## Smoothing: 3
##
##   Year Phrase Frequency    Corpus
## 1 1950 hacker 9.493039e-09 eng_2012
## 2 1951 hacker 1.168549e-08 eng_2012
## 3 1952 hacker 1.078450e-08 eng_2012
## 4 1953 hacker 1.010847e-08 eng_2012
## 5 1954 hacker 9.678727e-09 eng_2012
## 6 1955 hacker 9.315688e-09 eng_2012

Using ggplot, we can produce a nice graph with ng. Try ggplot(ng, aes(x=Year, y=Frequency, colour=Phrase)) + geom_line(). After the next session, we will understand better how, but it should also be self-explanatory.

ggplot(ng, aes(x=Year, y=Frequency, colour=Phrase)) + geom_line()