jieba
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}")

                    
                
                
            
        
浙公网安备 33010602011771号