I'm trying to create a Corpus from inside a folder A which contains many folder B,C,D,E.... each with one file inside.
I know you can create a Corpus using a folder that contain many files like this:
library(tm)
data = Corpus(DirSource("folder with many files"),
readerControl = list(language = “en”))
but how to do this in a folder with many folders each containing one file.
Thanks!
I get a folder/file listing like this:
[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"
Use recursive=TRUE
DirSource(directory = ".", encoding = "unknown", pattern = NULL, recursive = FALSE, ignore.case = FALSE)
recursive Logical. Should the listing recurse into directories?
So you example becomes:
Corpus(DirSource("folder with many files"),
readerControl = list(language = “en”),rec=TRUE)
EDIT
I create a new directocry source based on list.files
.
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
}
This worked for me.
I had to add the pattern and set recursive
to TRUE
corpus <- Corpus(
DirSource("/path/to/maim/folder/", encoding = "UTF-8",pattern="*.html",recursive=TRUE),
readerControl = list(language = "en")
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.