利用python分析《愛麗絲漫游仙境》詞頻

需要解決的問(wèn)題:

1.去掉標(biāo)點(diǎn),英文是英文著作,因此對(duì)形如「Damon's」還要對(duì)「'」作替換成空格處理,防止影響結(jié)果

2.將列表中的大小寫統(tǒng)一(時(shí)態(tài)、單復(fù)數(shù)暫時(shí)不考慮)

3.還需要一個(gè)去重的列表

下面是1st_version代碼

file_name = "Alice's Adventures in Wonderland.txt" #這里求方便,我們把文檔和.py放在同一目錄下

with open(file_name) as f_obj:

? ? contents = f_obj.read()

? ? words = contents.replace("'"," ") #首先將「'」替換成空格


import re #利用正則表達(dá)式去掉文中的標(biāo)點(diǎn)

r='[’!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~]' #注意這里的格式是'[要去除的內(nèi)容]'

clean_words = re.sub(r,'',words)


split_words =? clean_words.split() #將文中單詞劃分開,形成原始的詞匯表


complete_words = [] #建立一個(gè)空的列表

for word in split_words:?

? ? lowercase_word = word.lower() #將列表中的元素利用遍歷統(tǒng)一格式

? ? complete_words.append(lowercase_word) #這里是把統(tǒng)一化的詞匯放在新的列表中


unique_words = list(set(complete_words)) #這里是去重之后的列表

frequencies = {} #設(shè)立新的字典

for word in unique_words:

? ? frequency = complete_words.count(word)

? ? frequencies[word] = frequency

print(frequencies)?


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容