Searching for specific word containing specific letters and removing last element in the string

I have a wordbank containing only one word for every line. I want to sort of the words in struct, such that wordbank.a=airplane, where the beginning of the word that is stored in the struct starts with its field name, 'a' and then i'll have wordbank.b=ball....[row,1] array and for 'c' 'd' until the letter 'z'.. my question is, how do I make sure that the word that are stored with a specific goes to the right field of letters?
another question is,
if i have a string a=catss,and I want to make it a=cats only.How do I find the words with 's' at the end of the each word so that I could remove every word all plural forms of a word that end in s. If any word that is checked contain word+s, thre plural form must be removed.

 Accepted Answer

For the first part of your question:
wordlist = {'airplane', 'ball', 'bell', 'bull'};
letters = 'a':'z'; % a, b, c, ..., z
wordstruct = struct;
for ichar = 1:length(letters)
wordstruct.(letters(ichar)) = cell.empty;
end
for iword = 1:length(wordlist)
wordstruct.(wordlist{iword}(1)){end+1} = wordlist{iword};
end
I have no idea what you are asking in the second part. Maybe something like:
x = wordlist;
for iword = 1:length(wordlist)
if strcmp(wordlist{iword}(end), 's')
x{iword} = wordlist{iword}(1:(end-1));
end
end

11 Comments

for iword = 1:length(wordlist)
wordstruct.(wordlist{iword}(1)){end+1} = wordlist{iword};
end
for this part, it is very close to what supposed to happen,i am not sure if it's just the way I run the program or the code itself,
because in the cell, the same word is stored multiple times
for example when i have x={ewerwd'; 'wdewd';'wewewss}
the result that came out for
wordbank.w
ans =
Columns 1 through 3
'wdewd' 'wewewss' 'wdewd'
Column 4
'wewewss'
will you explain to me what I did wrong? Thank you.
By the way, it is completely legal to use
for ichar = 'a':'z'
It would be clearer to initialize to {} than to cell(0)
You might have
x={ewerwd'; 'wdewd';'wewewss}
but the words are extracted from the variable "wordlist", not from "x".
And you show the output from wordbank but your code assigns to wordstruct .
@NUR, you have clearly changed the variable names. You have something called wordbank (which my answer does not have). You also are setting x to be something different from what I have set. I cannot guess what you have changed and why you get the problem.
@Walter, despite it being legal I don't like 'a':'z'. I also don't like 97:122 (and have edited accordingly. I also like cell.empty over {}.
For your information, the wordlist that I have is actually arranged such that it is one word per line in a file. So, i used a while loop to get to every line of words and I am planning to put this code into the while loop but I am not sure if it will work until this part:
for iword = 1:length(wordlist)
wordstruct.(wordlist{iword}(1)){end+1} = wordlist{iword};
end
because I dont really have 'wordlist' but i have a word that is stored in a variable everytime it itinerates through the while loop. I need to store each line of words into the struct that is written earlier.
I hope this is not too confusing..
for iword = 1 : inf
thisword = fgetl(fid);
if ~ischar(thisword); break; end
wordstruct.(thisword(1)){end+1} = thisword;
end
alright, so I try to adapt the code that you have given me to the program i am writing... However, something is wrong with my loop, where it keeps on repeating the whole thing over and over again instead of keeping all the words in the wordbank struct. Please ignore any counting of numbers as I am still working on that part for later.
fid = fopen(FILENAME,'r');
if fid<0
%error could not find the file
return,
end
ii=0;
total_no_words=0;
lineNUM=1;
while (1)
tline = fgetl(fid);
if(isempty(tline))
%line is empty, skip it
continue;
end
if(~ischar(tline))
%if line does not contain character
fclose(fid)
break;
end
%we finally have a string
tline=strtrim(tline);
if(sum(isspace(tline)))==length(tline)
continue;
end
is_letter=isletter(tline);
is_space=isspace(tline);
checkcount=sum(is_letter+is_space);
if checkcount~=length(tline)
% invalid line in puzzle
return;
end
%tline only contain spaces and letters
if length(tline) > 3 && length(tline)<26
if strcmp(tline,lower(tline))==1 || strcmp(tline,upper(tline))==1
total_no_words=total_no_words+1;
wordbank=struct;
letters= 'a':'z'; % a, b, c, ..., z
for ichar = 1:length(letters)
wordbank.(letters(ichar))=cell.empty;
wordbank.(tline(lineNUM)) = tline;
end
end
end
lineNUM=lineNUM+1;
disp(wordbank)
end
sorry a correction for the last part of the program
if strcmp(tline,lower(tline))==1 || strcmp(tline,upper(tline))==1
total_no_words=total_no_words+1;
wordbank=struct;
letters= 'a':'z'; % a, b, c, ..., z
for ichar = 1:length(letters)
wordbank.(letters(ichar))=cell.empty;
wordbank.(tline(1)) = tline;
end
end
end
lineNUM=lineNUM+1;
disp(wordbank)
end

Sign in to comment.

More Answers (1)

Examine your code,
for ichar = 1:length(letters)
wordbank.(letters(ichar))=cell.empty;
wordbank.(tline(1)) = tline;
end
Now suppose tline(1) is 'b'. Then on the first loop over ichar, wordbank.(letters(1)) = cell.empty; so you would set wordbank.a to cell.empty; Then on the line below that, wordbank.(tline(1)=tline; so wordbank.b=tline. Now, the second iteration of the ichar loop, wordbank.(letters(1)=cell.empty; so you would set wordbank.b=cell.empty; but that would be overwriting the wordbank.b=tline of the loop above. But then again because of the wordbank.(tline(1))=tline; you would set wordbank.b=tline; Anyhow, keep going this way and you end up with with wordbank.a and .b and .c and so on to .z all set to cell.empty except for the one character that tline starts with, which will have been set to tline (which is a string, not a cell.) And it looks like you are in a loop so it looks like you will repeat this process of clearing back to {} except for the last tline processed.
By the way, when you trim the "plural" form of words, what is going to happen to the word "loss" or to "was" or "yes" or "is" or "us" ?

1 Comment

By the way, when you trim the "plural" form of words, what is going to happen to the word "loss" or to "was" or "yes" or "is" or "us"
Even though those words are not plural, they will not be stored in the wordbank. This program is made such that it still has such flaw because there are still other things to focus on.

Sign in to comment.

Categories

Tags

Community Treasure Hunt

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

Start Hunting!