2013-03-05 187 views
1

这是迄今为止我所知道的,但是我的pparagraph只包含5个句号,因此只有5个句子。但它一直保持返回14作为答案。谁能帮忙?如何计算python中段落中句子的数量

file = open ('words.txt', 'r') 
lines= list (file) 
file_contents = file.read() 
print(lines) 
file.close() 
words_all = 0 
for line in lines: 
    words_all = words_all + len(line.split()) 
    print ('Total words: ', words_all) 
full_stops = 0 
for stop in lines: 
    full_stops = full_stops + len(stop.split('.')) 
print ('total stops: ', full_stops) 

这里是txt文件

车床是根据规则的表上的磁带 的条操纵符号的装置。尽管简单,但图灵机可以适用于模拟任何计算机算法的逻辑,特别是用于解释计算机内部CPU的功能的 。 1933年,Alan Turing对“图灵”机器进行了描述,他称之为“一种(自动)机器”。图灵机并不是作为一种实用的计算技术,而是作为代表计算机的虚拟设备。图灵机帮助计算机科学家理解机械计算的极限。

+1

您正在计算零件*之间*满座。为什么不使用'stop.count('。')'而不是? – 2013-03-05 15:46:18

+0

你可以发布words.txt内容吗? – drekyn 2013-03-05 15:46:22

+0

@MartijnPieters不仅是时段之间的分段,还有换行符和句点之间的分段。 – 2013-03-05 15:47:24

回答

3

如果某行不包含期间,split将返回一个元素:行本身:

>>> "asdasd".split('.') 
    ['asdasd'] 

所以你正在计算行数加周期数。你为什么将文件分割成几行?

with open('words.txt', 'r') as file: 
    file_contents = file.read() 

    print('Total words: ', len(file_contents.split())) 
    print('total stops: ', file_contents.count('.')) 
+0

非常感谢你:-) – 2013-03-05 15:54:02

0

尝试

print "total stops: ", open('words.txt', 'r').read().count(".") 

详情:

with open("words.txt") as f: 
    data = f.read() 
    print "total stops: ", data.count(".") 
+0

也有'file_contents'变量.. – 2013-03-05 15:48:34

+0

@MartijnPieters谢谢,更新... – ATOzTOA 2013-03-05 15:49:14

+0

在OPs代码中'file_contents'是空的,该文件已经通过迭代在'list'内读取。 – 2013-03-05 15:49:44

1

使用正则表达式。

In [13]: import re 
In [14]: par = "This is a paragraph? So it is! Ok, there are 3 sentences." 
In [15]: re.split(r'[.!?]+', par) 
Out[15]: ['This is a paragraph', ' So it is', ' Ok, there are 3 sentences', '']