在R中使用udpipe提取关键字时的for循环

For loop when extracting keywords with udpipe in R

让我们从一个可重现的例子开始,它是一个名为 key 的数据框,由 8 列和 3 行组成:

key <- structure(c("Make Professional Maps with QGIS and Inkscape", 
"Gain the skills to produce original, professional, and aesthetically pleasing maps using free software", 
"English", "Inkscape 101 for Beginners - Design Vector Graphics", 
"Learn how to create and design vector graphics for free!", "English", 
"Design & Create Vector Graphics With Inkscape 2016", "The Beginners Guide to designing and creating Vector Graphics with Inkscape. No Experience needed!", 
"English", "Design a Logo for Free in Inkscape", "Learn from an award winning, published logo design professional!", 
"English", "Inkscape - Beginner to Pro", "If you want to have a decent learning curve, you are new to the program or even in design, this course is for you.", 
"English", "Creating 2D Textures in Inkscape", "A guide to creating colorful and interesting textures in inkscape.", 
"English", "Vector Art in Inkscape - Icon Design | Make Vector Graphics", 
"Learn Icon Design by creating Vector Graphics using the .SVG and PNG format with the Free Software Inkscape!", 
"English", "Inkscape and Bootstrap 3 -> Responsive Web Design!", 
"Design responsive websites using Free tools Inkscape and Bootstrap 3! Mood Boards and Style Tiles to Mobile First!", 
"English"), .Dim = c(3L, 8L), .Dimnames = list(c("Title", "Short_Description", 
"Language"), c("1", "2", "4", "5", "6", "9", "13", "15")))

我想独立提取每一列的关键词。为此,我使用了 R.

中的 udpipe

因为我想 运行 每一列中的函数,所以我 运行 一个 for 循环。

开始之前,我们以英文为参考创建模型(see this link for more info):

library(udpipe)
ud_model <- udpipe_download_model(language = "english")
ud_model <- udpipe_load_model(ud_model$file_model)

理想情况下,我的最终输出将是一个包含 8 列的数据框,并且提取的行与关键字一样多。

我尝试了两种方法:

方法一:使用dplyr

library(dplyr)
keywords <- list()
for(i in ncol(keywords_en_t)){
  keywords[[i]] <- keywords_en_t %>%
    udpipe_annotate(ud_model,s)
    as.data.frame()
}

方法二:

key <- list()
stats <- list()
for(i in ncol(keywords_en_t)){
    key[[i]] <- as.data.frame(udpipe_annotate(ud_model, x = keywords_en_t[,i]))
    stats[[i]] <- subset(key[[i]], upos %in% "NOUN")
    stats <- txt_freq(x = stats$lemma)
}

输出

在这两种情况下,或者我得到一些错误或者输出不是预期的。

如前所述,我期望的输出是一个包含 8 列的数据框,以行表示关键字

有什么想法吗?

很遗憾,您的代码包含很多错误。您的循环不会从 1 到列数,而是从 8 开始。使用 1:ncolseq_along。 您的关键数据是矩阵,而不是 data.frame。您需要提供 udpipe_annotate 一个字符向量。如果您只提供一个键 [ 8],那么您还向 udpipe_annotate 提供了 dimnames。这可能会生成您不需要的关键字。在方法 1 中,您使用 udpipe_annotate(ud_model,s) 但没有定义 s。在方法 2 中,您使用 stats[[i]],然后立即使用 stats.

覆盖它

为了纠正一些问题,我首先将数据转换为 data.frame。接下来我 运行 循环创建包含关键字的向量列表。在此之后,我创建了一个 data.frame 关键字。这部分代码考虑了向量的不同长度。

您可能想检查如何获取数据,因为拥有 3 列(“标题”、“Short_Description”、“语言”)和大量行更符合逻辑/整洁。

代码

# Transform key into a data.frame. Now it is a matrix. 
key <- as.data.frame(key, stringsAsFactors = FALSE)

library(udpipe)
# prevent downloading ud model if it already exists in the working directory
ud_model <- udpipe_download_model(language = "english", overwrite = FALSE)
ud_model <- udpipe_load_model(ud_model$file_model)

# prepare list with correct length
keywords <- vector(mode = "list", length = ncol(key))

for(i in 1:ncol(key)){
  temp <- as.data.frame(udpipe_annotate(ud_model, x = key[, i]))
  keywords[[i]] <- temp$lemma[temp$upos == "NOUN"]
}

#transform list of vectors to data.frame. 
# Use sapply because vectors are of different lengths.
keywords <- as.data.frame(sapply(keywords, '[', seq(max(lengths(keywords)))), stringsAsFactors = FALSE)

keywords

        V1        V2         V3     V4       V5       V6     V7      V8
1    skill beginners  beginners   logo learning       2d Design     web
2      map    design      guide  award    curve  Texture format  design
3 software    Vector experience   logo  program    guide   <NA>  design
4     <NA>  graphics       <NA> design   design  texture   <NA> website
5     <NA>    vector       <NA>   <NA>   course inkscape   <NA>    tool
6     <NA>   graphic       <NA>   <NA>     <NA>     <NA>   <NA>    <NA>