我想使用matlab命令提取每个句子中的特殊字符数量和元音数量 [英] I want to pull out the number of special characters and the number of vowels in each sentence using a matlab command
问题描述
我在Matlab中有一个单元格,其中有5个字符串,每个字符串都是一个句子,我想使用Matlab命令提取每个句子中的特殊字符数量和元音数量.我该怎么办?
I have a cell in Matlab with 5 strings in it, each string being a sentence and I want to pull out the number of special characters and the number of vowels in each sentence using a Matlab command. How would I do this?
这是我的代码:
sentences={['Dogs are cool!'];['They love to have their bellies scratched!'];['They love to run around and play in the grass.'];['Each normal year is equal to seven dog years.'];['Its sad when dogs die.']}
sentences1=length(sentences{1})
sentences2=length(sentences{2})
sentences3=length(sentences{3})
sentences4=length(sentences{4})
sentences5=length(sentences{5})
fprintf('There are %d, %d, %d, %d characters in each sentence respectively. ', sentences1,sentences2,sentences3,sentences4,sentences5)
推荐答案
如果将特殊字符定义为不是单词字符([a-z_A-Z0-9]
)而不是空格:
If you define a special character as not a word character ([a-z_A-Z0-9]
) and not whitespace:
cellfun(@(x) numel(regexp(x,'[^\w\s]')),sentences)
在您的情况下,每个句子只有一个.如果您只想计算不是单词字符的字符,其中包括空格:
In your case, that is just one per sentence. If you just want to count characters that are not word characters, which includes whitespace:
>> cellfun(@(x) numel(regexp(x,'[^\w]')),sentences)
ans =
3
7
14
9
5
元音的情况只是:
cellfun(@(x) numel(regexpi(x,'[aeiou]')),sentences)
注意,这种情况现在使用regexpi
而不是regexp
进行忽略,但是您也可以将模式扩展到'[AEIOUaeiou]'
并使用常规的regexp
.根据您的心情,您还可以选择使用[aeiouy]
.
Notice this now uses regexpi
instead of regexp
to ignore, case but you can also expand the pattern to '[AEIOUaeiou]'
and use the normal regexp
. Depending on your mood, you may also choose to use [aeiouy]
.
这篇关于我想使用matlab命令提取每个句子中的特殊字符数量和元音数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!