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

美国总统特朗普因批评NFL球员抗议种族不平等,并取消NBA冠军金州勇士队访问白宫的传统而受到众多体育明星的强烈谴责。包括克里夫兰骑士队的勒布朗·詹姆斯在内的多位知名运动员公开表达了对特朗普的不满。

1.读入待分析的字符串

2.分解提取单词 

3.计数字典

4.排除语法型词汇

5.排序

6.输出TOP(20)

crity.txt文件中的内容
Donald Trump was the subject of startlingly strong abuse from major sports stars on Saturday, after he criticised NFL players protesting against racial injustice and withdrew an invitation for the NBA-champion Golden State Warriors to visit the White House, breaking a tradition dating back to the Reagan years.

The Cleveland Cavaliers star forward LeBron James called the president a “bum” while the Buffalo Bills running back LeSean McCoy went further, calling Trump an “asshole”. Even NFL commissioner Roger Goodell saying in a statement: “Divisive comments like Trump’s demonstrate an unfortunate lack of respect for the NFL.”

In their own statement, the Warriors accepted they would not be going to the White House. But they said they would use their trip to Washington in February – they play the Washington Wizards on the 28th of that month – to “celebrate equality, diversity and inclusion”.

On Friday, point guard Steph Curry, the NBA champions’ star player, told reporters he planned to vote no when the players came together to decide whether to visit Trump. The Warriors could “inspire some change” and “send a statement” by snubbing the president, Curry said.

On Saturday morning, Trump tweeted: “Going to the White House is considered a great honor for a championship team, Stephen Curry is hesitating, therefore invitation is withdrawn!”

In a statement issued later, the Warriors said: “While we intended to meet as a team at the first opportunity we had this morning to collaboratively discuss a potential visit to the White House, we accept that President Trump has made it clear that we are not invited.

“We believe there is nothing more American than our citizens having the right to express themselves freely on matters important to them. We’re disappointed that we did not have an opportunity during this process to share our views or have open dialogue on issues we felt would be important to raise.

“In lieu of a visit to the White House, we have decided that we’ll constructively use our trip to the nation’s capital in February to celebrate equality, diversity and inclusion – the values that we embrace as an organization.

 1 # _*_coding:utf-8_*_
 2 # 实例:词频统计
 3 # 打开文件
 4 fr = open('crity.txt','r',encoding= 'utf-8')
 5 str = fr.read()
 6 fr.close()
 7 
 8 # 排除元数的集合
 9 exc = {'','a','an','the','in','on','that','and','to','is','of','for','have','not','nfl','said','would'}
10 # 将,?.!变成空格
11 x = {',','.','\n','  ','?',':','!','','',''}
12 for i in x:
13     str = str.replace(i, ' ')
14 # 将所有大写转换为小写
15 str = str.lower()
16 # 把歌词切片
17 words = str.split(' ')
18 # 定义一个空字典
19 di = {}
20 # 单词排序
21 words.sort()
22 # 用循环,写入字典
23 disc = set(words)
24 disc = disc - exc
25 for i in disc:
26     di[i] = words.count(i)
27 wc = list(di.items())
28 # print(wc)
29 wc.sort(key = lambda x:x[1],reverse=True)
30 # print(wc)
31 print('{0:-^50}'.format('词频统计结果前20'))
32 for i in range(20):
33     print('{0} = {1}'.format(wc[i][0],wc[i][1]))

 

运行结果:

 

转载于:https://www.cnblogs.com/alliancehacker/p/7595116.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值