【问题标题】:how to get a sentiment score (and keep the sentiment words) in quanteda?如何在 quanteda 中获得情感分数(并保留情感词)?
【发布时间】:2020-05-27 15:35:14
【问题描述】:

考虑这个简单的例子

library(tibble)
library(quanteda)

tibble(mytext = c('this is a good movie',
                  'oh man this is really bad',
                  'quanteda is great!'))

# A tibble: 3 x 1
  mytext                   
  <chr>                    
1 this is a good movie     
2 oh man this is really bad
3 quanteda is great!   

我想进行一些基本的情绪分析,但有一点不同。这是我的字典,存储在常规的tibble

mydictionary <- tibble(sentiment = c('positive', 'positive','negative'),
                       word = c('good', 'great', 'bad'))

# A tibble: 3 x 2
  sentiment word 
  <chr>     <chr>
1 positive  good 
2 positive  great
3 negative  bad  

本质上,我想计算每个句子中检测到的正面和负面单词的数量,同时还要跟踪匹配的单词。换句话说,输出应该是这样的

                          mytext nb.pos nb.neg   pos.words
1 this is a good and great movie      2      0 good, great
2      oh man this is really bad      0      1         bad
3             quanteda is great!      1      0       great

如何在quanteda 中做到这一点?这可能吗? 谢谢!

【问题讨论】:

    标签: r quanteda


    【解决方案1】:

    敬请关注 quanteda v. 2.1,其中我们将大大扩展专用的情绪分析功能。与此同时,见下文。请注意,我做了一些调整,因为您报告的文本和输入文本存在差异,而且您在pos.words 中有所有情绪词,而不仅仅是正面词。下面,我计算正面和所有情绪匹配。

    # note the amended input text
    mytext <- c(
      "this is a good and great movie",
      "oh man this is really bad",
      "quanteda is great!"
    )
    
    mydictionary <- tibble::tibble(
      sentiment = c("positive", "positive", "negative"),
      word = c("good", "great", "bad")
    )
    
    library("quanteda", warn.conflicts = FALSE)
    ## Package version: 2.0.9000
    ## Parallel computing: 2 of 8 threads used.
    ## See https://quanteda.io for tutorials and examples.
    
    # make the dictionary into a quanteda dictionary
    qdict <- as.dictionary(mydictionary)
    

    现在我们可以使用查找函数来获取最终的 data.frame。

    # get the sentiment scores
    toks <- tokens(mytext)
    df <- toks %>%
      tokens_lookup(dictionary = qdict) %>%
      dfm() %>%
      convert(to = "data.frame")
    names(df)[2:3] <- c("nb.neg", "nb.pos")
    
    # get the matches for pos and all words
    poswords <- tokens_keep(toks, qdict["positive"])
    allwords <- tokens_keep(toks, qdict)
    
    data.frame(
      mytext = mytext,
      df[, 2:3],
      pos.words = sapply(poswords, paste, collapse = ", "),
      all.words = sapply(allwords, paste, collapse = ", "),
      row.names = NULL
    )
    ##                           mytext nb.neg nb.pos   pos.words   all.words
    ## 1 this is a good and great movie      0      2 good, great good, great
    ## 2      oh man this is really bad      1      0                     bad
    ## 3             quanteda is great!      0      1       great       great
    

    【讨论】:

    • 谢谢肯!这很棒!在我的真实示例中,我有几百万行。即使有那么多句子,你认为这是正确的方法吗?
    • 期待v 2.1!什么时候发布?
    • 这样想,是的 - 我以前在 6-10 百万个 Tweet 语料库上使用过它。如果您遇到瓶颈,请随时报告 GitHub 问题,以便我们专注于该问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多