R로 배우는 Bag-of-Words 텍스트 마이닝
Ted Kwartler
Instructor

# 두 코퍼스 결합: all_tweets all_coffee <- paste(coffee_tweets$text, collapse = "") all_chardonnay <- paste(chardonnay_tweets$text, collapse = "")all_tweets <- c(all_coffee, all_chardonnay)# all_tweets 정제 all_tweets <- VectorSource(all_tweets) all_corpus <- VCorpus(all_tweets) all_clean <- clean_corpus(all_corpus) all_dm <- TermDocumentMatrix(all_clean) all_m <- as.matrix(all_tdm)# 공통성 클라우드 만들기 commonality.cloud(all_m, colors = "steelblue1", max.words = 100)


# 두 코퍼스 결합: all_tweets all_coffee <- paste(coffee_tweets$text, collapse = "") all_chardonnay <- paste(chardonnay_tweets$text, collapse = "") all_tweets <- c(all_coffee, all_chardonnay)# all_tweets 정제 all_tweets <- VectorSource(all_tweets) all_corpus <- VCorpus(all_tweets) all_clean <- clean_corpus(all_corpus) all_tdm <- TermDocumentMatrix(all_clean)colnames(all_tdm) <- c("coffee", "chardonnay")all_m <- as.matrix(all_tdm) # 비교 클라우드 만들기 comparison.cloud(all_m, colors = c("orange", "blue"), max.words = 50)

# 두 문서에 공통으로 등장하는 용어 식별 common_words <- subset( all_tdm_m, all_tdm_m[, 1] > 0 & all_tdm_m[, 2] > 0 )# 가장 흔한 공통 단어 찾기 difference <- abs(common_words[, 1] - common_words[, 2])common_words <- cbind(common_words, difference) common_words <- common_words[order(common_words[, 3], decreasing = TRUE), ] top25_df <- data.frame(x = common_words[1:25, 1], y = common_words[1:25, 2], labels = rownames(common_words[1:25, ]))
# 피라미드 플롯 생성
pyramid.plot(top25_df$x, top25_df$y,
labels = top25_df$labels,
main = "공통 단어",
gap = 8, laxly = NULL,
raxlab = NULL, unit = NULL,
top.labels = c("Chardonnay",
"Words",
"Coffee")
)

# 단어 네트워크 만들기
word_associate(coffee_tweets$text,
match.string = c("barista"),
stopwords = c(Top200Words, "coffee", "amp"),
network.plot = TRUE,
cloud.colors = c("gray85", "darkred"))
# 제목 추가
title(main = "바리스타 커피 트윗 연관어")

R로 배우는 Bag-of-Words 텍스트 마이닝