首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >专题建模,文学。初学者

专题建模,文学。初学者
EN

Stack Overflow用户
提问于 2021-01-26 11:13:33
回答 1查看 82关注 0票数 1

我完全是编程初学者。我正在尝试在三本文学书籍上应用这个主题建模。我试着以Silge和Robinson的例子(文本挖掘与R,第6章)为例,区别在于我使用的不是预先存在的图书列表,而是我的选择。我遇到了一些问题,即使我在上面提到的示例中应用了给定的代码。

我下载了包(gutenbergr、tidytext、stringr、topicmodel、dplyr、tidyr)和图书,并尝试创建一个独立的对象“book”,由控制台输出指导。我想通过书本运行分析,但我发现代码示例仅按章节进行。所以我试了一下:

代码语言:javascript
复制
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”方法。

欢迎任何提示,谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-26 12:50:08

books设置为dataframe,然后可以使用它上的函数。你可以试试:

代码语言:javascript
复制
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 rows
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65900279

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档