檔案方式實現完整的英文詞頻統計例項

2022-08-31 01:51:08 字數 1379 閱讀 3376

1.讀入待分析的字串

2.分解提取單詞 

3.計數字典

4.排除語法型詞彙

5.排序

6.輸出top(20)

7.對輸出結果的簡要說明。

fo=open('

english.txt

','r')

s=fo.read()

s=s.lower()

#大寫轉換為小寫

for i in

',.!':

s=s.replace(i,'')

#所有標點符號替換為空格

#單詞列表及詞頻

words=s.split('')

#不統計單詞的集合

exc=

dic={}

key=set(words)

key=key-exc #

鍵的集合

for i in

key:

dic[i] = words.count(i)#

單詞計數字典

wc=list(dic.items()) #

(單詞,計數)元組的列表

wc.sort(key=lambda x:x[1],reverse=true)#

列表的排序

for i in range(20):#

輸出前15個元組

print(wc[i])

('

human

', 5)('

development

', 4)('

resources

', 4)('

world

', 3)('

unprecedented

', 3)('

future

', 3)('

own', 3)('

beings

', 3)('

space

', 3)('

one', 3)('

environment

', 3)('

generations

', 2)('

limited

', 2)('

us', 2)('

straight

', 2)('

depleted

', 2)('

their

', 2)('

natural

', 2)('

technology

', 2)('

intelligent

', 2)

對輸出結果的簡要說明,環境對我們和我們的後代來說是多麼重要,我們應該用一種更加關愛環境的方法來生活以便我們的子孫後代也能有空間和資源生存。

檔案方式實現完整的英文詞頻統計例項

1.讀入待分析的字串 fo open test.txt r 讀入待分析的字串 str fo.read fo.close 2.分解提取單詞 for i in n 分解提取單詞 str str.replace i,words str.split 3.計數字典 for i in keys 建立計數字典 d...

檔案方式實現完整的英文詞頻統計例項

1.讀入待分析的字串 2.分解提取單詞 3.計數字典 4.排除語法型詞彙 5.排序 6.輸出top 20 7.對輸出結果的簡要說明。a open text.txt r new aa.read aa.close exc new new.lower for i in new new.replace i,...

英文詞頻統計

詞頻統計預處理 將所有,等分隔符全部替換為空格 將所有大寫轉換為小寫 生成單詞列表 生成詞頻統計 排序排除語法型詞彙,代詞 冠詞 連詞 輸出詞頻最大top10 word lately,i ve been,i ve been losing sleep dreaming about the things...