【问题标题】:modifying polarity words in sentimentr package修改感测器包中的极性词
【发布时间】:2020-02-13 14:15:40
【问题描述】:

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

text$sentiment <- sentiment_by(text$Comment)

评估句子情感。

【问题讨论】:

  • 查看文档,rsentiment 包有名为“custom_x”的函数,但看起来您使用的是感测器包中的函数?
  • 是的,维京人,您对此感到抱歉。我已经更新了帖子。如果您对这个包有任何了解,请告诉我

标签: r sentiment-analysis sentimentr


【解决方案1】:

可以修改sentimentr包的sentiment函数使用的极性字典如下:

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

【讨论】:

  • 感谢@xilliam 非常有帮助。有没有办法在输出中添加句子?例如: element_id sentence_id word_count 情感句子 1: 1 1 5 -1.118034 " Please please please, be nice.
  • 或者有没有办法改变sentiment_by函数中的极性???
  • 非常感谢 xilliam,非常有帮助。另外,您知道在 mysentiment 表中添加我的自定义单词和分数的方法吗?目前我正在使用: mysentiment[nrow(mysentiment) + 1 ]
  • @xiliam,感谢您的回复。我创建了数据框 x 并将其与 mysentiment 数据框合并使用:mysentiment2 [.data.table(polarity_dt, word_dat[[ "words"]]) :当 i 是 data.table(或字符向量)时,必须使用 'on=' 参数(参见 ?data.table)或键入 x(即排序,并且,标记为已排序,请参阅 ?setkey)。
  • 该建议不正确。试试这个df &lt;- data.frame(x = c("virus", "cat"), y= c(-1, 1), stringsAsFactors = F),然后是mysentiment &lt;- update_key(mysentiment, x = df, sentiment = TRUE)
猜你喜欢
  • 2022-06-26
  • 1970-01-01
  • 1970-01-01
  • 2021-11-08
  • 2019-12-27
  • 2017-03-28
  • 2018-09-18
  • 1970-01-01
  • 2014-07-22
相关资源
最近更新 更多