將向量轉成 VCorpus 物件(2)
現在我們已經把向量轉成 Source 物件了,接下來把它傳給另一個 tm 函式 VCorpus(),就能建立 volatile 語料庫。相當直覺,對吧?
VCorpus 物件是一個巢狀清單(list of lists)。在 VCorpus 的每個索引位置,都有一個 PlainTextDocument 物件。它本身是清單,包含實際的文字資料(content)以及對應的中繼資料(meta)。你可以參考這張圖來視覺化 VCorpus 的結構,幫助你掌握整體概念。
要檢視單一文件物件(第 10 個),使用雙層中括號取子集。
coffee_corpus[[10]]
要查看實際的「文字」內容,對清單取兩次索引。若要存取該文件的中繼資料(例如時間戳記),把 [1] 改成 [2]。另一個檢視純「文字」的方法是使用 content() 函式,它不需要第二組中括號。
coffee_corpus[[10]][1]
content(coffee_corpus[[10]])
本練習屬於課程
R 的 Bag-of-Words 文本探勘
練習說明
- 對
coffee_source物件呼叫VCorpus(),建立coffee_corpus。 - 將
coffee_corpus列印到主控台,確認它是VCorpus物件。 - 將
coffee_corpus的第 15 個元素列印到主控台,確認它是PlainTextDocument,且包含第 15 則推文的內容與中繼資料。使用雙中括號取子集。 - 列印
coffee_corpus中第 15 則推文的內容。先用雙中括號選到該推文,再用單中括號取出其內容。 - 列印
coffee_corpus中第 10 則推文的content()。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
## coffee_source is already in your workspace
# Make a volatile corpus from coffee_source
coffee_corpus <- ___
# Print out coffee_corpus
___
# Print the 15th tweet in coffee_corpus
___
# Print the contents of the 15th tweet in coffee_corpus
___
# Now use content to review the plain text of the 10th tweet
___(___[[___]])