%% Visualize LDA Model Using Text Scatter Plots
% To reproduce the results of this example, set rng to 'default'.

rng('default')

% Load the example data. The file sonnetsPreprocessed.txt contains preprocessed versions of Shakespeare's sonnets. The file contains one sonnet per line, with words separated by a space. Extract the text from sonnetsPreprocessed.txt, split the text into documents at newline characters, and then tokenize the documents.

filename = "sonnetsPreprocessed.txt";
str = extractFileText(filename);
textData = split(str,newline);
documents = tokenizedDocument(textData);

% Create a bag-of-words model using bagOfWords.

bag = bagOfWords(documents);

% Fit an LDA model with 20 topics. To suppress verbose output, set 'Verbose' to 0.

numTopics = 20;
mdl = fitlda(bag,numTopics,'Verbose',0);

% Visualize the LDA model topics using tsne and textscatter.
% The TopicWordProbabilities propery of mdl contains the per-topic word probabilities for each word in the vocabulary. For the first 500 words in the vocabulary, embed these probability vectors in 2-D space using tsne and display them in a text scatter plot.

numWords = 500;
words = mdl.Vocabulary(1:numWords);
wordProbabilities = mdl.TopicWordProbabilities(1:numWords,:);

XY = tsne(wordProbabilities);
figure
textscatter(XY,words)