我目前正在编写一个文本挖掘文档,在这里我想从我的文本中抽象相关的关键字(注意,我有很多很多文本文档)。
我用的是ud导管包。一个伟大的小插曲在线(http://bnosac.be/index.php/blog/77-an-overview-of-keyword-extraction-techniques)。一切正常,但当我运行代码时,
x <- udpipe_annotate(ud_model, x = comments$feedback)非常非常慢(特别是当你有大量的文本时)。,有没有人知道我如何更快地得到这个部分?当然,解决办法是可以的。
library(udpipe)
library(textrank)
## First step: Take the Spanish udpipe model and annotate the text. Note: this takes about 3 minutes
data(brussels_reviews)
comments <- subset(brussels_reviews, language %in% "es")
ud_model <- udpipe_download_model(language = "spanish")
ud_model <- udpipe_load_model(ud_model$file_model)
x <- udpipe_annotate(ud_model, x = comments$feedback) # This part is really, really slow
x <- as.data.frame(x)事先非常感谢!
发布于 2018-11-28 17:56:26
我将根据未来的API添加一个答案。这与您使用的操作系统(Windows、mac或linux风味)无关。
future.apply包为基*apply系列提供了所有并行的选项。其余代码基于@jwijffels的答案。唯一的区别是,我在data.table函数中使用了annotate_splits。
library(udpipe)
library(data.table)
data(brussels_reviews)
comments <- subset(brussels_reviews, language %in% "es")
ud_model <- udpipe_download_model(language = "spanish", overwrite = F)
ud_es <- udpipe_load_model(ud_model)
# returns a data.table
annotate_splits <- function(x, file) {
ud_model <- udpipe_load_model(file)
x <- as.data.table(udpipe_annotate(ud_model,
x = x$feedback,
doc_id = x$id))
return(x)
}
# load parallel library future.apply
library(future.apply)
# Define cores to be used
ncores <- 3L
plan(multiprocess, workers = ncores)
# split comments based on available cores
corpus_splitted <- split(comments, seq(1, nrow(comments), by = 100))
annotation <- future_lapply(corpus_splitted, annotate_splits, file = ud_model$file_model)
annotation <- rbindlist(annotation)发布于 2018-11-28 08:27:26
使用udpipe版本1.2 C++库。注释速度在本文中有详细介绍(请参阅https://doi.org/10.18653/v1/K17-3009中的表8)。如果您想加快速度,请并行运行它,因为注释在并行性上是微不足道的。
下面的示例使用并行::mclapply对16个核心进行并行处理,如果您有16个核心,则为大型企业提供16x的加速。您可以使用您所拥有的任何并行化框架,下面我使用了并行包--如果您在Windows上,您将需要例如并行::parallelisation,但是没有什么可以阻止您使用其他并行选项(斯诺/多核/前途/ foreach /.)以并行方式注释。
library(udpipe)
library(data.table)
library(parallel)
data(brussels_reviews)
comments <- subset(brussels_reviews, language %in% "fr")
ud_model <- udpipe_download_model(language = "french-partut")
annotate_splits <- function(x, file) {
model <- udpipe_load_model(file)
x <- udpipe_annotate(model, x = x$feedback, doc_id = x$id, tagger = "default", parser = "default")
as.data.frame(x, detailed = TRUE)
}
corpus_splitted <- split(comments, seq(1, nrow(comments), by = 100))
annotation <- mclapply(corpus_splitted, FUN = function(x, file){
annotate_splits(x, file)
}, file = ud_model$file_model, mc.cores = 16)
annotation <- rbindlist(annotation)请注意,udpipe_load_model也需要一些时间,所以更好的策略可能是将其并行化到您的机器上的核数,而不是像我前面所展示的那样,以100块的块进行并行处理。
发布于 2021-11-11 15:11:43
您还可以使用furrr和future库来完成这一任务,这两个库有一个进度条的额外好处。
在另外两个答案中,我感到困惑的一件事是它们在其功能中实现了udpipe_load_model。您可以首先在函数外部加载模型一次,这样函数就不必每次运行时都加载模型。
library(udpipe)
library(future)
library(furrr)
data(brussels_reviews)
comments <- subset(brussels_reviews, language %in% "es")
downloaded_model <- udpipe_download_model(language = "spanish", overwrite = FALSE)
model <- udpipe_load_model(downloaded_model)
annotate_splits <- function(text) {
anno <- udpipe_annotate(model, x = text$feedback, doc_id = text$id, tagger = "default", parser = "default")
x <- as.data.frame(anno, detailed = TRUE)
return(x)
}
split_corpus <- split(comments, seq(1, nrow(comments), by = 100))
#recommend setting workers equal to number of your computer's cores
plan(multisession, workers = 2)
dfs <- future_map(split_corpus, annotate_splits, .progress = TRUE)
annotated_df <- dplyr::bind_rows(dfs)https://stackoverflow.com/questions/53501341
复制相似问题