Retrieving sentence score based on values of words in a dictionary

后端 未结 2 1832
陌清茗
陌清茗 2021-01-13 08:34

Edited df and dict

I have a data frame containing sentences:

df <- data_frame(text = c(\"I love pandas         


        
2条回答
  •  没有蜡笔的小新
    2021-01-13 09:22

    A bit of double looping via sapply and gregexpr:

    res <- sapply(dict$word, function(x) {
      sapply(gregexpr(x,df$text),function(y) length(y[y!=-1]) )
    })
    rowSums(res * dict$score)
    #[1]  2 -2
    

    This also accounts for when there is multiple matches in a single string:

    df <- data.frame(text = c("I love love pandas", "I hate monkeys"))
    # run same code as above
    #[1]  3 -2
    

提交回复
热议问题