我在R中使用tm
软件包拥有一个语料库。 我正在使用removeWords
函数来去除停用词。
我在R中使用tm
包创建了一个语料库,现在我想使用removeWords
函数去除停用词。
tm_map(abs, removeWords, stopwords("english"))
有没有办法将我自己的停用词添加到这个列表中?
我在R中使用tm
软件包拥有一个语料库。 我正在使用removeWords
函数来去除停用词。
我在R中使用tm
包创建了一个语料库,现在我想使用removeWords
函数去除停用词。
tm_map(abs, removeWords, stopwords("english"))
有没有办法将我自己的停用词添加到这个列表中?
stopwords
只是提供一个单词向量,只需要将您自己的单词与此向量 c
组合即可。
tm_map(abs, removeWords, c(stopwords("english"),"my","custom","words"))
将自定义的 停用词
存储在一个 csv 文件中 (例如: word.csv
)。
library(tm)
stopwords <- read.csv("word.csv", header = FALSE)
stopwords <- as.character(stopwords$V1)
stopwords <- c(stopwords, stopwords())
然后,您可以将自定义单词
应用于您的文本文件。
text <- VectorSource(text)
text <- VCorpus(text)
text <- tm_map(text, content_transformer(tolower))
text <- tm_map(text, removeWords, stopwords)
text <- tm_map(text, stripWhitespace)
text[[1]]$content
textProcessor
包。它的效果相当不错:textProcessor(documents,
removestopwords = TRUE, customstopwords = NULL)
“您可以创建一个包含您自定义停用词的向量,并使用如下语句:”
tm_map(abs, removeWords, c(stopwords("english"), myStopWords))
我正在使用停用词库而不是tm库。只是决定将我的解决方案放在这里,以防有人需要它。
# Create a list of custom stopwords that should be added
word <- c("quick", "recovery")
lexicon <- rep("custom", times=length(word))
# Create a dataframe from the two vectors above
mystopwords <- data.frame(word, lexicon)
names(mystopwords) <- c("word", "lexicon")
# Add the dataframe to stop_words df that exists in the library stopwords
stop_words <- dplyr::bind_rows(stop_words, mystopwords)
View(stop_words)