# 分词,并过滤停用词 stop_words <- stop_words$word comments_words <- comments_df_clean$content %>% unnest_tokens(word, content) %>% anti_join(stop_words),优化代码,使其可以读取字符型数据
时间: 2024-03-18 22:38:35 浏览: 17
可以使用以下代码来进行分词并过滤停用词,同时支持读取字符型数据:
```R
library(tidytext)
library(dplyr)
# 读取停用词表
stop_words <- data.frame(word = stopwords("en"))
# 读取数据并进行清洗
comments_df <- read.csv("comments.csv", stringsAsFactors = FALSE)
comments_df_clean <- comments_df %>%
filter(!is.na(content)) %>%
mutate(content = tolower(content)) %>%
mutate(content = gsub("[^[:alnum:] ]", " ", content)) %>%
mutate(content = gsub("\\s+", " ", content))
# 分词并过滤停用词
comments_words <- comments_df_clean$content %>%
unnest_tokens(word, content) %>%
anti_join(stop_words, by = "word")
```
其中,`read.csv()`函数用于读取CSV文件,`stringsAsFactors = FALSE`参数指定字符型数据不转换为因子型。`filter()`函数用于过滤空值,`tolower()`函数用于将文本转换为小写,`gsub()`函数用于替换不需要的字符,`unnest_tokens()`函数用于将文本分词,`anti_join()`函数用于过滤停用词。