[英]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_min
和doc_proportion_min
参数对我的文本语料库有多积极。 我正在尝试执行类似于stm
包的方式,使我们可以使用plotRemoved
函数来处理此问题,该函数会生成如下图:
vocab $vocab
是一个data.table
,其中包含有关您的主体的大量统计信息。 prune_vocabulary
与term_count_min
, doc_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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.