フィルターのクリア

Count word except some special case

1 回表示 (過去 30 日間)
Mai Thành
Mai Thành 2021 年 12 月 8 日
コメント済み: Chunru 2021 年 12 月 10 日
Count the number of words in a string, excluding the article (a, an, the), regardless of capitalization. Demonstrate the working of your code with an example string.
  2 件のコメント
Adam Danz
Adam Danz 2021 年 12 月 8 日
This is clearly a homework assignment. You also haven't asked a question.
Show us what you've got and ask a specific question about the step where you're stuck. Don't ask people to do your homework for you.
Mai Thành
Mai Thành 2021 年 12 月 9 日
Oh I don't know about that, I'm sorry :(
so this is my code so far
x=input ('Input your string: ','s');
A1=strfind(x,'A ');
A2=strfind(x,' A.');
A3=strfind(x,' A ');
real_A= A1+A2-A3;
a1=strfind(x,'a ');
a2=strfind(x,' a.');
a3=strfind(x,' a ');
real_a= a1+a2-a3;
an1=strfind(x,'an ');
an2=strfind(x,' an.');
an3=strfind(x,' an ');
real_an=an1+an2-an3;
An1=strfind(x,'An ');
An2=strfind(x,' An.');
An3=strfind(x,' An ');
real_An=An1+An2-An3;
the1=strfind(x,'the ');
the2=strfind(x,' the.');
the3=strfind(x,' the ');
real_the=the1+the2+the3;
The1=strfind(x,'The ');
The2=strfind(x,' The.');
The3=strfind(x,' The ');
real_The=The1+The2+The3;
real=real_A + real_a + real_an + real_An + real_the + real_The;
pos=strfind(x, ' ');
y=numel(pos)+1-real;
fprintf ('The number of word in the string is %f\n', y)
I calculate the number of spaces in the sentence and add 1 into it to have the number of words. Then I calculate the number of article words (a, an, the) and use the total words minus article words to have the answer. However, the are some case that ans =[] and any number add or minus with ans will have the result is []. I don't know how to fix it :(

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

回答 (1 件)

Chunru
Chunru 2021 年 12 月 9 日
x ='some text here with a and an and the';
% you can use "lower" to convert it into lower case
%
% consider "split" to split the string into words with appropriate
% delimiters
%
% consider "ismember" to test the split words belong to "a" "an" and "the"
% idx = ~ismember(splitwords, {'a', 'an', 'the'})
% "sum" up idx to get the number of words
% n = sum(idx)
  3 件のコメント
Adam Danz
Adam Danz 2021 年 12 月 9 日
Instead of handeling upper and lower cases separately, force the text to be upper or lower case.
Example:
x = 'A thesis is an example of The Program';
lower(x)
ans = 'a thesis is an example of the program'
upper(x)
ans = 'A THESIS IS AN EXAMPLE OF THE PROGRAM'
Use Chunru's advice to split the sentence into words and use ismember to determine which words match the exclusion list (a,an,the). The list should match the case you choose.
Chunru
Chunru 2021 年 12 月 10 日
x = 'A thesis is an example of The Program';
x = lower(x)
x = 'a thesis is an example of the program'
splitwords = split(x, {' ', '.', ',', ':', '?', '!'})
splitwords = 8×1 cell array
{'a' } {'thesis' } {'is' } {'an' } {'example'} {'of' } {'the' } {'program'}

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

カテゴリ

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