%% Specify Word Sizes
% Create a word cloud from plain text by reading it into a string array, preprocessing it, and passing it to the wordcloud function.
% Read the text from Shakespeare's Sonnets with the fileread function and convert it to string.

sonnets = string(fileread('sonnets.txt'));
extractBefore(sonnets,"II")

% Split sonnets into a string array whose elements contain individual words. To do this, remove the punctuation characters and join all the string elements into a 1-by-1 string and then split on the space characters. Then, remove words with fewer than five characters and convert the words to lowercase.

punctuationCharacters = ["." "?" "!" "," ";" ":"];
sonnets = replace(sonnets,punctuationCharacters," ");
words = split(join(sonnets));
words(strlength(words)<5) = [];
words = lower(words);
words(1:10)

% Find the unique words in sonnets and count their frequency. Create a word cloud using the frequency counts as size data.

[numOccurrences,uniqueWords] = histcounts(categorical(words));
figure
wordcloud(uniqueWords,numOccurrences);
title("Sonnets Word Cloud")

% Copyright 2012 The MathWorks, Inc.