综合练习:英文词频统计

  1. 词频统计预处理
  2. 下载一首英文的歌词或文章
  3. 将所有,.?!’:等分隔符全部替换为空格
  4. 将所有大写转换为小写
  5. 生成单词列表
  6. 生成词频统计
  7. 排序
  8. 排除语法型词汇,代词、冠词、连词
  9. 输出词频最大TOP10
 1 # -*- coding: UTF-8 -*-
 2 # -*- author: yjw -*-
 3 
 4 Music="""Oh if there's one thing to be taught
 5 It's dreams are made to be caught
 6 And friends can never be bought
 7 Doesn't matter how long it's been
 8 I know you'll always jump in
 9 'Cause we don't know how to quit
10 Let's start a riot tonight
11 A pack of lions tonight
12 In this world, he who stops, won't get anything he wants
13 Play like the top one percent
14 Til nothing's left to be spent
15 Take it all, ours to take, celebrate because
16 We are the champions
17 Setting it off again
18 Oh we on fire
19 We on fire
20 Running our own campaign
21 Doing the whole shebang
22 Oh we on fire
23 We on fire
24 Get up, stand up, throw your hands up
25 Welcome to the other land of
26 Dreamers brothers sisters others
27 Yeah we on fire like that
28 Oh the bond is deeper than skin
29 The kind of club that we're in
30 The kind of love that we give
31 Oh ever since the dawn of mankind
32 We all belong to a tribe
33 It's good to know this one's mine
34 Let's start a riot tonight
35 A pack of lions tonight
36 In this world, he who stops, won't get anything he wants
37 Play like the top one percent
38 Til nothing's left to be spent
39 We don't care, we won't stop, call your mothers, call the cops
40 We are the champions
41 Setting it off again
42 Oh we on fire
43 We on fire
44 Running our own campaign
45 Doing the whole shebang
46 Oh we on fire
47 We on fire
48 Get up, stand up, throw your hands up
49 Welcome to the other land of
50 Dreamers brothers sisters others
51 Yeah we on fire like that
52 Ohh (fire, fire)
53 Ooooh (fire, fire)
54 And nothing's gonna be the same (fire fire, fire fire, fire fire)
55 Oh! We're the champions
56 We are the champions
57 Setting it off again
58 Oh we on fire
59 We on fire
60 Running our own campaign
61 Doing the whole shebang
62 Oh we on fire
63 We on fire
64 Dit dit heart and soul
65 Hey and nothing's going to be the same
66 Hey the life that you made will not be today"""
67 
68 Sign=[',','.','?','!','(',')',"'"]
69 article=['a','in','of','the','to','at','it','on','and','so','his','that','not','was','my','were','we','he','an','as','is','for','mr','us','oh']
70 
71 for x in Sign:
72     Music2=Music.replace(x,'')
73 
74 Music2=Music2.lower()
75 Music2=Music2.split()
76 
77 art={}
78 for y in Music2:
79     frequency=Music2.count(y)
80     art[y]=frequency
81 
82 for z in article:
83     if z in art.keys():
84         del art[z]
85 
86 art=sorted(art.items(),key=lambda item:item[1],reverse=True)
87 for i in range(10):
88     print(art[i])

截图:

原文地址:https://www.cnblogs.com/yjwamao/p/8647604.html