%% Transform Documents into LSA Semantic Space
% 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 LSA model with 20 components.

numCompnents = 20;
mdl = fitlsa(bag,numCompnents)

% Use transform to transform the first 10 documents into the semantic space of the LSA model.

dscores = transform(mdl,documents(1:10))

% Copyright 2012 The MathWorks, Inc.