%% Specify Word Colors
% Create text scatter plot of a word embedding and specify word colors.
% Load a pretrained word embedding using fastTextWordEmbedding. This function requires Text Analytics Toolbox™ Model for fastText English 16 Billion Token Word Embedding support package. If this support package is not installed, then the function provides a download link.

emb = fastTextWordEmbedding;

% Convert the first 250 words to vectors using word2vec. V is a matrix of word vectors of length 300.

words = emb.Vocabulary(1:250);
V = word2vec(emb,words);
size(V)

% Embed the word vectors in a 3-D space using tsne.

XYZ = tsne(V,'NumDimensions',3);

% Plot the words at the coordinates specified by XYZ in a 3-D text scatter plot. Specify the word colors to be random.

numWords = numel(words);
colorData = rand(numWords,3);
figure
textscatter3(XYZ,words,'ColorData',colorData)
title("Word Embedding t-SNE Plot")

% Copyright 2012 The MathWorks, Inc.