r - 是否有通过自定义字典清理的 R 函数
问题描述
在清理 R 中的数据时,我想使用自定义字典(超过 400,000 个单词)。我已经将字典加载为一个大字符列表,我正在尝试使用它,以便我的数据(VCorpus)中的内容妥协只有我字典里的单词。
例如:
#[1] "never give up uouo cbbuk jeez"
会成为
#[1*] "never give up"
因为“never”、“give”和“up”这些词都在自定义词典中。我以前尝试过以下方法:
#Reading the custom dictionary as a function
english.words <- function(x) x %in% custom.dictionary
#Filtering based on words in the dictionary
DF2 <- DF1[(english.words(DF1$Text)),]
但我的结果是一个单词的字符列表。有什么建议吗?
解决方案
由于您使用数据框,您可以尝试以下操作:
library(tidyverse)
library(tidytext)
dat<-tibble(text="never give up uouo cbbuk jeez")
words_to_keep<-c("never","give","up")
keep_function<-function(data,words_to_keep){
data %>%
unnest_tokens(word, text) %>%
filter(word %in% words_to_keep) %>%
nest(text=word) %>%
mutate(text = map(text, unlist),
text = map_chr(text, paste, collapse = " "))
}
keep_function(dat,words_to_keep)
推荐阅读
- c++ - 在模板中使用 vector::erase 时将 const 迭代器作为 'this' 参数传递会丢弃限定符
- typescript - Svelte 组件作为对象属性
- python-3.x - 将 SECRET_KEY 放入 .env 文件中的填充错误不正确
- java - 无法使用 NTriples (OWLAPI) 保存本体
- html - 在 iOS 应用程序中使用 XSLT 样式表从 XML 生成 HTML
- c# - InvalidOperationException:尚未为“/Views/_ViewImports.cshtml”处的页面调用 RenderBody。忽略调用 IgnoreBody()
- android - 在不使用 ADB 的情况下通过 USB 将数据从 Android 发送到 Windows
- java - 带有空正文响应的问题改造 Post API
- .net - 如何让应用程序在 WinPE 中运行?
- python - 当用户@the bot 时,Discord.py bot 不读取