TM 複習(I)
在 Text Mining: Bag of Words 課程中,你學到 corpus 是一組文本,並學過一些前處理文字的函式。快速複習一下,建立並清理 corpus 的其中一種方法如下。即使這是一門不同的課,情緒分析屬於文字探勘的一部分,所以複習一下會很有幫助。
- 使用
VectorSource()將字元向量轉為文字來源(text source)。 - 使用
VCorpus()將文字來源轉為 corpus。 - 使用
tm中的清理函式(如removePunctuation()與stripWhitespace()),以及qdap的replace_abbreviation()從 corpus 中移除不需要的字元。
在本練習中,已使用標準的前處理函式建立了一個自訂的 clean_corpus(),方便套用。
clean_corpus() 接受 VCorpus() 的輸出並套用清理函式。例如:
processed_corpus <- clean_corpus(my_corpus)
本練習屬於課程
R 情感分析
練習說明
你的 R 工作階段已載入一個文字向量 tm_define,包含兩個小文件,並提供了 clean_corpus() 函式。
- 將
VectorSource()套用到tm_define,建立名為tm_vector的物件。 - 對
tm_vector使用VCorpus()建立tm_corpus。 - 使用
content()檢視tm_corpus中第一個文件的內容。- corpus 中的文件以清單語法存取,所以請使用雙中括號,例如
[[1]]。
- corpus 中的文件以清單語法存取,所以請使用雙中括號,例如
- 使用自訂函式
clean_corpus()清理tm_corpus的文字。將新物件命名為tm_clean。 - 再次檢視新物件
tm_clean的第一個文件,看看套用clean_corpus()後文字有何變化。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# clean_corpus(), tm_define are pre-defined
clean_corpus
tm_define
# Create a VectorSource
tm_vector <- ___
# Apply VCorpus
tm_corpus <- ___
# Examine the first document's contents
___(___[[___]])
# Clean the text
tm_clean <- ___
# Reexamine the contents of the first doc
___