首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >计算句子*不是*文本文件的行

计算句子*不是*文本文件的行
EN

Stack Overflow用户
提问于 2014-02-28 05:54:47
回答 2查看 121关注 0票数 0

对于作业,我需要找到文本文件的句子数(而不是行)。这意味着,在字符串的末尾,我将拥有“”。或者“!”或者“?”在挣扎了很多之后,我写了一个代码,这是一个错误。不过,我没有看到任何错误。如果有人能帮我,我会非常感激的。谢谢

这是我的密码

代码语言:javascript
复制
fh1 = fopen(nameEssay); %nameEssay is a string of the name of the file with .txt
line1 = fgetl(fh1); 

% line1给出了文章的标题。这不算作一句

代码语言:javascript
复制
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);
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-02-28 06:50:53

regexp很好地处理了这一点:

代码语言:javascript
复制
>> 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表示句子以非空格字符开头,.*?匹配任意数量的字符(不贪婪),直到遇到标点符号([\.\!\?])为止。

票数 3
EN

Stack Overflow用户

发布于 2014-02-28 06:33:12

如果以“”为基础,则计算句数。或者“!”或者“?”,您只需计算essey中这些字符的数量。因此,如果随笔是包含字符的数组,则可以:

代码语言:javascript
复制
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.

如果你想要句子,你可以像丹建议的那样使用串裂

代码语言:javascript
复制
[C, matches] = strsplit(essay,{'.','?', '!'}, 'CollapseDelimiters',true)

% gives
C = 

    'Some sentece'    ' Sentec 2'    ' Sentece 3'    ' Sentece 4'    ''


matches = 

    '.'    '!'    '?'    '.'

并计算匹配中的元素数。对于示例,最后一个元素为空。它可以容易地过滤掉。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22087365

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档