%% Highest Probability Words of LDA Topic
% Create a table of the words with highest probability of an LDA topic.
% To reproduce the results, 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);

% Find the top 20 words of the first topic.

k = 20;
topicIdx = 1;
tbl = topkwords(mdl,k,topicIdx)

% Find the top 20 words of the first topic and use inverse mean scaling on the scores.

tbl = topkwords(mdl,k,topicIdx,'Scaling','inversemean')

% Create a word cloud using the scaled scores as the size data.

figure
wordcloud(tbl.Word,tbl.Score);

% Copyright 2012 The MathWorks, Inc.