建立 dictionary 與 corpus
為了執行 LDA 主題模型,你必須先「建立字典(dictionary)與語料庫(corpus)」,因為模型會用到它們。你會延續先前練習中已經整理乾淨的文字資料,也就是 text_clean 已經可供你接著使用,你將用它來建立字典與語料庫。
這個練習的執行時間會比平常稍長。
本練習屬於課程
Fraud Detection in Python
練習說明
- 分別匯入 gensim 套件與 gensim 中的 corpora。
- 對乾淨資料
text_clean呼叫正確的函式來建立字典(dictionary)。 - 對
text_clean中的每段文字執行doc2bow,以建立語料庫(corpus)。 - 列印結果,檢查看看
dictionary與corpus的內容。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import the packages
import ____
from ____ import ____
# Define the dictionary
dictionary = ____.____(____)
# Define the corpus
corpus = [dictionary.____(text) for ___ in ____]
# Print corpus and dictionary
print(____)
print(____)