- 词频统计预处理
- 下载一首英文的歌词或文章
- 将所有,.?!’:等分隔符全部替换为空格
- 将所有大写转换为小写
- 生成单词列表
- 生成词频统计
- 排序
- 排除语法型词汇,代词、冠词、连词
- 输出词频最大TOP10
str=''' Every night in my dreams I see you,I feel you That is how I know you go on Far across the distance And spaces between us You have come to show you go on Near far Wherever you are I believe That the heart does go on Once more you open the door And you're here in my heart And my heart will go on and on Love can touch us one time And last for a lifetime And never let go till we're gone Love was when I loved you One true time I hold to In my life well always go on Near far Wherever you are I believe That the heart does go on Once more you open the door And you're here in my heart And my heart will go on and on you're here There's nothing I fear And I know That my heart will go on We'll stay forever this way You are safe in my heart And my heart will go on and on ''' #逗号和略写 , ' sym=list("',") for i in sym: str=str.replace(i," ") #小写 str=str.lower() #按空格分裂单词 str=str.split() dictionary=dict() #单词和个数存到字典中 for i in str: dictionary[i]=str.count(i) #语法词删去 pron = ["for", "to", "the", "and", "of", "in","that","have","has","a","can","not"] for i in pron: if i in dictionary.keys(): del dictionary[i] #按value值大小排序 dictionary=sorted(dictionary.items(),key=lambda item:item[1],reverse=True) #输出 for i in range(10): print(dictionary[i])
结果: