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

2021-09-25 22:25:41 字數 1196 閱讀 6414

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:    #

建立計數字典

dict[i]=words.count(i)

4.排除語法型詞彙

dict={}         #

排除語法型詞彙

ecp=set(['','

a','

an','

the','

and','

to','

in','

on','

of','

for','

i','

our','

us','

into

','her

','we',

'when

','their

','my

','from

','them

','with

','after

','would

','was

','had

','that

','while',

'his

','she

','up

','it

','they

','so

','by

'])

5.排序

items.sort(key=lambda x:x[1],reverse=true)
6.輸出top(20)

print('

輸出top20:')

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

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,所有標點符號替換為空格...

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

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...