r - 如何删除不同版本的停用词
问题描述
我用这种方式从文本中删除停用词
dfm <-
tokens(df$text,
remove_punct = TRUE,
remove_numbers = TRUE,
remove_symbols = TRUE) %>%
tokens_remove(pattern = stopwords(source = "smart")) %>%
tokens_wordstem()
但是在结果中我发现有这样的停用词:
dont
有没有办法在不使用自定义停用词列表的情况下删除它们?
解决方案
当您说“删除它们”时,我假设您的意思是dont
从标记中删除,而现有的停用词列表仅删除don’t
. (尽管从您的问题或某些答案的解释中,这一点并不完全清楚。)quanteda框架中存在两个简单的解决方案。
首先,您可以在tokens_remove()
调用中附加额外的移除模式。
其次,您可以处理由返回的字符向量stopwords()
以包含不带撇号的版本。
插图:
library("quanteda")
## Package version: 1.5.1
toks <- tokens("I don't know what I dont or cant know.")
# original
tokens_remove(toks, c(stopwords("en")))
## tokens from 1 document.
## text1 :
## [1] "know" "dont" "cant" "know" "."
# manual addition
tokens_remove(toks, c(stopwords("en"), "dont", "cant"))
## tokens from 1 document.
## text1 :
## [1] "know" "know" "."
# automatic addition to stopwords
tokens_remove(toks, c(
stopwords("en"),
stringi::stri_replace_all_fixed(stopwords("en"), "'", "")
))
## tokens from 1 document.
## text1 :
## [1] "know" "know" "."
推荐阅读
- redux - 从 redux-observable 中调度多个动作
- sql - 我需要一个包含列 CatId 和 Flag 的视图。如果存在 CatID 的任何子类别,则标志将为 1
- python - 如何使用线程和回调函数测试python?
- excel - 如何从数据集逆向工程预测
- laravel - 如何从 Laravel 中具有多个角色的用户那里获取权限?
- ruby-on-rails - 按类别计算总预订量 - Rails
- javascript - 我们可以为同一个文本框使用多种文本颜色吗?
- sql-server - 如何在没有 FK 但只有查询连接的旧数据库上添加外键 (FK)
- javascript - 无法将对象数组映射到 jQuery 自动完成
- c++ - 带有复选框的 Qtreewidgetitem