码迷,mamicode.com
首页 > 其他好文 > 详细

文件方式实现完整的英文词频统计实例

时间:2017-09-27 18:54:29      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:提取   print   nbsp   分解   blog   items   log   词汇   一个   

1.读入待分析的字符串

2.分解提取单词 

3.计数字典

4.排除语法型词汇

5.排序

6.输出TOP(20)

7.对输出结果的简要说明。

fr=open(‘news.txt‘,‘r‘)
s=fr.read()
s=s.lower()   #字符串处理
print(‘将所有大写变为小写:‘,s)
for i in ‘,.‘:
    s=s.replace(i,‘ ‘)
print(‘替换符号:‘,s)

b=s.split(‘ ‘)#单词的列表
print(‘分隔出一个一个的单词:‘,b)
exp={‘‘,‘the‘,‘of‘,‘and‘,‘to‘,‘a‘,‘on‘,‘in‘}#不参与统计的单词的集合
dic={}
keys=set(b)-exp#键的集合
for w in keys:
    dic[w]=b.count(w)#计数字典
c=list(dic.items())#(单词,计数)元组的列表
c.sort(key=lambda x:x[1],reverse=True)#列表排序
print(‘排序后:‘,c)

for i in range(20):
    print(c[i])

 

文件方式实现完整的英文词频统计实例

标签:提取   print   nbsp   分解   blog   items   log   词汇   一个   

原文地址:http://www.cnblogs.com/wk15/p/7603149.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!