我有一个使用tm
包的 R 语料库。我正在应用该removeWords
功能来删除停用词
tm_map(abs, removeWords, stopwords("english"))
有没有办法将我自己的自定义停用词添加到此列表中?
我有一个使用tm
包的 R 语料库。我正在应用该removeWords
功能来删除停用词
tm_map(abs, removeWords, stopwords("english"))
有没有办法将我自己的自定义停用词添加到此列表中?
stopwords
只是为您提供一个单词向量,只需c
将您自己的单词与此结合即可。
tm_map(abs, removeWords, c(stopwords("english"),"my","custom","words"))
将您的自定义保存stop words
在 csv 文件中(例如:)word.csv
。
library(tm)
stopwords <- read.csv("word.csv", header = FALSE)
stopwords <- as.character(stopwords$V1)
stopwords <- c(stopwords, stopwords())
然后你可以申请custom words
到你的文本文件。
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
您可以创建自定义停用词的向量并使用如下语句:
tm_map(abs, removeWords, c(stopwords("english"), myStopWords))
您也可以使用该textProcessor
软件包。它工作得很好:
textProcessor(documents,
removestopwords = TRUE, customstopwords = NULL)
可以将您自己的停用词添加到 tm install 随附的默认停用词列表中。“tm”包带有许多数据文件,包括停用词,请注意停用词文件适用于多种语言。您可以添加、删除或更新 stopwords 目录下的 english.dat 文件。
查找停用词目录的最简单方法是通过文件浏览器在系统中搜索“停用词”目录。您应该会找到english.dat 以及许多其他语言文件。从 RStudio 打开english.dat 文件,该文件应该可以编辑文件 - 您可以根据需要添加自己的单词或删除现有的单词。如果您想编辑任何其他语言的停用词,也是同样的过程。
我正在使用停用词库而不是 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)