首页 > 解决方案 > 如何在 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 个以上的文件并将它们组合然后制作一个矩阵并找到文档频率来解决这个问题

标签: rdataframe

解决方案


这是我的问题的解决方案

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)

推荐阅读