jieba

发布时间 2023-12-29 00:42:40作者: Partikel
import jieba
path = "聊斋.txt"
file = open(path, "r", encoding="utf-8")
text = file.read()
file.close()

words = jieba.lcut(text)

counts = {}
for word in words:
    if len(word) == 1:
        continue
    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(f"{word:<10}{count:>5}")