Chapter 16 Feature Sentiment Score

We calculate the sentiment score for each line thru the following code.

getSentimentScore = function(train)
{
  sentiment_lines  =  train %>%
  unnest_tokens(word, text) %>%
  inner_join(get_sentiments("afinn"), by = "word") %>%
  group_by(id) %>%
  summarize(sentiment = mean(score))

sentiment_lines = sentiment_lines %>%
    right_join(train, by = "id") 

sentiment_lines = sentiment_lines %>% mutate(sentiment = ifelse(is.na(sentiment),0,sentiment))

return(sentiment_lines$sentiment)

}