text-analysis/analysis.R

17 lines
900 B
R

# Синтетические данные и расчёт метрик для обоих дашбордов.
set.seed(2026)
documents <- c("forest river forest hill", "city tram park city", "river park water river")
tokens <- strsplit(documents, " ", fixed = TRUE)
dictionary <- sort(unique(unlist(tokens)))
counts <- sapply(tokens, function(words) table(factor(words, levels = dictionary)))
tf <- sweep(counts, 2, colSums(counts), "/")
idf <- log(length(documents) / rowSums(counts > 0))
result <- data.frame(term = dictionary, max_tfidf = apply(tf * idf, 1, max))
# Проверяет конечность основной метрики перед экспортом.
stopifnot(nrow(result) > 1, all(is.finite(result$max_tfidf)))
if (sys.nframe() == 0L) {
arguments <- commandArgs(trailingOnly = TRUE)
stopifnot(length(arguments) == 1L)
write.csv(result, arguments[1], row.names = FALSE)
}