删除向量中除单词以外的所有单词
Removing all words except for words in a vector
从文本或字符向量中删除停用词是很常见的。我使用 tm
包中的函数 removeWords
。
但是,我正在尝试删除 除了 停用词之外的所有词。我有一个名为 x
的单词列表。当我使用
removeWords(text, x)
我收到这个错误:
In gsub(sprintf("(*UCP)\b(%s)\b", paste(sort(words, decreasing = TRUE), PCRE pattern compilation error 'regular expression is too large'`
我也试过使用 grep
:
grep(x, text)
但这行不通,因为 x
是一个向量而不是单个字符串。
那么,我怎样才能删除所有不在该向量中的词呢?或者,我怎样才能 select 只有向量中的单词?
如果您希望 x
作为 grep 的正则表达式模式,只需使用 x <- paste(x, collapse = "|")
,这样您就可以在 text
中查找这些词。但请记住,正则表达式可能仍然太大。如果你想删除任何 不是 和 stopword()
的词,你可以创建自己的函数:
keep_stopwords <- function(text) {
stop_regex <- paste(stopwords(), collapse = "\b|\b")
stop_regex <- paste("\b", stop_regex, "\b", sep = "")
tmp <- strsplit(text, " ")[[1]]
idx <- grepl(stop_regex, tmp)
txt <- paste(tmp[idx], collapse = " ")
return(txt)
}
text = "How much wood would a woodchuck if a woodchuck could chuck wood? More wood than most woodchucks would chuck if woodchucks could chuck wood, but less wood than other creatures like termites."
keep_stopwords(text)
# [1] "would a if a could than most would if could but than other"
基本上,我们只是将 stopwords()
设置为一个正则表达式,它会查找任何这些词。但是我们必须小心部分匹配,所以我们将每个停用词包装在 \b
中以确保它是完整匹配。然后我们拆分字符串,以便我们单独匹配每个单词并创建作为停用词的单词的索引。然后我们再次将这些单词粘贴在一起,return 将其作为一个字符串。
编辑
这是另一种方法,更简单易懂。它也不依赖正则表达式,这在大型文档中可能很昂贵。
keep_words <- function(text, keep) {
words <- strsplit(text, " ")[[1]]
txt <- paste(words[words %in% keep], collapse = " ")
return(txt)
}
x <- "How much wood would a woodchuck chuck if a woodchuck could chuck wood? More wood than most woodchucks would chuck if woodchucks could chuck wood, but less wood than other creatures like termites."
keep_words(x, stopwords())
# [1] "would a if a could than most could if a could but than other"
从文本或字符向量中删除停用词是很常见的。我使用 tm
包中的函数 removeWords
。
但是,我正在尝试删除 除了 停用词之外的所有词。我有一个名为 x
的单词列表。当我使用
removeWords(text, x)
我收到这个错误:
In gsub(sprintf("(*UCP)\b(%s)\b", paste(sort(words, decreasing = TRUE), PCRE pattern compilation error 'regular expression is too large'`
我也试过使用 grep
:
grep(x, text)
但这行不通,因为 x
是一个向量而不是单个字符串。
那么,我怎样才能删除所有不在该向量中的词呢?或者,我怎样才能 select 只有向量中的单词?
如果您希望 x
作为 grep 的正则表达式模式,只需使用 x <- paste(x, collapse = "|")
,这样您就可以在 text
中查找这些词。但请记住,正则表达式可能仍然太大。如果你想删除任何 不是 和 stopword()
的词,你可以创建自己的函数:
keep_stopwords <- function(text) {
stop_regex <- paste(stopwords(), collapse = "\b|\b")
stop_regex <- paste("\b", stop_regex, "\b", sep = "")
tmp <- strsplit(text, " ")[[1]]
idx <- grepl(stop_regex, tmp)
txt <- paste(tmp[idx], collapse = " ")
return(txt)
}
text = "How much wood would a woodchuck if a woodchuck could chuck wood? More wood than most woodchucks would chuck if woodchucks could chuck wood, but less wood than other creatures like termites."
keep_stopwords(text)
# [1] "would a if a could than most would if could but than other"
基本上,我们只是将 stopwords()
设置为一个正则表达式,它会查找任何这些词。但是我们必须小心部分匹配,所以我们将每个停用词包装在 \b
中以确保它是完整匹配。然后我们拆分字符串,以便我们单独匹配每个单词并创建作为停用词的单词的索引。然后我们再次将这些单词粘贴在一起,return 将其作为一个字符串。
编辑
这是另一种方法,更简单易懂。它也不依赖正则表达式,这在大型文档中可能很昂贵。
keep_words <- function(text, keep) {
words <- strsplit(text, " ")[[1]]
txt <- paste(words[words %in% keep], collapse = " ")
return(txt)
}
x <- "How much wood would a woodchuck chuck if a woodchuck could chuck wood? More wood than most woodchucks would chuck if woodchucks could chuck wood, but less wood than other creatures like termites."
keep_words(x, stopwords())
# [1] "would a if a could than most could if a could but than other"