使 udpipe_annotate() 更快

R o*_*low 4 r keyword tm udpipe

我目前正在编写一个文本挖掘文档,我想从文本中提取相关关键字(请注意,我有很多很多文本文档)。

我正在使用 udpipe 包。一个很棒的 Vignette 在线 ( http://bnosac.be/index.php/blog/77-an-overview-of-keyword-extraction-techniques )。一切正常,但是当我运行代码时,该部分

x <- udpipe_annotate(ud_model, x = comments$feedback)
Run Code Online (Sandbox Code Playgroud)

真的非常慢(特别是当你有很多文本时)。有人知道如何更快地获得这部分吗?解决方法当然没问题。

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)
Run Code Online (Sandbox Code Playgroud)

提前谢谢了!

phi*_*ver 6

我正在根据未来的 API 添加答案。这与您使用的操作系统(Windows、Mac 或 Linux 版本)无关。

future.apply 包具有基本 *apply 系列的所有并行替代方案。其余代码基于@jwijffels 的答案。唯一的区别是我在 annotate_splits 函数中使用 data.table 。

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)
Run Code Online (Sandbox Code Playgroud)