1.读入待分析的字符串

fo=open('test.txt','w')
fo.write('''You gotta go and get angry at all of my honesty
You know I try but I don’t do too well with apologies
I hope I don’t run out of time, could someone call a referee?
Cause I just need one more shot at forgiveness
I know you know that I made those mistakes maybe once or twice
By once or twice I mean maybe a couple a hundred times
So let me, oh let me redeem, oh redeem, oh myself tonight
Cause I just need one more shot at second chances
Yeah, is it too late now to say sorry?
Cause I’m missing more than just your body
Is it too late now to say sorry?
Yeah I know that I let you down
Is it too late to say I’m sorry now?
I’m sorry, yeah
Sorry, yeah
Sorry
Yeah I know that I let you down
Is it too late to say sorry now?
I’ll take every single piece of the blame if you want me to
But you know that there is no innocent one in this game for two
I’ll go, I’ll go and then you go, you go out and spill the truth
Can we both say the words and forget this?
Is it too late now to say sorry?
Cause I’m missing more than just your body
Is it too late now to say sorry?
Yeah I know that I let you down
Is it too late to say I’m sorry now?
I’m not just trying to get you back on me
Cause I’m missing more than just your body
Is it too late now to say sorry?
Yeah I know that I let you down
Is it too late to say sorry now?
I’m sorry, yeah
Sorry, oh
Sorry
Yeah I know that I let you down
Is it too late to say sorry now?
I’m sorry, yeah
Sorry, oh
Sorry
Yeah I know that I let you down
Is it too late to say sorry now?''')
fo.close()
fo=open('test.txt','r')
sorry=fo.read()

2.分解提取单词

3.计数字典

4.排除语法型词汇

 

exc={'the','a','that','to','you','it','more'}

sorry=sorry.lower()
for i in '?,':
    sorry=sorry.replace(i,' ')#全部小写
words=sorry.split(' ')#以空格分隔
print(words)

dic={}#定义一个空字典
words.sort()#排列切片好的单词
d=set(words)#集合d的元素就是切片好的单词
d=d-exc
for i in d:
    dic[i]=words.count(i)#循环插入值为空的主键i

5.排序

6.输出TOP(20)

wc=list(dic.items())
wc.sort(key=lambda x:x[1],reverse=True)#排序
for i in range(20):
    print(wc[i])

运行结果:

 

posted on 2017-09-26 13:07  朱茵茵  阅读(611)  评论(0编辑  收藏  举报