我在字符向量中有一组独特的词(已被“阻止”),我想知道在一个字符串中出现了多少个词。
这是我到目前为止的内容:
library(RTextTools)
string <- "Players Information donation link controller support years fame glory addition champion Steer leader gang ghosts life Power Pellets tables gobble ghost"
wordstofind <- c("player","fame","field","donat")
# I created a stemmed list of the string
string.stem <- colnames(create_matrix(string, stemWords = T, removeStopwords = F))
我知道下一步可能涉及grepl("\\bword\\b,value")
或使用了正则表达式,但是我不确定在这种情况下最快的选择是什么。
这是我的标准:
朝正确方向的任何推动都将是巨大的。
好吧,我从未使用过庞大的数据集,因此时间从来都不是最重要的,但是鉴于您提供的数据,这将使您对与单词中的某个单词完全匹配的单词数进行计数。可能是一个很好的起点。
sum(wordstofind %in% unlist(strsplit(string, " ")))
> sum(wordstofind %in% unlist(strsplit(string, " ")))
[1] 1
编辑感谢@Anthony Bissel,使用词干获得了正确的3个匹配项:
sum(wordstofind %in% unlist(string.stem))
> sum(wordstofind %in% unlist(string.stem))
[1] 3
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句