# 텍스트 마이닝 — 공백 토큰화에서 단어 빈도 그림까지
# 적용 범위: 공식 strsplit()·table()의 사용법을 세 개의 학습용 한국어 문장에 적용했습니다. 문장은 직접 만든 예시이며 공식 코퍼스나 원문 예제의 재현은 아닙니다.
# 출처: https://stat.ethz.ch/R-manual/R-patched/library/base/html/strsplit.html
# 출처: https://stat.ethz.ch/R-manual/R-patched/library/base/html/table.html
# 실행 확인 환경: R version 4.6.2 (2026-10-29)
# 아래 코드를 실행하면 출력과 그림을 확인할 수 있습니다.

# 재현 설정: 같은 난수 방식과 출력 자릿수를 사용합니다.
RNGkind("Mersenne-Twister", "Inversion", "Rejection")
set.seed(2026)
options(width = 90, digits = 7, scipen = 7)

docs <- c("데이터 분석 데이터", "데이터 시각화", "분석 결과 시각화")
tokens <- strsplit(docs, split = " ", fixed = TRUE)
terms <- sort(unique(unlist(tokens)))
dtm <- t(vapply(tokens, function(x) as.integer(table(factor(x, levels = terms))),
                integer(length(terms))))
dimnames(dtm) <- list(paste0("문서", 1:3), terms)
dtm
frequency <- colSums(dtm)
frequency
barplot(frequency, col = "#2563EB", main = "세 문서의 합산 단어 빈도",
        xlab = "공백으로 나눈 토큰", ylab = "출현 횟수")
