我需要有关此问题的帮助才能在R中创建一个程序,该程序可以找到: -索引器处理的最小文档数为4。 -从每个文档中提取所有术语以构建包含每个文档中要打印的术语频率的矩阵。 -打印每个术语及其DF(文档频率)
我试图通过for循环和函数输入文件,但没有用,我也不知道如何解决
analyze <- function(filename) {
# Plots the average, min, and max inflammation over time.
# Input is character string of a csv file.
dat <- read.csv(file = filename, header = FALSE)
avg_day_inflammation <- apply(dat, 2, mean)
plot(avg_day_inflammation)
max_day_inflammation <- apply(dat, 2, max)
plot(max_day_inflammation)
min_day_inflammation <- apply(dat, 2, min)
plot(min_day_inflammation)
}
analyze("E://FCI-H//level 3 - Second Semester//Information Retrival//Section//assignment//assignment3//1.csv")
此代码向我显示了一个错误,但我希望通过打开5个以上的文件并合并它们,然后形成一个矩阵并找到文档频率来解决此问题
答案 0 :(得分:0)
这是我的问题的解决方法
library(tm)
mypath = "E://FCI-H//level 3 - Second Semester//Information Retrival//Section//assignment//assignment3" setwd(mypath)
txt_files_ls = list.files(path=mypath, pattern="*.txt") txt_files_df <- lapply(txt_files_ls, function(x) {read.table(file = x, header = F, sep ="/")})
combined_df <- do.call("rbind", lapply(txt_files_df, as.data.frame))
names(combined_df) <- "text" myCorpus <- Corpus(VectorSource(combined_df$text))
tdm <- TermDocumentMatrix(myCorpus,control = list(removePunctuation = TRUE, stopwords = TRUE)) dtm <- DocumentTermMatrix(myCorpus, control = list(weighting = function(x) weightTfIdf(x, normalize = FALSE), stopwords = TRUE))
inspect(tdm) inspect(dtm)