%% Create Word Encoding from Word Embedding
% To create a word encoding from a word embedding, input the word embedding vocabulary to the wordEncoding function as a list of words.
% Load pretrained word embedding.

emb = fastTextWordEmbedding;

% Extract the vocabulary.

words = emb.Vocabulary;

% Create a word encoding using the vocabulary.

enc = wordEncoding(words)

% To initialize the corresponding word embedding layer in a deep learning network with the word embedding weights, use the word2vec function to extract the layer weights and set the 'Weights' name-value pair of the wordEmbeddingLayer function. The word embedding layer expects columns of word vectors, so you must transpose the output of the word2vec function.

dimension = emb.Dimension;
numWords = numel(words);

layer = wordEmbeddingLayer(dimension,numWords, ...
    'Weights',word2vec(emb,words)')

% Copyright 2019 The MathWorks, Inc.