開始使用免費開始

視覺化不相似的單字

假設你想要視覺化彼此不重複的單字。你也可以使用 comparison.cloud(),步驟和前面很像,但有一個主要差異。

就像你在找共同單字時一樣,先把推文整理成各自的語料庫,並合併成一個 VCorpus() 物件。接著套用 clean_corpus() 函式,並將其整理為 TermDocumentMatrix

為了標示哪些字屬於 coffee、哪些屬於 chardonnay,你可以這樣設定 TDM 的欄名:

colnames(all_tdm) <- c("chardonnay", "coffee")

最後,使用 as.matrix() 將物件轉成矩陣,供 comparison.cloud() 使用。對於傳入 comparison.cloud() 的每個獨立語料庫,你都可以指定顏色,例如 colors = c("red", "yellow", "green"),以便區分各區塊。

本練習屬於課程

R 的 Bag-of-Words 文本探勘

檢視課程

練習說明

all_corpus 已預先載入你的工作空間。

  • 將預先定義的 clean_corpus 函式套用到 all_corpus,建立 all_clean
  • all_clean 建立 TermDocumentMatrix,命名為 all_tdm
  • 使用 colnames() 重新命名 all_tdm 中各個獨立語料庫的欄名。將第一欄命名為 "coffee",第二欄命名為 "chardonnay"。
  • all_tdm 轉為矩陣形式建立 all_m
  • 使用 all_m 建立一個 comparison.cloud(),並設定 colors = c("orange", "blue")max.words = 50

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Clean the corpus
___ <- ___(___)

# Create all_tdm
___ <- ___(___)

# Give the columns distinct names
___(___) <- ___

# Create all_m
___ <- ___(___)

# Create comparison cloud
comparison.cloud(___, ___ = c("___", "___"), max.words = ___)
編輯並執行程式碼