布同 统计英文单词的个数的python代码
更新时间:2011年03月13日 13:21:09 作者:
最近需要翻译英文文章,所以需要统计单词个数。索性写了一段代码在此,可以简单的统计单词的个数
word中对于英文单词的统计也很好,大家不妨试试。如果没有安装word,而且你也是程序员的话,那么可以使用我的这段代码。通过测试,word的统计结果是18674,软件的统计结果是18349,相差不到2%,可以作为一个参考。
代码如下:
# -*- coding: utf-8 -*-
import os,sys
info = os.getcwd() #获取当前文件名称
fin = open(u'谷歌C++编程代码规范.txt')
info = fin.read()
alist = info.split(' ') # 将文章按照空格划分开
fout = open(u'count.txt', 'w')
fout.write('\n'.join(alist)) # 可以通过文本文件的行号同样看到效果
##fout.write('%s' % alist)
fout.close()
allen = len(alist) # 总的单词数
nulen = alist.count('') # 空格的数量
print "words' number is",allen
print "null number is",nulen
print "poor words number is", allen-nulen # 实际的单词数目
fin.close()
代码如下:
复制代码 代码如下:
# -*- coding: utf-8 -*-
import os,sys
info = os.getcwd() #获取当前文件名称
fin = open(u'谷歌C++编程代码规范.txt')
info = fin.read()
alist = info.split(' ') # 将文章按照空格划分开
fout = open(u'count.txt', 'w')
fout.write('\n'.join(alist)) # 可以通过文本文件的行号同样看到效果
##fout.write('%s' % alist)
fout.close()
allen = len(alist) # 总的单词数
nulen = alist.count('') # 空格的数量
print "words' number is",allen
print "null number is",nulen
print "poor words number is", allen-nulen # 实际的单词数目
fin.close()
相关文章
python opencv鼠标事件实现画框圈定目标获取坐标信息
这篇文章主要为大家详细介绍了python opencv鼠标事件实现画框圈定目标,获取坐标信息,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2019-08-08Python threading模块condition原理及运行流程详解
这篇文章主要介绍了Python threading模块condition原理及运行流程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下2020-10-10
最新评论