Можно изменить словарь полярности, используемый функцией 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