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

3 ビュー (過去 30 日間)
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.

採用された回答

Daniel Shub
Daniel Shub 2011 年 11 月 8 日
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 件のコメント
NUR KHAIRUNNISA rahimi
NUR KHAIRUNNISA rahimi 2011 年 11 月 20 日
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
NUR KHAIRUNNISA rahimi
NUR KHAIRUNNISA rahimi 2011 年 11 月 20 日
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

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2011 年 11 月 20 日
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 件のコメント
NUR KHAIRUNNISA rahimi
NUR KHAIRUNNISA rahimi 2011 年 11 月 20 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by