jieba分詞
尾號為1,2,3的同學做,西游記相關的分詞,出現次數最高的20個。
```import jieba # 讀取文本文件 path = "西游記.txt" file = open(path, "r", encoding="utf-8") text = file.read() file.close() # 使用jieba分詞 words = jieba.lcut(text) # 統計詞頻 counts = {} for word in words: # 過濾掉長度為1的詞語 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) # 輸出前20個高頻詞語 for i in range(20): word, count = items[i] print(f"{word:<10}{count:>5}")
浙公網安備 33010602011771號