필터 지우기
필터 지우기

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 CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by