1

Is there a way to modify words in the sentimentr package? For example I want to change the word "please" to have a negative score rather than a positive one. Now I'm using the function:

text$sentiment <- sentiment_by(text$Comment)

to evaluate sentence sentiment.

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
  • Looking at the docs, the rsentiment package has functions with the name "custom_x", but it looks like you are using function from the sentimentr package? – M.Viking Feb 13 '20 at 15:28
  • Yes Viking, you are right sorry about that. I've updated the post. If you know anything about this package please let me know – Xristos Solwmou Feb 13 '20 at 18:04

1 Answers1

1

One can modify the polarity dictionary used by the sentiment function of the sentimentr package as follows:

library(sentimenr)
library(lexicon)    

    # sample text
    text <- "Please please please, be nice."

    # output with default dictionary
    sentiment(text, polarity_dt = lexicon::hash_sentiment_jockers_rinker)

    # polarity of 'please' in default dictionary
    lexicon::hash_sentiment_jockers_rinker$y[lexicon::hash_sentiment_jockers_rinker$x %in% "please"]

    # create a modified dictionary
    mysentiment <- lexicon::hash_sentiment_jockers_rinker
    mysentiment$y[mysentiment$x %in% "please"] <- -1

    # modified polarity of 'please'
    mysentiment$y[mysentiment$x %in% "please"]

    # run sentiment with modified dictionary 
    sentiment(text, polarity_dt = mysentiment)

Similar results can be achieved with sentiment_by. And with either function it is possible to merge the text elements with the output:

    # sample text
    text <- data.frame(Comment = c(
      "Please please please, be nice.", 
      "You can please some of the people all of the time.",
      "You can please all of the people some of the time.", 
      "But you can’t please all of the people all of the time.",
      "Pleased to meet you, hope you guess my name."),
       stringsAsFactors = F)

    # add element_id column
    text$element_id <- 1:nrow(text)

    # run seniment_by with default dictionary
    sentiment_by(text$Comment, polarity_dt = lexicon::hash_sentiment_jockers_rinker)

    # polarity of please in default dictionary
    lexicon::hash_sentiment_jockers_rinker$y[lexicon::hash_sentiment_jockers_rinker$x %in% "please"]

    # create a modified dictionary
    mysentiment <- lexicon::hash_sentiment_jockers_rinker
    mysentiment$y[mysentiment$x %in% "please"] <- -1

    # modified polarity
    mysentiment$y[mysentiment$x %in% "please"]

    # run sentiment with modified dictionary 
    sentiments_modified <- sentiment_by(text$Comment, polarity_dt = mysentiment)

    # merge sentiment output with original text
    sentiments_tbl <- merge(sentiments_modified, text, by = "element_id")
    sentiments_tbl
xilliam
  • 2,074
  • 2
  • 15
  • 27
  • Thanks @xilliam very helpful. Is there a way to add the sentence in the output as well? For example: element_id sentence_id word_count sentiment sentence 1: 1 1 5 -1.118034 "Please please please, be nice. – Xristos Solwmou Feb 14 '20 at 08:26
  • Or is there a way to change the polarity in the sentiment_by function??? – Xristos Solwmou Feb 14 '20 at 08:52
  • Thank you very much xilliam, very helpful. Also, do you know of a way to add my custom words and scores in the mysentiment table?? Currently i am using : mysentiment[nrow(mysentiment) + 1 ] <- c("fix","-1") but its not adding the new row (although i do not get an error message) – Xristos Solwmou Feb 14 '20 at 11:12
  • @ xiliam,thanks for your reply. I have created the dataframe x and merged it with mysentiment dataframe using: mysentiment2 <- rbind.data.frame(mysentiment,x). Indeed the new rows are added, but when i use the text$sentiment <- sentiment_by(get_sentences(text$Comment), polarity_dt = mysentiment2) command, i now get the following error: Error in `[.data.table`(polarity_dt, word_dat[["words"]]) : When i is a data.table (or character vector), the columns to join by must be specified either using 'on=' argument (see ?data.table) or by keying x (i.e. sorted, and, marked as sorted, see ?setkey). – Xristos Solwmou Feb 14 '20 at 12:50
  • Btw i don't understand why this is not working since: text$sentiment <- sentiment_by(get_sentences(text$Comment), polarity_dt = mysentiment) works fine – Xristos Solwmou Feb 14 '20 at 12:55
  • 1
    That suggestion was incorrect. Try this ```df <- data.frame(x = c("virus", "cat"), y= c(-1, 1), stringsAsFactors = F)``` followed by ```mysentiment <- update_key(mysentiment, x = df, sentiment = TRUE)``` – xilliam Feb 14 '20 at 13:48
  • You are welcome. But we should delete any comments that don't really have to do with the question/answer. SO guidelines ask us to limit comments to "Comments are used to ask for clarification or to point out problems in the post." This message will self destruct later today. – xilliam Feb 14 '20 at 14:53