文字詞頻統計是字典嗎 Python 文字詞頻統計

2021-10-16 06:26:03 字數 584 閱讀 6161

很多時候需要對一篇文章統計其中多次出現詞語,進而分析文章的內容,這就需要用到詞頻統計。詞頻統計就是累加問題,即對文件中每個詞設定乙個計數器,詞語每出現一次,相關計數器就加一次。

def gettext(): text = open('ceshi.txt','r').read() text = text.lower() for ch in '!''#*()+-:;,?>@^_』~': text =text.replace(ch," ") return texttext = gettext()words = text.split()counts={}for word in words: counts[word] = counts.get(word,0)+1items = list(counts.items())items.sort(key=lambda x:x[1],reverse=true)for i in range(10): word,count=items[i] print(" ".format(word,count))
結果如下:

Python 文字詞頻統計

hamlettxt gettext words hemlettxt.split counts for word in words counts word counts.get word,0 1這是一段遍歷hamlet.txt檔案的一段 s.split 函式返回的是列表list 我有一些困惑 1.最後...

coding 文字的詞頻統計

給定一英文文字檔案data.dat,編寫c 程式,讀取檔案中的內容,統計檔案中出現次數最多的三個單詞,並給出這三個單詞的出現次數,同時輸出程式執行的時間。注 這裡不區分單詞大小寫,如,he 與 he 當做是同乙個單詞計數 include include include include include...

例項 文字詞頻統計

降噪,避免大小寫的干擾 用空格替換特殊符號 for ch in txt txt.replace ch,return txt hamletxt gettext words hamletxt.split counts for word in words counts word counts.get wo...