%% Create Word Encoding of Top Words in Documents
% 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);
documents(1:10)

% Create a word encoding. Sort the indices by frequency and encode only the top 100 words.

enc = wordEncoding(documents, ...
    'Order','frequency', ...
    'MaxNumWords',100)

% View the words corresponding to indices 1, 2, and 3 using the ind2word function.

idx = [1 2 3];
words = ind2word(enc,idx)

% Copyright 2018 The MathWorks, Inc.