python簡單詞頻統計

詞頻統計

簡單英語詞頻統計

# 詞頻:單詞出現的次數


f = open(r'D:\上海Python11期視頻\預科班\hamlet.txt','r',encoding='utf8')
data = f.read().lower()
# print(data)
data_split = data.split(' ')
# print(data_split)

count_dict = {}
for word in data_split:
    if word not in count_dict:
        count_dict[word] = 1
    else:
        count_dict[word] += 1

# print(count_dict)
def func(i):
    return i[1]

lt = list(count_dict.items())
lt.sort(key=func)

lt.reverse()

for i in lt[0:10]:
    print(f'{i[0]:^7}{i[1]^5}')

簡單中文詞頻統計

import jieba

f = open(r'F:\pythonxm\7.19day06\threekingdoms.txt','r',encoding='utf8')

data = f.read()

data_jieba = jieba.lcut(data)

count_dict = {}
for word in data_jieba :
    if len(word) == 1 :
        continue

    if word in {"第二天","引兵","軍馬","左右","將軍", "卻說", "荊州", "二人","如何","軍士","不可", "不能", "如此", "商議"}:
        continue
    if '曰' in word :
        word = word.replace('曰','')
    if word == '玄德' :
        word = '劉備'
    # if word == '孔明曰':
    #     word = '孔明'
    # elif word == '玄德曰':
    #     word = '玄德'
    if word in count_dict :
        count_dict[word] += 1
    else:
        count_dict[word] = 1
def func(i):
    return i[1]
data_list = list(count_dict.items())

data_list.sort(key=func)
data_list.reverse()
for i in data_list[0:10] :
    print(f'{i[0]:^4}{i[1] ^ 6}')

做  者:豆瓣醬瓣豆
出  處:https://www.cnblogs.com/chenziqing/
查看其它博文請點擊:https://www.cnblogs.com/chenziqing/
聲援博主:若是您以爲文章對您有幫助,能夠點擊文章右下角【推薦】一下。您的鼓勵是博主的最大動力!python

微信:

相關文章
相關標籤/搜索