資料前處理
在 2016 年美國大選期間,俄羅斯的推文機器人持續向民主黨與共和黨陣營散播政治訊息。你拿到一個此類推文的資料集,名為 russian_tweets。你決定將這些推文分類為偏左(民主黨)或偏右(共和黨)。在建立分類模型之前,你需要先清理並準備文字以便建模。
本練習屬於課程
R 的自然語言處理入門
練習說明
- 透過詞幹化完成權杖化(tokenization)流程。
- 使用
cast_dtm()建立 document-term 矩陣。 - 以 tf-idf 權重為 document-term 矩陣加權。
- 列印該矩陣。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Stem the tokens
russian_tokens <- russian_tweets %>%
unnest_tokens(output = "word", token = "words", input = content) %>%
anti_join(stop_words) %>%
___(word = ___(word))
# Create a document term matrix using TFIDF weighting
tweet_matrix <- russian_tokens %>%
count(tweet_id, word) %>%
___(document = ___, term = ___,
value = n, weighting = tm::___)
# Print the matrix details
___