r - 在 r 中的值内过滤 n char
问题描述
我正在做情绪分析,但我需要在每条推文中按 n char 过滤。我是说:
df <- c("the most beauty", "the most ugly", "you are beauty")
Library(dplyr)
df %>%
filter((n char >3) %in% df)
我期待这样的结果:“最美丽”、“丑陋”、“美丽”
我试过$str_detect
但没用
解决方案
对于预先确定的情感分析过滤nchar()
可能有点粗糙。我建议查看library,它可以让您将有意义的文本单元(如单词)标记为整洁的数据结构。tidytext
在您的情况下,您可以将每个单词转换为一个标记并重塑数据框,以便每个标记(或单词)位于单独的行上。然后,您可以轻松过滤掉文章和其他不相关的东西。例如:
library(dplyr)
library(tidytext)
df <- c("the most beauty", "the most ugly", "you are beauty")
text_df <- data_frame(line = 1:3, text = df)
text_df %>%
unnest_tokens(word, text)
# A tibble: 9 x 2
line word
<int> <chr>
1 1 the
2 1 most
3 1 beauty
4 2 the
5 2 most
6 2 ugly
7 3 you
8 3 are
9 3 beauty
然后,只需用不需要的词向量过滤掉任何词。
remove_words <- c("the", "a", "you", "are")
text_df %>%
unnest_tokens(word, text) %>% filter(!(word %in% remove_words))
# A tibble: 5 x 2
line word
<int> <chr>
1 1 most
2 1 beauty
3 2 most
4 2 ugly
5 3 beauty
标记化允许您通过对推文中所有单词的情绪分数求和来轻松计算每条推文的情绪分数。示例可以在这里找到:https ://www.tidytextmining.com/sentiment.html
推荐阅读
- java - Selenium - 我怎样才能刮掉这张桌子?
- python - ValueError:无法查询“用户名”:必须是“用户”实例
- amazon-web-services - 检查 Route 53 Alias 记录映射到 ELB 的 URL 的命中次数
- c# - linq 减去列表中的数字
- django - 如何使用基于类的自定义验证器在 Django 中即时验证 CSV 文件上传?
- mysql - 查询数据以及mysql中有多少行有该日期
- javascript - react js功能组件中的条件渲染
- regex - 正则表达式查找正则表达式的多个外观
- javascript - 当涉及 JS 库时,如何查找哪个 Javascript 函数正在操作特定的 DOM 元素
- python - 如何在 Python 中从 S3 存储桶访问数据集?