jieba分析 西游记

import jieba
 
def takeSecond(elem):
    return elem[1]
 
def main():
    path = "西游记.txt"
    file = open(path,"r",encoding="utf-8")
    text=file.read()
    file.close()
 
    words = jieba.lcut(text)
    counts = {}
    for word in words:
        counts[word] = counts.get(word,0) + 1
 
    items = list(counts.items())
    items.sort(key = takeSecond,reverse=True)    
 
    for i in range(20):
        item=items[i]
        keyWord =item[0]
        count=item[1]
        print("{0:<10}{1:>5}".format(keyWord,count))
 
main()

 

posted @ 2020-11-13 15:55  Zhhou  阅读(146)  评论(0)    收藏  举报