Defines functions wordsPlusEmo strip_punc

# prepare text to analyze by stripping leading and trailing punctuation
# preserves contractions and all emoticons found in vader dictionary

strip_punc <- function(wpe) {
  for(i in 1:length(wpe)){
    #checks if word is emoticon found in vader lexicon
    if(!(tolower(wpe[i]) %in% vaderLexicon$V1)) {
      #if not, strip punctuation
      leadingAndTrailing <- "(^\\W+)|(\\W+$)"
      wpe[i] <- gsub(leadingAndTrailing, "", wpe[i])

wordsPlusEmo <- function(text) {
  #splits text into vector of words
  wpe <- unlist(strsplit(text, "\\s+"))
  #strips words of punctuation (unless the word is an emoticon)
  stripped <- strip_punc(wpe)

Try the vader package in your browser

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

vader documentation built on Sept. 7, 2020, 5:09 p.m.