1

有没有办法修改sentimentr包中的单词?例如,我想将“”一词更改为负分而不是正分。现在我正在使用该功能:

text$sentiment <- sentiment_by(text$Comment)

评估句子情感。

4

1 回答 1

1

可以修改包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
于 2020-02-13T20:51:11.033 回答