изменение слова полярности в пакете сентиментра - PullRequest
1 голос
/ 13 февраля 2020

Есть ли способ изменить слова в пакете sentimentr? Например, я хочу изменить слово « пожалуйста », чтобы иметь отрицательный результат, а не положительный. Сейчас я использую функцию:

text$sentiment <- sentiment_by(text$Comment)

для оценки настроения предложения.

1 Ответ

1 голос
/ 13 февраля 2020

Можно изменить словарь полярности, используемый функцией sentiment пакета sentimentr, следующим образом:

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)

Аналогичные результаты могут быть достигнуты с помощью sentiment_by. И с любой функцией можно объединить текстовые элементы с выводом:

    # 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
...