2

我有一个带有一些文本数据的df,例如

words <- data.frame(terms = c("qhick brown fox",
                              "tom dick harry", 
                              "cats dgs",
                              "qhick black fox"))

我已经能够根据包含拼写错误的任何行进行子集化:

library(qdap)
words[check_spelling(words$terms)$row,,drop=F]

但鉴于我有很多文本数据,我只想过滤更频繁发生的拼写错误:

> sort(which(table(which_misspelled(toString(unique(words$terms)))) > 1), decreasing = T)
qhick 
    2 

所以我现在知道“qhick”是一个常见的拼写错误。

然后我怎么能根据这张表子集单词呢?所以只返回包含“qhick”的行?

4

1 回答 1

1

这些词本身就是你的sort()函数的名称。如果你只有一个名字,你可以这样做:

top_misspelled <- sort(which(table(which_misspelled(toString(unique(words$terms)))) > 1), decreasing = T)

words[grepl(names(top_misspelled), words$terms), , drop = F]
#            terms
#1 qhick brown fox
#4 qhick black fox

但是,如果您有多个,则可以将它们折叠在一起以构建grepl如下查找:

words[grepl(paste0(names(top_misspelled), collapse = "|"), words$terms), ,drop = F]

非正则表达式选项也可以将每一行拆分为单词,然后如果该行中的任何单词与您感兴趣的字符串匹配,则返回该行:

words[sapply(strsplit(as.character(words[,"terms"]), split=" "), function(x) any(x %in% names(top_misspelled))),
      ,drop = F]

#            terms
#1 qhick brown fox
#4 qhick black fox
于 2017-06-30T02:15:22.177 回答