url <- "https://www.o-bible.com/download/kjv.txt"
bible <- readLines(url, warn = FALSE)
head(bible)
## [1] "Holy Bible, Authorized (King James) Version, Textfile 930105."                                                                                       
## [2] "Ge1:1 In the beginning God created the heaven and the earth."                                                                                        
## [3] "Ge1:2 And the earth was without form, and void; and darkness was upon the face of the deep. And the Spirit of God moved upon the face of the waters."
## [4] "Ge1:3 And God said, Let there be light: and there was light."                                                                                        
## [5] "Ge1:4 And God saw the light, that it was good: and God divided the light from the darkness."                                                         
## [6] "Ge1:5 And God called the light Day, and the darkness he called Night. And the evening and the morning were the first day."
genesis <- grep("^Ge", bible, value = TRUE, ignore.case = TRUE)
head(genesis)
## [1] "Ge1:1 In the beginning God created the heaven and the earth."                                                                                        
## [2] "Ge1:2 And the earth was without form, and void; and darkness was upon the face of the deep. And the Spirit of God moved upon the face of the waters."
## [3] "Ge1:3 And God said, Let there be light: and there was light."                                                                                        
## [4] "Ge1:4 And God saw the light, that it was good: and God divided the light from the darkness."                                                         
## [5] "Ge1:5 And God called the light Day, and the darkness he called Night. And the evening and the morning were the first day."                           
## [6] "Ge1:6 And God said, Let there be a firmament in the midst of the waters, and let it divide the waters from the waters."
library(dplyr)
## 
## 다음의 패키지를 부착합니다: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
genesis_text <- paste(genesis, collapse = " ")
genesis_text <- tolower(genesis_text)
genesis_text <- gsub("[[:punct:]]", "", genesis_text)

words <- strsplit(genesis_text, "\\s+")
words <- unlist(words)
words <- words[words != ""]
stop <- c(

  "a", "about", "above", "across", "after", "again", "against", "all", "almost", "alone",

  "along", "already", "also", "although", "always", "am", "among", "an", "and", "another",

  "any", "anybody", "anyone", "anything", "anywhere", "are", "area", "areas", "aren't", "around",

  "as", "ask", "asked", "asking", "asks", "at", "away", "b", "back", "backed", "backing", "backs",

  "be", "became", "because", "become", "becomes", "been", "before", "began", "behind", "being",

  "beings", "below", "best", "better", "between", "big", "both", "but", "by", "c", "came", "can",

  "cannot", "can't", "case", "cases", "certain", "certainly", "clear", "clearly", "come", "could",

  "couldn't", "d", "did", "didn't", "differ", "different", "differently", "do", "does", "doesn't",

  "doing", "done", "don't", "down", "downed", "downing", "downs", "during", "e", "each", "early",

  "either", "end", "ended", "ending", "ends", "enough", "even", "evenly", "ever", "every", "everybody",

  "everyone", "everything", "everywhere", "f", "face", "faces", "fact", "facts", "far", "felt", "few",

  "find", "finds", "first", "for", "four", "from", "full", "fully", "further", "furthered", "furthering",

  "furthers", "g", "gave", "general", "generally", "get", "gets", "give", "given", "gives", "go", "going",

  "good", "goods", "got", "great", "greater", "greatest", "group", "grouped", "grouping", "groups", "h",

  "had", "hadn't", "has", "hasn't", "have", "haven't", "having", "he", "he'd", "he'll", "her", "here",

  "here's", "hers", "herself", "he's", "high", "higher", "highest", "him", "himself", "his", "how",

  "however", "how's", "i", "i'd", "if", "i'll", "i'm", "important", "in", "interest", "interested",

  "interesting", "interests", "into", "is", "isn't", "it", "its", "it's", "itself", "i've", "j", "just",

  "k", "keep", "keeps", "kind", "knew", "know", "known", "knows", "l", "large", "largely", "last", "later",

  "latest", "least", "less", "let", "lets", "let's", "like", "likely", "long", "longer", "longest", "m",

  "made", "make", "making", "man", "many", "may", "me", "member", "members", "men", "might", "more", "most",

  "mostly", "mr", "mrs", "much", "must", "mustn't", "my", "myself", "n", "necessary", "need", "needed",

  "needing", "needs", "never", "new", "newer", "newest", "next", "no", "nobody", "non", "noone", "nor",

  "not", "nothing", "now", "nowhere", "number", "numbers", "o", "of", "off", "often", "old", "older",

  "oldest", "on", "once", "one", "only", "open", "opened", "opening", "opens", "or", "order", "ordered",

  "ordering", "orders", "other", "others", "ought", "our", "ours", "ourselves", "out", "over", "own", "p",

  "part", "parted", "parting", "parts", "per", "perhaps", "place", "places", "point", "pointed", "pointing",

  "points", "possible", "present", "presented", "presenting", "presents", "problem", "problems", "put", "puts",

  "q", "quite", "r", "rather", "really", "right", "room", "rooms", "s", "said", "same", "saw", "say", "says",

  "second", "seconds", "see", "seem", "seemed", "seeming", "seems", "sees", "several", "shall", "shan't", "she",

  "she'd", "she'll", "she's", "should", "shouldn't", "show", "showed", "showing", "shows", "side", "sides",

  "since", "small", "smaller", "smallest", "so", "some", "somebody", "someone", "something", "somewhere",

  "state", "states", "still", "such", "sure", "t", "take", "taken", "than", "that", "that's", "the", "their",

  "theirs", "them", "themselves", "then", "there", "therefore", "there's", "these", "they", "they'd", "they'll",

  "they're", "they've", "thing", "things", "think", "thinks", "this", "those", "though", "thought", "thoughts",

  "three", "through", "thus", "to", "today", "together", "too", "took", "toward", "turn", "turned", "turning",

  "turns", "two", "u", "under", "until", "up", "upon", "us", "use", "used", "uses", "v", "very", "w", "want",

  "wanted", "wanting", "wants", "was", "wasn't", "way", "ways", "we", "we'd", "well", "we'll", "wells", "went",

  "were", "we're",   "weren't", "we've", "what", "what's", "when", "when's", "where", "where's", "whether", "which", "while",

  "who", "whole", "whom", "who's", "whose", "why", "why's", "will", "with", "within", "without", "won't",

  "work", "worked", "working", "works", "would", "wouldn't", "x", "y", "year", "years", "yes"

  , "yet", "you",

  "you'd", "you'll", "young", "younger", "youngest", "your", "you're", "yours", "yourself", "yourselves", "you've", "z", "unto", "thou", "thy", "thee")
