library(quanteda)

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

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_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.73 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 [2018-04-10 20:51:59] 2018-04-10 20:51:59 - epoch 1, expected cost 0.0829 ## INFO [2018-04-10 20:52:18] 2018-04-10 20:52:18 - epoch 2, expected cost 0.0616 ## INFO [2018-04-10 20:52:37] 2018-04-10 20:52:37 - epoch 3, expected cost 0.0541 ## INFO [2018-04-10 20:52:56] 2018-04-10 20:52:56 - epoch 4, expected cost 0.0501 ## INFO [2018-04-10 20:53:15] 2018-04-10 20:53:15 - epoch 5, expected cost 0.0475 ## INFO [2018-04-10 20:53:34] 2018-04-10 20:53:34 - epoch 6, expected cost 0.0458 ## INFO [2018-04-10 20:53:53] 2018-04-10 20:53:53 - epoch 7, expected cost 0.0444 ## INFO [2018-04-10 20:54:13] 2018-04-10 20:54:13 - epoch 8, expected cost 0.0434 ## INFO [2018-04-10 20:54:32] 2018-04-10 20:54:32 - epoch 9, expected cost 0.0425 ## INFO [2018-04-10 20:54:51] 2018-04-10 20:54:51 - epoch 10, expected cost 0.0418 ## INFO [2018-04-10 20:55:10] 2018-04-10 20:55:10 - epoch 11, expected cost 0.0412 ## INFO [2018-04-10 20:55:31] 2018-04-10 20:55:31 - epoch 12, expected cost 0.0406 ## INFO [2018-04-10 20:55:51] 2018-04-10 20:55:51 - epoch 13, expected cost 0.0402 ## INFO [2018-04-10 20:56:11] 2018-04-10 20:56:10 - epoch 14, expected cost 0.0398 ## INFO [2018-04-10 20:56:30] 2018-04-10 20:56:30 - epoch 15, expected cost 0.0394 ## INFO [2018-04-10 20:56:51] 2018-04-10 20:56:51 - epoch 16, expected cost 0.0391 ## INFO [2018-04-10 20:57:12] 2018-04-10 20:57:12 - epoch 17, expected cost 0.0388 ## INFO [2018-04-10 20:57:34] 2018-04-10 20:57:34 - epoch 18, expected cost 0.0386 ## INFO [2018-04-10 20:57:55] 2018-04-10 20:57:55 - epoch 19, expected cost 0.0383 ## INFO [2018-04-10 20:58:14] 2018-04-10 20:58:14 - 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. 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      paris     vienna     berlin    germany
##  1.0000000  0.7585456  0.7228084  0.7124334  0.6725250

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    england         uk       club     london
##  1.0000000  0.7693720  0.7407939  0.7401705  0.7276365