r - 在标记 ngram 中检测相同的单词并删除它们
问题描述
在 dfm 中,如何在 ngram 中检测到相同的单词,即
hello_hello, text_text
并将它们从 dfm 中删除?
解决方案
对于您的 ngram 元素由 连接的 dfm,_
您可以拆分它们并确定哪些是相同的。
library("quanteda")
## Package version: 2.1.2
dfmat <- dfm(c("test1_test1", "test1_test2", "test2_test2_test2", "test2_other", "other"))
featsplit <- strsplit(featnames(dfmat), "_")
same <- sapply(featsplit, function(y) {
length(y) >= 2 & # it's a compound (ngram)
length(unique(y)) == 1 # all elements are the same
})
same
## [1] TRUE FALSE TRUE FALSE FALSE
然后,您可以使用它来选择 dfm 中不同的元素:
dfmat[, !same]
## Document-feature matrix of: 5 documents, 3 features (80.0% sparse).
## features
## docs test1_test2 test2_other other
## text1 0 0 0
## text2 1 0 0
## text3 0 0 0
## text4 0 1 0
## text5 0 0 1
如果您的 ngram 连接器是不同的字符,只需将其替换为_
.
推荐阅读
- java - 如何从 gradle idea 插件中排除 jar
- awk - 根据列属性重复行
- xamarin - 如何滚动到 iOS 的 xamarin ui 测试中的特定元素
- django - 尝试对用户进行身份验证时出现类型错误
- javascript - 在 Javascript 中使用变量在调用查询的 cfml 标记中添加功能
- sql-server - SSIS 2014 - 列镜像
- javascript - express-validator - req.checkBody 不是函数
- php - 每个 jquery 访问值
- php - 使用 php/codeigntier 按月组织 MySQL 数据
- sql - How to show the final count in each row returned?