BoW 示例
在文献综述中,研究者会尽可能多地阅读并总结某一主题的文本。有时他们会读到重复的文章,或者已读文章的摘要。您拿到了一份包含 20 篇关于原油的文章,作为名为 crude_tibble 的 R 对象。与其立刻逐篇阅读,您决定先看看这些文章中有哪些词是共同出现的。为此,您将先构建文本的词袋(bag-of-words,BoW)表示。
本练习是课程的一部分
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(___)