我在使用文本文件和文件的關聯元數據進行一些工作時遇到了一些麻煩。我可以讀取文件,預處理它們,然後將它們轉換爲我正在使用的lda軟件包的可讀格式(using this guide by Sievert)。實施例下面:R:將列表與csv元數據結合使用
#Reading the files
corpus <- file.path("Folder/Fiction/texts")
corpus <- list.files(corpus)
corpus <- lapply(corpus, readLines)
***pre-processing functions removed for space***
corp.list <- strsplit(corpus, "[[:space:]]+")
# compute the table of terms:
corpterm.table <- table(unlist(corp.list))
corpterm.table <- sort(corpterm.table, decreasing = TRUE)
***removing stopwords, again removed for space***
# now put the corpus into the format required by the lda package:
getCorp.terms <- function(x) {
index <- match(x, vocabCorp)
index <- index[!is.na(index)]
rbind(as.integer(index - 1), as.integer(rep(1, length(index))))
}
corpus <- lapply(corp.list, getCorp.terms)
在這一點上,corpus
變量是每個文檔分開的載體文檔令牌的列表,但是從它的文件路徑已經被分離,並且該文件的名稱。這裏是我的問題開始的地方:我有一個csv,包含文本的元數據(他們的文件名,標題,作者,年份,流派等),我希望它們與每個標記向量相關聯,以便輕鬆模型我的信息隨着時間的推移,按性別等
我不確定如何做到這一點,但我猜這將需要完成的文件正在閱讀,並沒有合併後我操縱文檔文本。我可以想象這將是東西,看起來像:
corpus.f <- file.path (stuff)
corpus <- list.files(corpus)
corpus <- lapply(corpus, ReadLines)
corpus.df <- as.data.frame(c(corpus.f,corpus))
corpus.info <- read.csv(stuff.csv)
從那裏通過合併或匹配功能結合到每個文檔(或文檔令牌的載體)與元數據的正確的行關聯起來。
什麼是你的數據在本此步驟後的結構:語料庫< - lapply(語料庫,readlines方法) – Chris
@克里斯的結構是一個大名單,看起來像這樣: 'List of 1128 $:chr [1:61616]「word」「word」「word」「word」... $:chr [1:108093]「,」「,」「,」「, 「... $:chr [1:29334]」,「」,「」,「」,「...' 依此類推,每個文檔有一個向量。 – Niveus