代码之家  ›  专栏  ›  技术社区  ›  Union find

如何将文本拆分为向量,其中每个条目对应分配给每个唯一单词的索引值?[副本]

  •  0
  • Union find  · 技术社区  · 7 年前

    假设我有一个文档,其中包含一些文本,如以下内容:

    doc <- 'Questions with similar titles have frequently been downvoted and/or closed. Consider using a title that more accurately describes your question.'
    

    然后我可以创建一个数据帧,其中每个单词在df中都有一行:

    library(stringi)
    dfall <- data.frame(words = unlist(stri_extract_all_words(stri_trans_tolower(doc))))
    

    我们将添加具有唯一ID的第三列。若要获取ID,请删除重复项:

    library(dplyr)
    uniquedf <- distinct(data.frame(words = unlist(stri_extract_all_words(stri_trans_tolower(doc)))))
    

    我正在努力研究如何将行与两个数据帧进行匹配,以从中提取行索引值。 uniquedf 作为的新行值 df

    alldf <- alldf %>% mutate(id = which(uniquedf$words == words))
    

    像这样的dply方法不起作用。

    有更有效的方法吗?

    为了给出一个更简单的示例来显示预期的输出,我想要一个如下所示的数据帧:

      words id
    1     to  1
    2     row  2
    3     zip  3
    4     zip  3
    

    我的起始词vector是: doc <- c('to', 'row', 'zip', 'zip') 或 doc <- c('to row zip zip') . ID列为每个唯一的单词添加一个唯一的ID。

    1 回复  |  直到 7 年前
        1
  •  2
  •   Union find    7 年前

    sapply

    doc <- 'Questions with with titles have frequently been downvoted and/or closed. Consider using a title that more accurately describes your question.'
    

    alldf=cbind(dfall,sapply(1:nrow(dfall),function(x) which(uniquedf$words==dfall$words[x])))
    
    colnames(alldf)=c("words","id")
    > alldf
            words id
    1   questions  1
    2        with  2
    3        with  2
    4      titles  3
    5        have  4
    6  frequently  5
    7        been  6
    8   downvoted  7
    9         and  8
    10         or  9
    11     closed 10
    12   consider 11
    13      using 12
    14          a 13
    15      title 14
    16       that 15
    17       more 16
    18 accurately 17
    19  describes 18
    20       your 19
    21   question 20