You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

文本挖掘词关联结果无意义问题求助(附R代码与数据)

R语言文本分析词关联出现无意义词汇的问题

我用R语言做文本分析获取词关联结果,但输出的关联结果毫无意义。比如我关注“women”和其他词汇的关联,却得到“bagthey”这类拼接出来的无意义词。试了运行和不运行“消除多余空格”的代码,问题还是存在。

问题根源

  • 预处理顺序完全错误:你先做了词干提取,再去除标点,导致原本被标点分隔的单词(比如bag.they)去掉标点后直接拼接成bagthey,后续词干处理又保留了这个错误组合。
  • 重复操作冗余:重复执行tolower和stemDocument,不仅无效还可能干扰处理流程。
  • 关键清理步骤被注释:stripWhitespace被注释,即使开启,也因为前面的顺序错误无法解决单词拼接问题。

修正后的预处理逻辑

正确顺序必须是:先清理噪声,再做标准化处理

  1. 转小写
  2. 去除标点
  3. 去除数字
  4. 移除停用词/自定义词汇
  5. 消除多余空格
  6. 词干提取

修正后的完整代码

company <- read.csv("C:/Data.csv")

#### 数据预处理 ####

# 创建语料库
corpus_review <- Corpus(VectorSource(company$review))

# 统一转小写(仅执行一次)
corpus_review <- tm_map(corpus_review, content_transformer(tolower))

# 去除标点(优先处理,避免单词拼接)
corpus_review <- tm_map(corpus_review, removePunctuation, 
                        preserve_intra_word_contractions = TRUE, 
                        preserve_intra_word_dashes = TRUE,  
                        ucp = TRUE)

# 去除数字
corpus_review <- tm_map(corpus_review, removeNumbers)

# 移除英文停用词
corpus_review <- tm_map(corpus_review, removeWords, stopwords("english"))

# 移除自定义词汇
corpus_review <- tm_map(corpus_review, removeWords, 
                        c("also", "get","like", "made", "can", "im", "just","a", "i"))

# 消除多余空格(必须开启)
corpus_review <- tm_map(corpus_review, stripWhitespace)

# 词干提取(最后执行)
corpus_review <- tm_map(corpus_review, stemDocument)

# 构建词-文档矩阵
TextDoc_dtm <- TermDocumentMatrix(corpus_review)
dtm_m <- as.matrix(TextDoc_dtm)

# 按词频降序排序
dtm_v <- sort(rowSums(dtm_m), decreasing=TRUE)
dtm_d <- data.frame(word = names(dtm_v), freq=dtm_v)
head(dtm_d, 5)

#### 词关联分析 ####

associations <- findAssocs(TextDoc_dtm, "women", 0.1)
associations_df <- list_vect2df(associations)[, 2:3]

ggplot(associations_df, aes(y = associations_df[, 1])) + 
  geom_point(aes(x = associations_df[, 2]), 
             data = associations_df, size = 3) + 
  ggtitle("与关键词的词关联") + 
  theme_gdocs()

额外提醒

预处理顺序是文本分析的核心逻辑,噪声清理不彻底就做标准化,必然会产生无意义的拼接词;重复的预处理步骤只会增加无效计算,建议严格按流程执行。

内容的提问来源于stack exchange,提问作者Xian Zhao

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 05:35:29