可以下载一长篇的英文小说,进行词频的分析。
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) >>>