gpt4 book ai didi

performance - R: tm Textmining 包:Doc-Level 元数据生成很慢

转载 作者:行者123 更新时间:2023-12-04 05:10:45 25 4
gpt4 key购买 nike

我有一个要处理的文档列表,对于每个记录,我想将一些元数据附加到 R 包 tm 生成的“语料库”数据结构内的文档“成员”(通过读取文本文件)。

这个 for 循环有效,但速度很慢 ,
性能似乎随着函数 f ~ 1/n_docs 而下降。

for (i in seq(from= 1, to=length(corpus), by=1)){
if(opts$options$verbose == TRUE || i %% 50 == 0){
print(paste(i, " ", substr(corpus[[i]], 1, 140), sep = " "))
}
DublinCore(corpus[[i]], "title") = csv[[i,10]]
DublinCore(corpus[[i]], "Publisher" ) = csv[[i,16]] #institutions
}

这可能会对语料库变量产生影响,但我不知道是什么。
但是当我把它放在 tm_map() (类似于 lapply() 函数)中时,它运行得更快,但更改不会持久化:
i = 0
corpus = tm_map(corpus, function(x){
i <<- i + 1


if(opts$options$verbose == TRUE){
print(paste(i, " ", substr(x, 1, 140), sep = " "))
}

meta(x, tag = "Heading") = csv[[i,10]]
meta(x, tag = "publisher" ) = csv[[i,16]]
})

退出 tm_map 函数后,变量语料库的元数据字段为空。它应该被填满。我还有其他一些事情与收藏有关。

meta() 函数的 R 文档是这样说的:
     Examples:
data("crude")
meta(crude[[1]])
DublinCore(crude[[1]])
meta(crude[[1]], tag = "Topics")
meta(crude[[1]], tag = "Comment") <- "A short comment."
meta(crude[[1]], tag = "Topics") <- NULL
DublinCore(crude[[1]], tag = "creator") <- "Ano Nymous"
DublinCore(crude[[1]], tag = "Format") <- "XML"
DublinCore(crude[[1]])
meta(crude[[1]])
meta(crude)
meta(crude, type = "corpus")
meta(crude, "labels") <- 21:40
meta(crude)

我尝试了很多这样的调用(使用 var "corpus"而不是 "c​​rude"),但它们似乎不起作用。
其他人似乎曾经对类似的数据集遇到过同样的问题( forum post from 2009 ,无响应)

最佳答案

这是一些基准测试......

for循环:

expr.for <- function() {
for (i in seq(from= 1, to=length(corpus), by=1)){
DublinCore(corpus[[i]], "title") = LETTERS[round(runif(26))]
DublinCore(corpus[[i]], "Publisher" ) = LETTERS[round(runif(26))]
}
}

microbenchmark(expr.for())
# Unit: milliseconds
# expr min lq median uq max
# 1 expr.for() 21.50504 22.40111 23.56246 23.90446 70.12398

tm_map :
corpus <- crude

expr.map <- function() {
tm_map(corpus, function(x) {
meta(x, "title") = LETTERS[round(runif(26))]
meta(x, "Publisher" ) = LETTERS[round(runif(26))]
x
})
}

microbenchmark(expr.map())
# Unit: milliseconds
# expr min lq median uq max
# 1 expr.map() 5.575842 5.700616 5.796284 5.886589 8.753482

所以 tm_map正如您所注意到的,版本似乎快了大约 4 倍。

在您的问题中,您说 tm_map 中的更改版本不持久,那是因为你不回 x在匿名函数的末尾。最后应该是:
meta(x, tag = "Heading") = csv[[i,10]]  
meta(x, tag = "publisher" ) = csv[[i,16]]
x

关于performance - R: tm Textmining 包:Doc-Level 元数据生成很慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14960863/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com