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?
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Matt Kindig
el 19 de Jun. de 2013
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?
Respuesta aceptada
Matt Kindig
el 18 de Jun. de 2013
Editada: Matt Kindig
el 18 de Jun. de 2013
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;
Más respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!