Can you please help me with solving the problem by using Matlab Unnecessary Cens
ID: 3571805 • Letter: C
Question
Can you please help me with solving the problem by using Matlab
Unnecessary Censorship^2. It's easy: Read in a text file. Replace all 4 letter words with *** Write the result back out to a new file. For example, text. txt contains the text: Hullabaloo, Caneck! Caneck! Hullabaloo, Caneck! Caneck! Good-bye to texas university So long to the orange and the white Good luck to dear old Texas Aggies They are the boys that show the real old fight "The eyes of Texas are upon you" That is the song they sing so well Invoke the censor function: >> censor('text.txt'); The file text censored.txt contains the censored text: Hullabaloo, Caneck! Caneck! Hullabaloo, Caneck! Caneck! Good-bye to texas university So *** to the orange and the white **** **** to **** old Texas Aggies **** are the **** **** **** the **** old fight "The *** of Texas are **** you" **** is the **** **** **** so **** >> in - 'text.txt'; >> out = (in(1:end-4)' censored' in(end-3:end)) out = text censored.txtExplanation / Answer
text.txt
Hullabaloo, Caneck! Caneck!
Hullabaloo, Caneck! Caneck!
Good-bye to texas university
So long to the orange and the white
Good luck to dear old Texas Aggies
They are the boys that show the real old fight
"The eyes of Texas are upon you"
That is the song they sing so well
text_censored.txt
Hullabaloo, Caneck! Caneck!
Hullabaloo, Caneck! Caneck!
Good-bye to texas university
So **** to the orange and the white
**** **** to **** old Texas Aggies
**** are the **** **** **** the **** old fight
"The **** of Texas are **** you"
**** is the **** **** **** so ****
censorship.m
f = fileread('text.txt');
f = strread(f, '%s', 'delimiter', sprintf(' '));
g = fopen('text_censored.txt','w');
for x = 1:length(f)
% go through the lines of f, breaking each line into a list of words.
line = char(f(x)); % the line we are currently looking at
sent = [strsplit(line)]; % the tokenized form of our line by spaces.
newLine = []; % initialize the new sentence we are going to create.
% go through the words of a line, finding the words that need to be censored.
for y = 1:length(sent)
% Use proper spacing based on where we are in the line.
if y == length(sent)
endSpace = '';
else
endSpace = ' ';
end
% the easiest way to replace the 4-lettered words with a censored version
% of themselves without messing up any punctuation or special characters
% would be to replace the existing instance of their text all at once.
word = char(sent(y));
cleanWord = word(isstrprop(word,'alpha'));
if length(cleanWord) == 4
% This is a word that needs to be censored. Mark it.
newLine = [newLine, [strrep(word,cleanWord,'****'),endSpace]];
else
% It's clean, add the full word.
newLine = [newLine, [word,endSpace]];
end
end
% write to text_censored.txt
fprintf(g,'%s ',char(newLine));
end