library(quanteda)

This is intended to show how quanteda can be used with the text2vec package in order to replicate its gloVe example.

Download the corpus

Download a corpus comprised of the texts used in the text2vec vignette:

wiki_corp <- quanteda.corpora::download(url = 'https://www.dropbox.com/s/9mubqwpgls3qi9t/data_corpus_wiki.rds?dl=1')

Select features

First, we tokenize the corpus, and then get the names of the features that occur five times or more. Trimming the features before constructing the fcm:

wiki_toks <- tokens(wiki_corp)
feats <- dfm(wiki_toks, verbose = TRUE) %>% 
    dfm_trim(min_count = 5) %>%
    featnames()
## Creating a dfm from a tokens input...
##    ... lowercasing
##    ... found 1 document, 253,853 features
##    ... created a 1 x 253,853 sparse dfm
##    ... complete. 
## Elapsed time: 1.17 seconds.
# leave the pads so that non-adjacent words will not become adjacent
wiki_toks <- tokens_select(wiki_toks, feats, padding = TRUE)

Construct the feature co-occurrence matrix

wiki_fcm <- fcm(wiki_toks, context = "window", count = "weighted", weights = 1 / (1:5), tri = TRUE)

Fit word embedding model

Fit the GloVe model using text2vec.

library(text2vec)

GloVe is an unsupervised learning algorithm for obtaining vector representations for words. Training is performed on aggregated global word-word co-occurrence statistics from a corpus, and the resulting representations showcase interesting linear substructures of the word vector space.

GloVe encodes the ratios of word-word co-occurrence probabilities, which is thought to represent some crude form of meaning associated with the abstract concept of the word, as vector difference. The training objective of GloVe is to learn word vectors such that their dot product equals the logarithm of the words’ probability of co-occurrence.

glove <- GlobalVectors$new(word_vectors_size = 50, vocabulary = featnames(wiki_fcm), x_max = 10)
wiki_main <- fit_transform(wiki_fcm, glove, n_iter = 20)
## INFO [2017-12-07 15:39:54] 2017-12-07 15:39:54 - epoch 1, expected cost 0.0831
## INFO [2017-12-07 15:39:58] 2017-12-07 15:39:58 - epoch 2, expected cost 0.0615
## INFO [2017-12-07 15:40:02] 2017-12-07 15:40:02 - epoch 3, expected cost 0.0544
## INFO [2017-12-07 15:40:07] 2017-12-07 15:40:07 - epoch 4, expected cost 0.0503
## INFO [2017-12-07 15:40:12] 2017-12-07 15:40:12 - epoch 5, expected cost 0.0478
## INFO [2017-12-07 15:40:17] 2017-12-07 15:40:17 - epoch 6, expected cost 0.0460
## INFO [2017-12-07 15:40:21] 2017-12-07 15:40:21 - epoch 7, expected cost 0.0446
## INFO [2017-12-07 15:40:26] 2017-12-07 15:40:26 - epoch 8, expected cost 0.0435
## INFO [2017-12-07 15:40:34] 2017-12-07 15:40:34 - epoch 9, expected cost 0.0426
## INFO [2017-12-07 15:40:39] 2017-12-07 15:40:39 - epoch 10, expected cost 0.0419
## INFO [2017-12-07 15:40:45] 2017-12-07 15:40:45 - epoch 11, expected cost 0.0413
## INFO [2017-12-07 15:40:50] 2017-12-07 15:40:50 - epoch 12, expected cost 0.0408
## INFO [2017-12-07 15:40:54] 2017-12-07 15:40:54 - epoch 13, expected cost 0.0403
## INFO [2017-12-07 15:40:58] 2017-12-07 15:40:58 - epoch 14, expected cost 0.0399
## INFO [2017-12-07 15:41:02] 2017-12-07 15:41:02 - epoch 15, expected cost 0.0395
## INFO [2017-12-07 15:41:06] 2017-12-07 15:41:06 - epoch 16, expected cost 0.0392
## INFO [2017-12-07 15:41:10] 2017-12-07 15:41:10 - epoch 17, expected cost 0.0389
## INFO [2017-12-07 15:41:15] 2017-12-07 15:41:15 - epoch 18, expected cost 0.0387
## INFO [2017-12-07 15:41:19] 2017-12-07 15:41:19 - epoch 19, expected cost 0.0384
## INFO [2017-12-07 15:41:23] 2017-12-07 15:41:23 - epoch 20, expected cost 0.0382

Averaging learned word vectors

The two vectors are main and context. According to the Glove paper, averaging the two word vectors results in more accurate representation.

wiki_context <- glove$components
dim(wiki_context)
## [1]    50 71290
wiki_vectors <- wiki_main + t(wiki_context)

Examining term representations

Now we can find the closest word vectors for paris - france + germany

new_berlin <-  wiki_vectors["paris", ] - 
    wiki_vectors["france", ] + 
    wiki_vectors["germany", ]

# calculate the similarity
wiki_vector_dfm <- as.dfm(rbind(wiki_vectors, new_berlin))
cos_sim <-  textstat_simil(wiki_vector_dfm, "new_berlin", 
                           margin = "documents", method = "cosine")
head(sort(cos_sim[,1], decreasing = TRUE), 5)
## new_berlin     berlin     munich      paris    germany 
##  1.0000000  0.7734271  0.7343173  0.7327437  0.6806264

Here is another example for london = paris - france + uk + england

new_london <-  wiki_vectors["paris", ] - 
    wiki_vectors["france", ] + 
    wiki_vectors["uk", ] + 
    wiki_vectors["england", ] 

wiki_vector_dfm <- as.dfm(rbind(wiki_vectors, new_london))
cos_sim <-  textstat_simil(wiki_vector_dfm, "new_london", 
                           margin = "documents", method= "cosine")
head(sort(cos_sim[,1], decreasing = TRUE), 5)
## new_london         uk     london    england         at 
##  1.0000000  0.7722497  0.7690377  0.7399987  0.7278628