r - 如何在 R 中查找文档频率?
问题描述
我需要帮助解决这个问题,以便在 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 个以上的文件并将它们组合然后制作一个矩阵并找到文档频率来解决这个问题
解决方案
这是我的问题的解决方案
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)
推荐阅读
- java - 如何添加两个包含十六进制数字的字符串而不将它们转换为 int?
- c# - C#计数并列出另一个方法的字典对
- swift - 如何在 UITextView 中动态加载图像
- angular - Ionic4 Firebase 加载数据时间过长
- java - 无法使用 API 客户端库 (Java) 启动数据流模板,因为我收到无效参数异常
- angular - 如果项目不是使用 Angular-CLI 创建的,则添加 Polyfill 支持
- python - 使用矩阵切片使循环更有效
- asp.net - IIS 上的某些请求会挂起几分钟并以丢失的连接结束
- javascript - 如何在 vue.js 中创建多维 v-model 作为名称属性
- python - 清除值 JSON