在 R text2vec 中绘制文档修剪对文本语料库的影响

Plotting the effect of document pruning on text corpus in R text2vec

text2vec包中应用prune_vocabulary后是否可以检查语料库中剩余多少文档?

这是一个获取数据集并修剪词汇表的示例

library(text2vec)
library(data.table)
library(tm)

#Load movie review dataset
data("movie_review")
setDT(movie_review)
setkey(movie_review, id)
set.seed(2016L)

#Tokenize
prep_fun = tolower
tok_fun = word_tokenizer
it_train = itoken(movie_review$review, 
              preprocessor = prep_fun, 
              tokenizer = tok_fun, 
              ids = movie_review$id, 
              progressbar = FALSE)


#Generate vocabulary
vocab = create_vocabulary(it_train
                      , stopwords = tm::stopwords())

#Prune vocabulary
#How do I ascertain how many documents got kicked out of my training set because of the pruning criteria?
pruned_vocab = prune_vocabulary(vocab, 
                            term_count_min = 10, 
                            doc_proportion_max = 0.5,
                            doc_proportion_min = 0.001)

# create document term matrix with new pruned vocabulary vectorizer
vectorizer = vocab_vectorizer(pruned_vocab)
dtm_train  = create_dtm(it_train, vectorizer)

有没有一种简单的方法可以了解 term_count_mindoc_proportion_min 参数在我的文本语料库中的攻击性。我正在尝试做一些类似于 stm 包如何让我们使用 plotRemoved 函数来处理这个问题的方法,该函数生成如下图:

vocab $vocab 是一个 data.table,其中包含大量关于您的语料库的统计信息。 prune_vocabularyterm_count_mindoc_proportion_min 参数只是过滤此 data.table。例如,这里是如何计算已删除令牌的数量:

total_tokens = sum(v$vocab$terms_counts)
total_tokens
# 1230342
# now lets prune
v2 = prune_vocabulary(v, term_count_min = 10)
total_tokens - sum(v2$vocab$terms_counts)
# 78037
# effectively this will remove 78037 tokens

另一方面,您可以使用不同的词汇表创建文档术语矩阵,并使用 Matrix 包中的函数检查不同的统计信息:colMeans(), colSums(), rowMeans(), rowSums(),等等。我相信您可以获得任何上面的指标。

例如这里是如何查找空文档:

doc_word_count = Matrix::rowSums(dtm)
indices_empty_docs = which(doc_word_count == 0)