
Read Pretrained Static Embeddings From Files
Source:R/word_embeddings-read.R
read_embeddings_model2vec.RdThese functions read downloaded static embeddings into the tibble that the
embeddings argument of step_word_embeddings() expects: the first column
holds the tokens and the other columns hold the vectors. They read files
that are already on disk and do not download anything. Use
download_embeddings_model2vec() to download a Model2Vec model first.
Value
A tibble with a character column token
followed by one numeric column for each dimension of the embeddings.
Details
The vectors belong to the tokens of one specific tokenizer, and tokens that
are not found in the table are dropped without a warning. The text therefore
has to be tokenized in the same way the model was trained. See
vignette("static-embeddings", package = "textrecipes") for the recipes to
use with each model, and for where to download the files.
See also
step_word_embeddings() to use the embeddings in a recipe.
Examples
# A small file in the BPEmb format
file <- tempfile(fileext = ".w2v.txt")
writeLines(
c("3 2", "<unk> 0.1 0.2", "▁the 0.3 0.4", "▁cat 0.5 0.6"),
file
)
read_embeddings_bpemb(file)
#> # A tibble: 3 × 3
#> token d1 d2
#> <chr> <dbl> <dbl>
#> 1 <unk> 0.1 0.2
#> 2 ▁the 0.3 0.4
#> 3 ▁cat 0.5 0.6