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!