Count word except some special case
显示 更早的评论
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.
回答(1 个)
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 个评论
Mai Thành
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)
upper(x)
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.
x = 'A thesis is an example of The Program';
x = lower(x)
splitwords = split(x, {' ', '.', ',', ':', '?', '!'})
类别
在 帮助中心 和 File Exchange 中查找有关 Characters and Strings 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!