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

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_termfreq = 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.23 seconds.

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.

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.

## INFO [2018-09-06 09:54:55] 2018-09-06 09:54:55 - epoch 1, expected cost 0.0826
## INFO [2018-09-06 09:54:59] 2018-09-06 09:54:59 - epoch 2, expected cost 0.0616
## INFO [2018-09-06 09:55:02] 2018-09-06 09:55:02 - epoch 3, expected cost 0.0542
## INFO [2018-09-06 09:55:06] 2018-09-06 09:55:06 - epoch 4, expected cost 0.0502
## INFO [2018-09-06 09:55:09] 2018-09-06 09:55:09 - epoch 5, expected cost 0.0476
## INFO [2018-09-06 09:55:12] 2018-09-06 09:55:12 - epoch 6, expected cost 0.0459
## INFO [2018-09-06 09:55:16] 2018-09-06 09:55:16 - epoch 7, expected cost 0.0445
## INFO [2018-09-06 09:55:19] 2018-09-06 09:55:19 - epoch 8, expected cost 0.0435
## INFO [2018-09-06 09:55:23] 2018-09-06 09:55:23 - epoch 9, expected cost 0.0426
## INFO [2018-09-06 09:55:27] 2018-09-06 09:55:27 - epoch 10, expected cost 0.0419
## INFO [2018-09-06 09:55:30] 2018-09-06 09:55:30 - epoch 11, expected cost 0.0412
## INFO [2018-09-06 09:55:34] 2018-09-06 09:55:34 - epoch 12, expected cost 0.0407
## INFO [2018-09-06 09:55:38] 2018-09-06 09:55:38 - epoch 13, expected cost 0.0402
## INFO [2018-09-06 09:55:42] 2018-09-06 09:55:42 - epoch 14, expected cost 0.0398
## INFO [2018-09-06 09:55:46] 2018-09-06 09:55:46 - epoch 15, expected cost 0.0395
## INFO [2018-09-06 09:55:50] 2018-09-06 09:55:50 - epoch 16, expected cost 0.0392
## INFO [2018-09-06 09:55:54] 2018-09-06 09:55:54 - epoch 17, expected cost 0.0389
## INFO [2018-09-06 09:55:58] 2018-09-06 09:55:58 - epoch 18, expected cost 0.0386
## INFO [2018-09-06 09:56:02] 2018-09-06 09:56:02 - epoch 19, expected cost 0.0384
## INFO [2018-09-06 09:56:06] 2018-09-06 09:56:06 - epoch 20, expected cost 0.0381

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.

## [1]    50 71290

Examining term representations

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

## new_berlin      paris     berlin    germany     munich 
##  1.0000000  0.7507470  0.7332112  0.6879837  0.6779857

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

## new_london         uk     london    england         at 
##  1.0000000  0.7705784  0.7552868  0.7516413  0.7177546