可以下载一长篇的英文小说,进行词频的分析。
1.读入待分析的字符串
2.分解提取单词
3.计数字典
4.排除语法型词汇
5.排序
6.输出TOP(20)
7.对输出结果的简要说明。
fight=open('C:/Users/ASUS/Desktop/why men fight.txt','r') fight=fight.read() fight=fight.lower()#转换成小写 for i in ",...'": fight=fight.replace(i," ") fight=fight.split()#单词列表 ex={'the','to','of','it','a','not','as','and','may','by','what','make','who','one','they','is','in','are','them','itself','been','though','from','be','an','all','but','will','has','which','be','for'} dic={} keys=set(fight)-ex#键的集合 for i in keys: dic[i]= fight.count(i)#单词计数 fight=list(dic.items())#字典转换成列表,由元组组成 fight.sort(key=lambda x:x[1],reverse=True)#排序 for i in range(20): print(fight[i])

从上面代码的运行结果,我们可以看出作者讨论的是战争的一个观点,它在政治和哲学的方面是普遍的,会使人失去信仰,没有同情。