可以下载一长篇的英文小说,进行词频的分析。
1.读入待分析的字符串
2.分解提取单词
3.计数字典
4.排除语法型词汇
5.排序
6.输出TOP(20)
7.对输出结果的简要说明。
答:
#读入待分析的字符串 fo=open('fight.txt','r') str=fo.read() fo.close() #分解提取单词 str=str.lower() for i in ',.!': str=str.replace(i,' ') words=str.split(' ') #排除语法型词汇 exp={'','the','to','and','is','in','are','which','it','be','that','of', 'a','by','not','as','or','an','has'} #计数字典 dic={} keys=set(words)-exp for i in keys: dic[i]=words.count(i) d=list(dic.items()) #排序 d.sort(key=lambda x:x[1],reverse=True) #输出TOP(20) for i in range(20): print(d[i])
结果如图:
从结果中可以看出impulse的单词出现率最高,这是一篇关于人类与冲动与斗争的故事