R count how often words from a list appear in a sentence

旧街凉风 提交于 2019-12-11 12:16:31

问题


Currently participating in a MOOC and trying my hand at some sentiment analysis, but having trouble with the R code.

What I have is a list of bad words and a list of good words. For instance my bad words are c("dent", "broken", "wear", "cracked") ect.

I have a list of descriptions in my data frame, what I want to do is get a count on how many of my bad words appear in the list and how many of my good words appear for each row.

for instance suppose this is my data frame

desc = c("this screen is cracked", "minor dents and scratches", "100% good",     "in perfect condition")
id = c(1,2,3,4)
df = data.frame(id, desc)
bad.words = c("cracked", "scratches", "dents")

what I want is to make a sum column that counts how often each bad word appears in the description

so hoping my final df would look like

id    desc                        sum
1     "this screen is cracked"    1
2     "minor dents and scratches" 2
3     "100% good"                 0
4     "in perfect condition"      0

what I have so far is

df$sum <- grepl(paste( bad.words, collapse="|"), df$desc)

which only gets me a true or false if a word appears


回答1:


If you are finding a sum, vapply() is more appropriate than sapply(). You could do

library(stringi)
df$sum <- vapply(df$desc, function(x) sum(stri_count_fixed(x, bad.words)), 1L)

Which gives

df
#   id                      desc sum
# 1  1    this screen is cracked   1
# 2  2 minor dents and scratches   2
# 3  3                 100% good   0
# 4  4      in perfect condition   0



回答2:


Since you're likely going to try different lists of words, like good.words, bad.words, really.bad.words; I would write a function. I like lapply, but vapply and others will work too.

countwords <- function(x,comparison){
  lapply(x,function(x,comparewords){
    sum(strsplit(x,' ')[[1]] %in% comparewords)
  },comparewords = comparison)
}
df$good <- countwords(df$desc,good.words)
df$bad <- countwords(df$desc,bad.words)

The tm package is useful as well, after you're content with learning and moving to production speed.



来源:https://stackoverflow.com/questions/31776276/r-count-how-often-words-from-a-list-appear-in-a-sentence

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!