開始使用免費開始

建立 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(____)
編輯並執行程式碼