R 文本挖掘构建了一个由数据帧的子集构成的语料库

R textmining build a corpus structured with subsets of a dataframe

我是 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

我目前正在为 wordcloud 创建我的矢量列表:

## 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 中我的真实示例中,有一个动态维度数组 - 不仅是类别 "big" 或 "small"(其中一些类别由网站的用户,并且是相当不可预测的)。即使对于固定类别,代码也变得重复和丑陋,并且必须测试每个维度的存在以避免错误 comparative.wordcloud() 在类别中有 NA 时产生(比如 [=35= 中没有数据) ] 类别)。

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

我已经找到了一个答案,我已经按照 MrFlick 的建议放在了答案部分。

我创建了一个函数来使用 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 函数),我有我需要的东西,一个包含单词的向量列表。从那里我可以重用它来重塑我的语料库,特别是为了使用几种不同的 categories/classifications.

构建网络分析 texmining

但它是完美的,我猜测可能有可能不使用循环,特别是当你真的有很多类别来构建你的语料库时......我对其他应用程序的情况会怎样。 ..

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

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

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

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