對於任務,我需要找到文本文件的句子數(而不是行數)。這意味着在字符串末尾我會有'。'要麼 '!'要麼 '?'。在掙扎了很多之後,我寫了一個代碼,這是一個錯誤。我沒有看到任何錯誤。如果任何人都可以幫助我,那將是高度讚賞。由於計算句子*不是*文本文件的行
這裏是我的代碼
fh1 = fopen(nameEssay); %nameEssay is a string of the name of the file with .txt
line1 = fgetl(fh1);
%的LINE-1給出了文章的標題。即不算作句子
essay = [];
line = ' ';
while ischar(line)
line =fgetl(fh1);
essay = [essay line];
%creates a long string of the whole essay
end
sentenceCount=0;
allScore = [ ];
[sentence essay] = strtok(essay, '.?!');
while ~isempty(sentence)
sentenceCount = sentenceCount + 1;
sentence = [sentence essay(1)];
essay= essay(3:end); %(1st character is a punctuation. 2nd is a space.)
while ~isempty(essay)
[sentence essay] = strtok(essay, '.?!');
end
end
fclose(fh1);
什麼錯誤和哪一行? – Marcin
而不是'strtok',你應該看看['strsplit''](http://www.mathworks.com/help/matlab/ref/strsplit.html),然後你可以計算單元格的長度它返回的矩陣。 – Dan
在計算兩個句子之後,這裏給出錯誤 「句子= [句子短文(1)]」numeri(essay)= 0。 但是,我沒有看到原因! – user3226108