中文词频统计

  1. 下载一中文长篇小说,并转换成UTF-8编码。
  2. 使用jieba库,进行中文词频统计,输出TOP20的词及出现次数。
  3. 排除一些无意义词、合并同一词。
import jieba
fo = open('骆驼祥子.txt','r',encoding='utf-8').read()
words = list(jieba.cut(fo,cut_all=True))#全

for i in ''',。‘’“”?!\n \n\n\u3000\u3000''':
    fo = fo.replace(i,' ')
words = fo.split(' ')#词的列表

dic={}
keys=set(words)
for w in keys:
    dic[w]=words.count(w)

wc = list(dic.items()) #函数列表形式
wc.sort(key= lambda x:x[1],reverse=True)

for i in range(20):
    print(wc[i])

方法二:

import jieba
fo = open('骆驼祥子.txt','r',encoding='utf-8').read()
words = list(jieba.cut(fo,cut_all=True))#

exp={''',。‘’“”?!\n \n\n\u3000\u3000'''}
dic={ }
words=set(words)-exp
for w in words:
    if len(w)==1:
        continue
    else:
        dic[w]=dic.get(w,0)+1

wc = list(dic.items())
wc.sort(key= lambda x:x[1],reverse=True)

for i in range(20):
    print(wc[i])

结果:

('', 1)
('外国', 1)
('啪啪', 1)
('跳墙', 1)
('洛夫斯', 1)
('彼此', 1)
('再说', 1)
('力量', 1)
('而言', 1)
('发现', 1)
('湿透', 1)
('吸着', 1)
('目不旁视', 1)
('抬起头来', 1)
('游街', 1)
('干燥', 1)
('只要', 1)
('民歌', 1)
('卖点', 1)
('怀着', 1)

posted on 2017-09-29 09:58  004陈楠芸  阅读(173)  评论(0编辑  收藏  举报