1.读入待分析的字符串
2.分解提取单词
3.计数字典
4.排除语法型词汇
5.排序
6.输出TOP(20)
fo=open('dead romance.txt','w')fo.write('''in a rainy nightcan you hear mein a rainy nightcan you help meman,what are you thinking of man,what do you needman,nobody tell you what to doman ,you need somebody to hurtin a rainy nightcan you hear mein a rainy nightcan you help meman,you feel so lonelyman,can you hear the message come from the skyman,you are driving into the rainman,you know it's time to find the preyin a rainy nightcan you hear mein a rainy nightcan you help me''')fo.close()fo=open('dead romance.txt','r')A= fo.read()exc={'the','and','to','of','in','a','for','with',''}for i in ',.?!\n"': A=A.replace(i,' ')A=A.lower()A=A.split(" ")words=set(A)dic={}keys=set(A)#出现过单词的集合,字典的KEYkeys=keys-excfor i in keys: dic[i]=A.count(i)w=list(dic.items())w.sort(key=lambda x:x[1],reverse=True)for i in range(20): print(w[i])fo.close()