繁体   English   中英

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

[英]Create corpus in R from multiple folders

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

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

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"

使用recursive=TRUE

DirSource(目录=“。”,编码=“未知”,模式= NULL,递归= 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
}

这对我有用。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM