jieba库及词频统计

 1 import jieba
 2 txt = open("C:\\Users\\Administrator\\Desktop\\流浪地球.txt", "r", encoding='utf-8').read()
 3 words  = jieba.lcut(txt)
 4 counts = {}
 5 for word in words:
 6     if len(word) == 1:  #排除单个字符的分词结果
 7         continue
 8     else:
 9         counts[word] = counts.get(word,0) + 1
10 items = list(counts.items())
11 items.sort(key=lambda x:x[1], reverse=True) 
12 for i in range(10):
13     word, count = items[i]
14     print ("{0:<10}{1:>5}".format(word, count))

posted @ 2019-04-03 23:14  简笺  阅读(603)  评论(0)    收藏  举报