%% Resume Fitting of LDA Model
% To reproduce the results in 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 four topics. The resume function does not support the default solver for fitlda. Set the LDA solver to be collapsed variational Bayes, zeroth order.

numTopics = 4;
mdl = fitlda(bag,numTopics,'Solver','cvb0')

% View information about the fit.

mdl.FitInfo

% Resume fitting the LDA model with a lower log-likelihood tolerance.

tolerance = 1e-5;
updatedMdl = resume(mdl,bag, ...
    'LogLikelihoodTolerance',tolerance)

% View information about the fit.

updatedMdl.FitInfo

%
% Copyright 2012 The MathWorks, Inc.