最后一次大作业——jieba 分词

import jieba

txt = open("聊斋志异.txt", "r", encoding="utf-8").read()
excludes = {"不知", "不可", "一日", "不敢", "数日", "家人", "以为"}
words = jieba.lcut(txt)
counts = {}

for word in words:
    if len(word) == 1 or word in excludes:
        continue
    else:
        counts[word] = counts.get(word, 0) + 1

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

for i in range(20):
    word, count = items[i]
    print("{0:<10}{1:>5}".format(word, count))

 

posted @ 2023-12-13 09:27  yu4848  阅读(31)  评论(0)    收藏  举报