words[!words %in% stop] -> words
word_freq <- table(words)
sorted_word_freq <- sort(word_freq, decreasing = TRUE)
top20 <- data.frame(words = names(sorted_word_freq)[1:20], freq = as.numeric(sorted_word_freq)[1:20])

단어빈도 막대 그래프

library(ggplot2)
order <- arrange(top20, desc(freq))$words
ggplot(data = top20, aes(x = factor(words, levels = order), y = freq)) +
  geom_col() +
  coord_flip() +
  geom_text(aes(label = freq, hjust = -0.3)) +
  ylim(0, max(top20$freq) + 100) 

워드 클라우드

options(repos = c(CRAN = "https://cran.r-project.org"))
install.packages("RColorBrewer")
## 'C:/Users/user/AppData/Local/R/win-library/4.3'의 위치에 패키지(들)을 설치합니다.
## (왜냐하면 'lib'가 지정되지 않았기 때문입니다)
## 패키지 'RColorBrewer'를 성공적으로 압축해제하였고 MD5 sums 이 확인되었습니다
## 
## 다운로드된 바이너리 패키지들은 다음의 위치에 있습니다
##  C:\Users\user\AppData\Local\Temp\RtmpAjsIWm\downloaded_packages
library(RColorBrewer)
install.packages("wordcloud")
## 'C:/Users/user/AppData/Local/R/win-library/4.3'의 위치에 패키지(들)을 설치합니다.
## (왜냐하면 'lib'가 지정되지 않았기 때문입니다)
## 패키지 'wordcloud'를 성공적으로 압축해제하였고 MD5 sums 이 확인되었습니다
## 
## 다운로드된 바이너리 패키지들은 다음의 위치에 있습니다
##  C:\Users\user\AppData\Local\Temp\RtmpAjsIWm\downloaded_packages
library(wordcloud)
## Warning: 패키지 'wordcloud'는 R 버전 4.3.2에서 작성되었습니다
pal <- brewer.pal(8, "Dark2")
set.seed(1234)
wordcloud(words = top20$word,
  freq = top20$freq,
  min.freq = 10,
  max.words = 200,
  random.order = FALSE,
  rot.per = 1,
  scale = c(6, 0.2),
  colors = pal)