R 文本挖掘生成使用数据帧子集构建的语料库



我是R公式的新手,我正在努力将一些重复的代码更改为更紧凑的代码。正如MrFlick的评论中所建议的那样,我在答案部分发布了我已经找到的工作解决方案。

所以我的问题是使用几种不同的分类为比较词云创建各种语料库,正如您在 wikispiral.org 中看到的那样。为此,我需要基于原始数据帧的子集(给定数据帧中存在的类别)创建一个字符向量列表。请参阅以下示例:

library(wordcloud)
library(tm)
element <- c("Adams Pearmain ", "Aia Ilu ", "Airlie Red Flesh", "Akane ", "Åkerö ", "Alkmene", "Allington Pippin ", "Ambrosia ", "Anna ", "Annurca ", "Antonovka ", "Apollo ", "Ariane ", "Arkansas Black ", "Arthur Turner")
qty <- c(2, 1, 4, 3, 6, 2, 1, 4, 3, 6, 2, 1, 4, 3, 6)
category1 <- c("Red", "Green", "Red", "Green", "Yellow", "Orange", "Red", "Red", "Green", "Red", "Green", "Yellow",  "Green", "Yellow", "Orange")
category2 <- c("small", "big", "big", "small", "small", "medium", "medium", "medium", big", "big", "small", "medium", "big", "very big", "medium")
d <- data.frame(element=element, qty=qty, category1=category1, category2=category2)

这给出了这个数据帧:

    element             qty category1   category2
1   Adams Pearmain      2   Red         small
2   Aia Ilu             1   Green       big
3   Airlie Red Flesh    4   Red         small
4   Akane               3   Green       big
5   Åkerö               6   Yellow      small
6   Alkmene             2   Orange      big
7   Allington Pippin    1   Red         small
8   Ambrosia            4   Red         big
9   Anna                3   Green       small
10  Annurca             6   Red         big
11  Antonovka           2   Green       small
12  Apollo              1   Yellow      big
13  Ariane              4   Green       small
14  Arkansas Black      3   Yellow      big
15  Arthur Turner       6   Orange      big

我目前正在为词云创建我的向量列表,如下所示:

## Subsetting two dataframes to category2 values
wordBig <- d[d$category2 == "big",]
wordSmall <- d[d$category2 == "small",]
## Extracting the vectors in the category1 columns
wordSmall <- as.vector(wordSmall$category1)
wordBig <- as.vector(wordBig$category1)
## Building the list for the corpus
wordALL <- list(wordBig, wordSmall) # Without list() it doesn' t work

最后:

语料库 <- 语料库(VectorSource(wordALL), readerControl = list(language = "fr"))

在我 wikispiral.org 的现实生活中的例子中,有一个动态的维度数组 - 不仅是"大"或"小"类别(其中一些类别是由网站用户定义的,并且非常不可预测)。即使对于固定类别,代码也变得越来越重复和丑陋,并且必须测试每个维度的存在,以避免comparative.wordcloud()类别中有NA时产生的错误(例如"大"类别中没有数据)。

所以我的问题是:如何在更紧凑的代码中转换先例示例,该代码能够:1 - 检测分类列中的类别2 - 构建字符向量列表3 - 也许这样做可以避免循环...

我已经找到了一个答案,我已经按照Flick先生的建议将其放在答案部分。

我创建了一个函数来使用 for 循环构建此列表,该循环从主数据帧中子集字符向量并填充列表。我更新了我的函数,使其更加通用,以便其他人可以重用它来帮助构建他们的应用程序。

CorpusFromDF <- function(DF, textcol, catcol) {
  # DF = a dataframe 
  # wordcol = the name of a column from DF containing the text to build the corpus
  # catcol = the name of a column from DF containing the categorisation
  cat <- levels(DF[,catcol])
  list <- list()
  for (n in cat){
    temp <- DF[DF[,catcol] == n,]
    temp <- as.vector(temp[,textcol])
    list <- append(list, list(temp))
    }
  return(list)
}

并使用它:

wordALL <- CorpusFromDF(d, "element", "category2")

所以我真的很高兴,因为它有效(毕竟这是我的第一个 R 函数),我有我需要的东西,一个包含单词的向量列表。从那里我可以重用它来重塑我的语料库,特别是为了使用几个不同的类别/分类构建网络分析。

但它是完美的,我猜在不使用循环的情况下可能会有这种可能性,尤其是当你真的有很多类别来构建你的语料库时......对于其他应用程序,我的情况如何...

好的,按照 42 在他的评论中的建议,它确实更快、更干净......

listtest <- split(d,d$category2)
listtest <- lapply(listtest, droplevels.data.frame)
wordALL <- lapply(listtest, "[[", "element")

这就是来自数据帧的一系列因素,准备构建语料库:

corpus <- Corpus(VectorSource(wordALL), readerControl = list(language = "fr"))

最新更新