【问题标题】:How to tokenize documents in R and list tokens by original document title?如何在 R 中标记文档并按原始文档标题列出标记?
【发布时间】:2017-11-17 17:51:22
【问题描述】:

我有数据框D,其中包含文档标题和文本,如下例所示:

document   content
Doc 1      "This is an example of a document"
Doc 2      "And another one"

我需要使用 quanteda 包中的 tokenize 函数来标记每个文档,然后返回其原始文档标题列出的标记,如本例所示:

document   content
    Doc 1      "This"
    Doc 1      "This is"
    Doc 1      "This is an"
    Doc 1      "This is an example" 

这是我目前从文档列表中获取带有标记的数据框的过程:

require(textreadr)
D<-textreadr::read_dir("myDir")
D<-paste(D$content,collapse=" ")
strlist<-paste0(c(":","\\)",":","'",";","!","+","&","<",">","\\(","\\[","\\]","-","#",","),collapse = "|")
D<-gsub(strlist, "", D)
library(quanteda)
require(quanteda)
t<-tokenize(D, what = c("word","sentence", "character","fastestword", "fasterword"), 
            remove_numbers = FALSE, remove_punct = FALSE,
            remove_symbols = FALSE, remove_separators = TRUE,
            remove_twitter = FALSE, remove_hyphens = FALSE, remove_url = FALSE,
            ngrams = 1:10, concatenator = " ", hash = TRUE,
            verbose = quanteda_options("verbose"))
t<-unlist(t, use.names=FALSE)
t1<-data.frame(t)

但是,我找不到一种简单的方法来在标记化过程之后保留文档名称并相应地列出标记。有人可以帮忙吗?

【问题讨论】:

  • 您可以使用嵌套的 data.frames 使用 dplyrtidyrpurrr 来执行此操作。

标签: r nlp tokenize


【解决方案1】:

R 的列表对象可以像这样采用字符串索引:

my_list = list()

document_title = 'asdf.txt'
my_data = tokenize( etc... )
my_list[[document_title]] = my_data

使用您现有的代码,但将您的最终数据框分配给如下列表:

my_list[[document_title]] = data.frame(t)

【讨论】:

    【解决方案2】:

    用一个函数找到了它的底部。这是任何有兴趣的人的代码:

    myFunction <- function(x){
    
    b <- x[2]
    b<-paste(b,collapse=" ")
    
    require(quanteda)
    value <- tokenize(b, what = c("word","sentence", "character","fastestword", "fasterword"), 
                                remove_numbers = FALSE, remove_punct = FALSE,
                                remove_symbols = FALSE, remove_separators = TRUE,
                                remove_twitter = FALSE, remove_hyphens = FALSE, remove_url = FALSE,
                                ngrams = 1:10, concatenator = " ", hash = TRUE,
                                verbose = quanteda_options("verbose"))
    
    value<-unlist(value, use.names=FALSE)
    
    return(value)
            }
    
    D$out <- apply(D, 1, myFunction)
    
    library(tidyr)
    D<-unnest(D)
    

    【讨论】:

      猜你喜欢
      • 2013-06-10
      • 1970-01-01
      • 2018-01-23
      • 2016-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-14
      • 1970-01-01
      相关资源
      最近更新 更多