Matlab code Function Name: celery Inputs: 1. ( char ) The name of a text file, i
ID: 3727191 • Letter: M
Question
Matlab code
Function Name: celery
Inputs:
1. ( char ) The name of a text file, including file extension
Outputs:
None
File Outputs:
1. A formatted text file containing information about the initial text file
Banned Functions:
unique()
Background:
You recently started writing up some of your favorite short stories and scripts on your computer, but since you have started working with celerys *cough cough* I mean cell arrays in MATLAB, they have been corrupted! Oh no! Now, you must embark on an epic quest to use the very same piece of software that corrupted your files to analyze your files.
Function Description:
You will be given a text file. Extract every unique word and count the number of times each unique word appears in the text file. Case should be ignored when looking for unique words, and any characters that are not letters or spaces should be removed. The words should appear in decreasing order of how often they appeared in the text file. If there is a tie, you should sort the ties alphabetically. To make the output text file, follow these rules:
The output text file should have the name <filename>_counted.txt .
The output file should start with 'A counting of the story <filename>:'.
The second line should read 'The number of times celery was seen: <number of
celery>' .
The third line should read 'All other words:' .
The rest of the file should list the unique words in the following form: '<word>: <num
times>' .
Each word should be lowercase.
Put each word on a new line.
Do not include the word "celery" in this section.
Example:
Notes :
There should NOT be a newline character at the end of your text file.
All the words in the text file should be lowercase.
Hints :
sort() preserves the original order of duplicated elements. In order to sort by two
things, think about how you could use the sort() function twice.
Explanation / Answer
Hi, I have done the logic for taking file, making unique words array and then getting each's frequency.
You might need to work on the last part where you are printing and define some proper formatspec otherwise the logic is working and checked.
All the best!
%% First import the words from the text file into a cell array
filename = < Input file path here >
ifid = fopen(filename);
[filepath,inputFileName,ext] = fileparts(filename);
words = textscan(ifid, '%s');
fclose(ifid);
%% Get rid of all the characters that are not letters or numbers
for i=1:numel(words{1,1})
ind = find(isstrprop(words{1,1}{i,1}, 'alphanum') == 0);
words{1,1}{i,1}(ind)=[];
end
%% Remove entries in words that have zero characters
for i = 1:numel(words{1,1})
disp(['The word is: ' words{1,1}{i,1}]);
if size(words{1,1}{i,1}, 2) == 0
words{1,1}{i,1} = ' ';
end
end
%% Now count the number of times each word appears
%unique_words = unique(words{1,1}); % words without duplicates
unique_words = {};
for i = 1:numel(words{1,1})
if (!ismember(words{1,1}{i},unique_words))
unique_words = vertcat(unique_words, words{1,1}{i});
end
end
frequencies = zeros(numel(unique_words), 1);
for i = 1:numel(unique_words)
if max(unique_words{i} ~= ' ')
for j = 1:numel(words{1,1})
if strcmp(words{1,1}(j), unique_words{i})
frequencies(i) = frequencies(i) + 1; % frequencies: frequency of each word in unique_words
end
end
end
end
%Now we will create the output file and print there
output_file = [inputFileName,'_counted.txt'];
ofid=fopen(output_file ,'w');
fprintf(ofid, output_file);
fprintf(ofid, ' A counting of the story %s :' filename);
for i = 1:numel(unique_words)
if (ismember('celery',unique_words))
fprintf(ofid, ' The number of times celery was seen: %d' frequencies(i))
end
end
fprintf(ofid, ' All other words:');
for i = 1:numel(unique_words)
if (!ismember('celery',unique_words))
fprintf(ofid, ' %s: %d' unique_words{i} frequencies(i))
end
end
fclose(ofid);