对于作业,我需要找到文本文件的句子数(而不是行)。这意味着,在字符串的末尾,我将拥有“”。或者“!”或者“?”在挣扎了很多之后,我写了一个代码,这是一个错误。不过,我没有看到任何错误。如果有人能帮我,我会非常感激的。谢谢
这是我的密码
fh1 = fopen(nameEssay); %nameEssay is a string of the name of the file with .txt
line1 = fgetl(fh1); % line1给出了文章的标题。这不算作一句
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);发布于 2014-02-28 06:50:53
regexp很好地处理了这一点:
>> essay = 'First sentence. Second one? Third! Last one.'
essay =
First sentence. Second one? Third! Last one.
>> sentences = regexp(essay,'\S.*?[\.\!\?]','match')
sentences =
'First sentence.' 'Second one?' 'Third!' 'Last one.'在模式'\S.*?[\.\!\?]'中,\S表示句子以非空格字符开头,.*?匹配任意数量的字符(不贪婪),直到遇到标点符号([\.\!\?])为止。
发布于 2014-02-28 06:33:12
如果以“”为基础,则计算句数。或者“!”或者“?”,您只需计算essey中这些字符的数量。因此,如果随笔是包含字符的数组,则可以:
essay = 'Some sentece. Sentec 2! Sentece 3? Sentece 4.';
% count number of '.' or '!' or '?' in essey.
sum(essay == abs('.'))
sum(essay == abs('?'))
sum(essay == abs('!'))
% gives, 2, 1, 1. Thus there are 4 sentences in the example.如果你想要句子,你可以像丹建议的那样使用串裂。
[C, matches] = strsplit(essay,{'.','?', '!'}, 'CollapseDelimiters',true)
% gives
C =
'Some sentece' ' Sentec 2' ' Sentece 3' ' Sentece 4' ''
matches =
'.' '!' '?' '.'并计算匹配中的元素数。对于示例,最后一个元素为空。它可以容易地过滤掉。
https://stackoverflow.com/questions/22087365
复制相似问题