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

可以下载一长篇的英文小说,进行词频的分析。

1.读入待分析的字符串

2.分解提取单词 

3.计数字典

4.排除语法型词汇

5.排序

6.输出TOP(20)

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

 

fo=open('test.txt','r')#读取文件内容
e=fo.read()
fo.close()
e=e.lower()    #字符串处理
for i in '.,':
    e=e.replace(i,' ')
    
    words=e.split(' ')#单词的列表
    exp={'the','a','are','i','you'}
dic={}
keys=set(words)-exp     #键的集合
for j in keys:
    dic[j]=words.count(j)    #单词计数字典

d=list(dic.items())       #单词计数元组的列表
d.sort(key=lambda x:x[1],reverse=True) #列表排序
for i in range(20): #输出TOP20元组
        print(d[i])
 RESTART: C:/Users/Administrator/AppData/Local/Programs/Python/Python36/hj.py 
('', 98)
('and', 24)
('of', 21)
('to', 15)
('it', 11)
('as', 10)
('was', 9)
('they', 9)
('in', 9)
('had', 9)
('his', 8)
('other', 8)
('were', 7)
('each', 7)
('her', 6)
('an', 6)
('is', 6)
('with', 6)
('he', 5)
('so', 5)
>>> 

 

 

转载于:https://www.cnblogs.com/095lqt/p/7602444.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值