BoW 範例
在文獻回顧中,研究者會盡可能閱讀並整理某個主題的所有相關文本。有時他們可能會讀到重複的文章,或是已讀文章的摘要。現在你拿到 20 篇與原油相關的文章,已經存成名為 crude_tibble 的 R 物件。與其馬上逐篇閱讀,你決定先看看這些文章之間有哪些共同的詞彙。為了達成這點,你會先建立文字的 bag-of-words(詞袋)表示法。
本練習屬於課程
R 的自然語言處理入門
練習說明
- 使用欄位
article_id,依文章計算單字出現次數,建立一個 BoW 表示法。 - 使用輸出結果判斷建立了多少個唯一的「文章/單字」組合。
- 將結果篩選為只包含
'prices'的提及。 - 有多少篇文章使用了單字
prices?
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Count occurrence by article_id and word
words <- crude_tibble %>%
unnest_tokens(output = "word", token = "words", input = text) %>%
anti_join(stop_words) %>%
count(___, ___, sort=TRUE)
# How many different word/article combinations are there?
unique_combinations <- nrow(___)
# Filter to responses with the word "prices"
words_with_prices <- words %>%
___(word == "___")
# How many articles had the word "prices"?
number_of_price_articles <- nrow(___)