我完全是编程初学者。我正在尝试在三本文学书籍上应用这个主题建模。我试着以Silge和Robinson的例子(文本挖掘与R,第6章)为例,区别在于我使用的不是预先存在的图书列表,而是我的选择。我遇到了一些问题,即使我在上面提到的示例中应用了给定的代码。
我下载了包(gutenbergr、tidytext、stringr、topicmodel、dplyr、tidyr)和图书,并尝试创建一个独立的对象“book”,由控制台输出指导。我想通过书本运行分析,但我发现代码示例仅按章节进行。所以我试了一下:
library(gutenbergr)
library(tidytext)
library(stringr)
library(topicmodels)
library(dplyr)
library(tidyr)
wuthering_heights <- gutenberg_download(768, mirror = "http://mirrors.xmission.com/gutenberg/")
utopia <- gutenberg_download(2130, mirror = "http://mirrors.xmission.com/gutenberg/")
the_grand_inquisitor <- gutenberg_download(8578, mirror = "http://mirrors.xmission.com/gutenberg/")
titles <- c("Wuthering Heights", "Utopia", "The Grand Inquisitor")
books <- gutenberg_works(title %in% titles) %>%
gutenberg_download(meta_fields = "title")
books <- list(wuthering_heights, utopia, the_grand_inquisitor)
by_chapter = books %>%
group_by(title) %>%
mutate(chapter = cumsum(str_detect(text, regex("^chapter ", ignore_case = TRUE)))) %>% # ignore the word chapter
ungroup() %>%
filter(chapter > 0) %>%
unite(document, title, chapter)
by_chapter_word = by_chapter %>%
unnest_tokens(word, text) 我在控制台上看到:"ungroup“方法不能应用于类”列表“的对象,它找不到对象章节,也找不到"ungroup”方法。
欢迎任何提示,谢谢。
发布于 2021-01-26 12:50:08
将books设置为dataframe,然后可以使用它上的函数。你可以试试:
library(tidyverse)
library(tidytext)
books <- lst(wuthering_heights, moby_dick, great_expectations)
bind_rows(books, .id = 'books') %>%
unnest_tokens('word', 'text') %>%
filter(!word %in% stop_words$word) %>%
count(books, word) %>%
group_by(books) %>%
slice_max(n, n = 10) -> top10words
top10words
# books word n
# <chr> <chr> <int>
# 1 the_grand_inquisitor thou 104
# 2 the_grand_inquisitor thee 61
# 3 the_grand_inquisitor thy 51
# 4 the_grand_inquisitor bread 28
# 5 the_grand_inquisitor freedom 28
# 6 the_grand_inquisitor hast 24
# 7 the_grand_inquisitor god 22
# 8 the_grand_inquisitor inquisitor 19
# 9 the_grand_inquisitor earth 18
#10 the_grand_inquisitor weak 18
# … with 20 more rowshttps://stackoverflow.com/questions/65900279
复制相似问题