can i also split the sentences in words using regexp because i want to find the length of each word and then compare each letter of the word with all other letters of the same word to check how many times the specific letter occur in that word.i will try to post the algo which i want to implement so u can get a better idea.
how to count the no of words in a sentence?
5 次查看(过去 30 天)
显示 更早的评论
i have read the text file sentence by sentence by using the following code. now i want to count the total no of words in each sentence.. how can i do that? i know how to read a text file word by word but it gives total no of words in the whole document and i want to calculate no of words for each sentence separately.
fid=fopen('hello.txt');
text = textread('hello.txt','%s','delimiter','.')
fclose(fid);
[r,c]=size(text)
3 个评论
Matt Kindig
2013-6-19
Can you clarify what your desired output actually is? Is it a count of letters per word, or a count of all of the letters used within a single sentence, or within the document.
I think you can do this fairly easily using hist(), but I admit that I am a bit confused as to what your final goal is. Can you post a sample text (maybe only a couple of short sentences), and your desired output?
采纳的回答
Matt Kindig
2013-6-18
编辑:Matt Kindig
2013-6-18
txt = fileread('hello.txt'); %read in file, assuming it is not too big
sentences = strtrim(regexp(txt, '[\.\?\!]+', 'split')); %split into sentences
%I assume that sentences end with ., ?, or !, which don't appear elsewhere in
% the file (i.e., no quoted strings, abbreviations containing '.', etc.).
words = regexp(sentences, '\s+', 'start'); % find spaces (which separate words)
wordsPerSentence = cellfun(@length, words, 'UniformOutput', true); %count words
%we only care about sentences containing at least one word.
wordsPerSentence = wordsPerSentence(wordsPerSentence>=1)+1;
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!