I want to replace more than 1 random words in a sentence with "___" using matrices. I can type it out long hand but want to avoid this.

1 view (last 30 days)
I'm trying to make a tool for memorizing things but I'm having trouble with replacing words with blanks in an efficient and elegant way.
So far I have the user prompted to enter what they want to memorize.
The user then indicates that they are ready. Finally they pick the amount of random words they want gone from what they are trying to memorize.
I don't want to use iterative (first one word, then two words, etc.) I'd like the user to be able to choose the number of words.
I used specifying the location of a word by the characters between spaces.
Then I randomly generated a word and replaced it with the desired ___.
This would be long and tedious to code for memorizing passages with well over 100 words.
fprintf('Hello! When entering the verse make sure that the spaces are correct. \n However, please leave no spaces in between the Book, chapter in verse like so: \n John3:16')
Vwos = input("\n\n Please enter what you would like to memorize: \n" , 's'); % The verse without a space at the front.
space = ' ';
Verse = append(space,Vwos); % Now the verse has an extra space at the beggining to rid of an error with negative numbers in randperm function below.
SL = isspace(Verse); % Finding out where the location of the spaces are.
SLArray = find(SL==1); % Making the locations a matrix. ie if the 1 was at position 4, 6, 8 in the string then the matrix would be [4,6,8]
length = length(SLArray); % Used in random number generation
x = input("\n Ready? (1=yes/0=no) \n"); % So as not to jump straight into the loop
while x==1
y = input("\n How many words would you like to remove? (Enter 999 for all)\n"); % Excuse this mess.
if y==1
randnum = 1+randperm(length-1,1); % Randomly generates a matrix of a certain size depending on use input.
Dw = Verse(SLArray(randnum-1)+1:SLArray(randnum)); % Finds the word that will be deleted from the verse.
Vw1m = strrep(Verse, Dw,'____ '); % Creates a new string without that word
fprintf(Vw1m);fprintf('\n') % Displays it to user.
elseif y==2
randnum = 1+randperm(length-1,2); % Randomly generates a matrix of a certain size depending on use input.
Dw1 = Verse(SLArray(randnum(1)-1)+1:SLArray(randnum(1))); % Finds the word that will be deleted from the verse.
Dw2 = Verse(SLArray(randnum(2)-1)+1:SLArray(randnum(2))); % Finds the word that will be deleted from the verse.
Vw1m = strrep(Verse, Dw1,'____ '); % Deletes the first word
Vw2m = strrep(Vw1m, Dw2,'____ '); % Deletes the second word.
fprintf(Vw2m); % displays result to user
end
x=input('\n\n\n Continue? (1=Yes/0=No) \n'); % Ability to keep going or end the loop
clc;
end
As you can see this would take forever to code to be able to get rid of 100 words.
Can anyone think of a way to make it matrix driven and without the huge messy loop?
I came up with:
Vwos = input("\n\n Please enter what you would like to memorize: \n" , 's'); % The verse without a space at the front.
space = ' ';
Verse = append(space,Vwos); % Now the verse has an extra space at the beggining to rid of an error with negative numbers in randperm function below.
SL = isspace(Verse); % Finding out where the location of the spaces are.
SLArray = find(SL==1); % Making the locations a matrix. ie if the 1 was at position 4, 6, 8 in the string then the matrix would be [4,6,8]
length = length(SLArray); % Used in random number generation
y = input("\n How many words would you like to remove? \n"); % Excuse this mess.
randnum = 1+randperm(length-1,y) % Random number array just like before
DeletedWordsLocT = [SLArray(randnum-1);SLArray(randnum)] % Creates a 2 by y matrix
DeletedWordsLoc = transpose(DeletedWordsLocT) % Transposes it to be a y by 2 matrix for easier use. Now words are coded into each row.
% The first column is the location of the first character of the word and sencond column is the location of the space following the word.
Versewithmissing = strrep(Verse, DeletedWords,'____ ') % I don't know how to get it to work
fprintf(Versewithmissing);
This is using the same logic as above with the location of the words, but not individually but as a matrix. I can figure out how to replace the words using a matrix. For this to work a couple of things need to happen.
  1. Need to be able to define a matrix of strings from their location in a parent string for y number of words.
  2. Need to be able to replace all of those strings with ____ without typing every line of code for all 100 words like the first code.
Thanks for your help!

Accepted Answer

Paul
Paul on 20 Apr 2022
Hi Cade,
It would be helpful to include an example input and show what the desired output should be. As best I can tell, maybe this is what you want?
Vwos = "The quick brown fox jumped over the lazy dog."
Vwos = "The quick brown fox jumped over the lazy dog."
y = 3;
rstr = "_____";
VerseWithMissing = split(Vwos);
rng(100); % for repeatibility during development
DeletetedWordsLoc = randperm(numel(VerseWithMissing),y);
VerseWithMissing(DeletetedWordsLoc) = rstr;
VerseWithMissing = join(VerseWithMissing)
VerseWithMissing = "The _____ brown _____ _____ over the lazy dog."

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!