16

我有一个使用tm包的 R 语料库。我正在应用该removeWords功能来删除停用词

tm_map(abs, removeWords, stopwords("english")) 

有没有办法将我自己的自定义停用词添加到此列表中?

4

6 回答 6

38

stopwords只是为您提供一个单词向量,只需c将您自己的单词与此结合即可。

tm_map(abs, removeWords, c(stopwords("english"),"my","custom","words")) 
于 2013-08-26T14:33:27.763 回答
5

将您的自定义保存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
于 2017-05-15T14:05:14.633 回答
2

您可以创建自定义停用词的向量并使用如下语句:

tm_map(abs, removeWords, c(stopwords("english"), myStopWords)) 
于 2016-11-04T16:47:54.030 回答
2

您也可以使用该textProcessor软件包。它工作得很好:

textProcessor(documents, 
  removestopwords = TRUE, customstopwords = NULL)
于 2018-07-12T18:03:50.377 回答
1

可以将您自己的停用词添加到 tm install 随附的默认停用词列表中。“tm”包带有许多数据文件,包括停用词,请注意停用词文件适用于多种语言。您可以添加、删除或更新 stopwords 目录下的 english.dat 文件。
查找停用词目录的最简单方法是通过文件浏览器在系统中搜索“停用词”目录。您应该会找到english.dat 以及许多其他语言文件。从 RStudio 打开english.dat 文件,该文件应该可以编辑文件 - 您可以根据需要添加自己的单词或删除现有的单词。如果您想编辑任何其他语言的停用词,也是同样的过程。

于 2017-01-09T00:41:56.393 回答
0

我正在使用停用词库而不是 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)
于 2021-03-11T12:44:41.610 回答