Create a .txt file with text copied from a movie or a product review from the we
ID: 3842375 • Letter: C
Question
Create a .txt file with text copied from a movie or a product review from the web-do not exceed 100 words. In one single matlab script file (exercise02.m), implementing the following: Read in the lines of the text file using the fgetl function. Store the lines ofthis file in a cell array. Useful functions: for, fopen, fgetl. Parse your stored lines of text into their constituent words using the strtok function. Store all of the words in the entire document in a cell array with one word per index. Create a lexicon consisting of all of the unique words in all the files. Useful function: unique. Create a column vector representing how many times each lexicon word occurs in the document. This is a word vector representation for the document. Useful function: zeros.Explanation / Answer
fileId = fopen('test.txt'); % file to read
%empty cell array
A = {};
words={};
x = 1;
y = 1;
%read file
line = fgetl(fileId);
while ischar(line)
line = fgetl(fileId);
A{x,1} = line;
x=x+1;
end
fclose(fileId); %close file
[rows,cols] = size(A)
%read cell array and apply atrtok
for i = 1:rows
remain = A{i,1};
while (remain ~= "")
[token,remain] = strtok(remain);
words{y,1}=token;
y=y+1;
end
end
%create matrix of words, and then find unique words
C = unique(cell2mat(words)) %lexicon of unique words
result=[]; % to store words and number of occurance
%loop to find number of occurance of a word in the file
for i = 1:size(C)
temp = 0;
for j = 1:size(words,1)
if(strcmp(C(i),words{j,1}))
temp = temp+1
end
end
result = [result ; C(i) temp]
end