如何在 text2vec 中包含停用词(术语)

How do I include stopwords(terms) in text2vec

text2vec包中,我使用了create_vocabulary函数。例如: 我的文本是 "This book is very good" 并且假设我没有使用停用词和 1L 到 3L 的 ngram。所以词汇术语将是

这本书非常非常好,这本书.....这本书非常非常好。我只想删除术语 "book is very" (以及使用向量的其他术语的主机)。因为我只想删除一个短语,所以我不能使用停用词。我编写了以下代码:

vocab<-create_vocabulary(it,ngram=c(1L,3L))
vocab_mod<- subset(vocab,!(term %in% stp) # where stp is stop phrases.

x<- read.csv(Filename') #these are all stop phrases
stp<-as.vector(x$term)

当我执行上述步骤时,属性中的元信息在 vocab_mod 中丢失,因此无法在 create_dtm 中使用。

subset 函数似乎删除了一些属性。你可以试试:

library(text2vec)
txt = "This book is very good"
it = itoken(txt)
v = create_vocabulary(it, ngram = c(1, 3))
v = v[!(v$term %in% "is_very_good"), ]    
v
# Number of docs: 1 
# 0 stopwords:  ... 
# ngram_min = 1; ngram_max = 3 
# Vocabulary: 
#   term term_count doc_count
# 1:         good          1         1
# 2: book_is_very          1         1
# 3:    This_book          1         1
# 4:         This          1         1
# 5:         book          1         1
# 6:    very_good          1         1
# 7:      is_very          1         1
# 8:      book_is          1         1
# 9: This_book_is          1         1
# 10:           is          1         1
# 11:         very          1         1
dtm = create_dtm(it, vocab_vectorizer(v))

@Dmitriy 即使这样也可以删除属性...所以我找到的出路现在只是使用 attr 函数手动添加属性

attr(vocab_mod,"ngram")<-c(ngram_min = 1L,ngram_max=3L) 和其他属性的儿子之一。我们可以从 vocab 中获取属性详细信息。