簡體   English   中英

主題建模,文學。 初學者

[英]topic modelling, literature. Beginner

我完全是編程和 R 的初學者。 我正在嘗試將主題建模應用於三本文學書籍。 我嘗試以 Silge 和 Robinson 的示例為例(使用 R 進行文本挖掘,第 6 章),不同之處在於我沒有使用預先存在的書籍列表,而是使用我的選擇。 我遇到了問題,即使我在上面提到的示例中應用了給定的代碼。

我下載了軟件包(gutenbergr、tidytext、stringr、topicmodels、dplyr、tidyr)和書籍,並嘗試創建一個單獨的 object “書籍”,由控制台引導 Z78E6221F63983F1356681DB3 我想按書運行分析,但我只按章節找到代碼示例。 所以我嘗試了這個:

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) 

我在控制台中收到:“取消組合”方法不能應用於 class“列表”的對象,它找不到 object 章節,它找不到“取消組合”方法。

歡迎任何提示,提前謝謝

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 rows

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM