簡體   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