文件方式实现完整的英文词频统计实例

读入待分析的字符串
>>> fo=open('test.txt','w') >>> fo.write('''The very thought of you leaving my life Broke me down in tears. I took for granted all the love That you gave to me I know that's what I feared Don't go away. Every heart beat Every moment Everything I see is you Please forgive me. I'm so sorry Don't say we're through Girl I need you to be by my side Girl I need you to open up my eyes Cause without you where would I be Yes I need you Come back to me A kiss is not a kiss Without your lips kissing mine You bring me paradise I can't live if you took your love Away from me Without you I would die. Every second Every minute Every time I close my eyes. I could feel you So I want you To stay in my life Girl I need you to be by my side Girl I need you to open up my eyes Cause without you where would I be Yes I need you Come back to me Every second, Every minute, Every time I close my eyes, I could feel you. So I want you To always be mine Girl I need you to be by my side Girl I need you to open up my eyes Cause without you where would I be Yes I need you come back to me Girl I need you to be by my side Girl I need you to open up my eyes Cause without you where would I be Yes I need you come back to me Girl I need you to be by my side Girl I need you to open up my eyes Cause without you where would I be Yes I need you come back to me''') 1314 >>> fo.close <built-in method close of _io.TextIOWrapper object at 0x0000000002EEAB40> >>> fo.close() >>> fo=open('test.txt','r') >>> fo.read() "The very thought of you leaving my life\nBroke me down in tears.\nI took for granted all the love\nThat you gave to me\nI know that's what I feared\nDon't go away.\nEvery heart beat\nEvery moment\nEverything I see is you\nPlease forgive me.\nI'm so sorry\nDon't say we're through\nGirl I need you to be by my side\nGirl I need you to open up my eyes\nCause without you where would I be\nYes I need you\nCome back to me\nA kiss is not a kiss\nWithout your lips kissing mine\nYou bring me paradise\nI can't live if you took your love\nAway from me\nWithout you I would die.\nEvery second\nEvery minute\nEvery time I close my eyes.\nI could feel you\nSo I want you\nTo stay in my life\nGirl I need you to be by my side\nGirl I need you to open up my eyes\nCause without you where would I be\nYes I need you\nCome back to me\nEvery second,\nEvery minute,\nEvery time I close my eyes,\nI could feel you.\nSo I want you\nTo always be mine\nGirl I need you to be by my side\nGirl I need you to open up my eyes\nCause without you where would I be\nYes I need you come back to me\nGirl I need you to be by my side\nGirl I need you to open up my eyes\nCause without you where would I be\nYes I need you come back to me\nGirl I need you to be by my side\nGirl I need you to open up my eyes\nCause without you where would I be\nYes I need you come back to me" >>> fo.close() >>>

排除语法型词汇,分解提取单词,计数字典,排序,输出top(20)

exc={'the','a','to','of','and','in','that','on',''}
news=news.lower()
for i in ',.!':
    news=news.replace(i,' ')
words=news.split(' ')
print(words)

dic={}
keys=set(words)
print(keys)
keys=keys-exc

for i in keys:
    dic[i]=words.count(i)
print(dic)
fo.close()
hh=list(dic.items())#列表
hh.sort(key=lambda x:x[1],reverse=True)#5.排序
print(hh)

for i in range(20):#6.输出TOP(20)
    print(hh[i])

 

 

posted on 2017-09-26 09:50  133陈贝  阅读(377)  评论(0编辑  收藏  举报