从多个文件夹中创建R语料库

时间:2013-12-12 20:09:22

标签: r

我正在尝试从包含许多文件夹B,C,D,E的文件夹A中创建一个语料库.... 每个都有一个文件。

我知道您可以使用包含许多文件的文件夹创建一个语料库:

library(tm)
data = Corpus(DirSource("folder with many files"),
    readerControl = list(language = “en”))

但如何在包含多个文件夹的文件夹中执行此操作,每个文件夹都包含一个文件。

谢谢!

我得到一个像这样的文件夹/文件列表:

 [1] "10000/10000-0" "10005/10005-0" "100/100-0"     "10021/10021-0"
   [5] "10033/10033-0" "10037/10037-0" "1004/1004-0"   "10045/10045-0"
   [9] "10049/10049-0" "10055/10055-0" "10071/10071-0" "10079/10079-0"
  [13] "10095/10095-0" "10099/10099-0" "1010/1010-0"   "10101/10101-0"
  [17] "10103/10103-0" "10105/10105-0" "10123/10123-0" "10125/10125-0"
  [21] "10129/10129-0" "10146/10146-0" "10152/10152-0" "10156/10156-0"
  [25] "10166/10166-0" "10168/10168-0" "10176/10176-0" "10188/10188-0"
  [29] "10192/10192-0" "10206/10206-0" "10208/10208-0" "10216/10216-0"
  [33] "10220/10220-0" "10226/10226-0" "10236/10236-0" "10238/10238-0"
  [37] "10246/10246-0" "10258/10258-0" "10272/10272-0" "10274/10274-0"
  [41] "1028/1028-0"   "10284/10284-0" "10288/10288-0" "10292/10292-0"
  [45] "10294/10294-0" "10306/10306-0" "10308/10308-0" "10310/10310-0"

2 个答案:

答案 0 :(得分:1)

使用recursive=TRUE

  

DirSource(directory =“。”,encoding =“unknown”,pattern = NULL,             recursive = FALSE,ignore.case = FALSE)

     

递归逻辑。列表应该递归到目录吗?

所以你的例子变成了:

Corpus(DirSource("folder with many files"),
    readerControl = list(language = “en”),rec=TRUE)

修改

我基于list.files创建了一个新的directocry源。

recursiveDirSource(directory_path){
    d <- list.files(directory_path,rec=TRUE,full.names=TRUE)

    isfile <- logical(length(d))
    for (i in seq_along(d)) isfile[i] <- !file.info(d[i])["isdir"]
    s <- tm:::.Source(readPlain, "unknown", sum(isfile), TRUE, basename(d), 
                                        0, TRUE, class = "DirSource")
    s$FileList <- d
    s
}

答案 1 :(得分:0)

这对我有用。

我必须添加模式并将recursive设置为TRUE

corpus <- Corpus(
  DirSource("/path/to/maim/folder/", encoding = "UTF-8",pattern="*.html",recursive=TRUE),
  readerControl = list(language = "en")
)