R/sent.R

Defines functions sentiment_est sentiment_afinn sentiment_bing sentiment_syuzhet sentiment_vader

sentiment_est <- function(x, dict) {
  if (is.character(x)) {
    ## this removes URLs
    x <- gsub("https?://\\S+", "", x)
    x <- tokenizers::tokenize_words(
      x, lowercase = TRUE, strip_punct = TRUE, strip_numeric = FALSE
    )
  }
  vapply(
    x,
    function(.x) sum(dict$value[match(.x, dict$word)], na.rm = TRUE),
    FUN.VALUE = numeric(1),
    USE.NAMES = FALSE
  )
}

sentiment_afinn <- function(x) {
  sentiment_est(x, afinn_dict)
}

sentiment_bing <- function(x) {
  sentiment_est(x, bing_dict)
}

sentiment_syuzhet <- function(x) {
  sentiment_est(x, syuzhet_dict)
}

sentiment_vader <- function(x) {
  sentiment_est(x, vader_dict)
}

Try the textfeatures package in your browser

Any scripts or data that you put into this service are public.

textfeatures documentation built on Sept. 4, 2019, 1:05 a.m.