【问题标题】:Extract top positive and negative features when applying dictionary in quanteda在 quanteda 中应用字典时提取顶部的正面和负面特征
【发布时间】:2019-06-24 08:55:04
【问题描述】:

我有一个包含文本数据的大约 100k 行的数据框。使用 quanteda 包,我应用情感分析(Lexicoder 字典)来最终计算情感分数。 对于一个额外的 - 更定性的 - 分析步骤,我想提取最重要的特征(即字典中在我的数据中最常出现的负面/正面单词),以检查话语是否由特定单词驱动。

my_corpus <- corpus(my_df, docid_field = "ID", text_field = "my_text", metacorpus = NULL, compress = FALSE)
sentiment_corp <- dfm(my_corpus, dictionary = data_dictionary_LSD2015)

但是,通过quanteda documentation,我无法弄清楚如何实现这一点 - 有没有办法? 我知道topfeatures 并且我确实读过this question,但它没有帮助。

【问题讨论】:

    标签: r dictionary sentiment-analysis quanteda


    【解决方案1】:

    在所有采用 pattern 参数的 quanteda 函数中,有效的模式类型是字符向量、列表和字典。因此,评估每个字典类别(我们也称为字典key)中每个顶级特征的最佳方法是在该字典上进行选择,然后使用topfeatures()

    以下是使用内置 data_corpus_irishbudget2010 对象的方法,例如,使用 Lexicoder 情感词典。

    library("quanteda")
    ## Package version: 1.4.3
    
    # tokenize and select just the dictionary value matches
    toks <- tokens(data_corpus_irishbudget2010) %>%
      tokens_select(pattern = data_dictionary_LSD2015)
    lapply(toks[1:5], head)
    ## $`Lenihan, Brian (FF)`
    ## [1] "severe"        "distress"      "difficulties"  "recovery"     
    ## [5] "benefit"       "understanding"
    ## 
    ## $`Bruton, Richard (FG)`
    ## [1] "failed"   "warnings" "sucking"  "losses"   "debt"     "hurt"    
    ## 
    ## $`Burton, Joan (LAB)`
    ## [1] "remarkable" "consensus"  "Ireland"    "opposition" "knife"     
    ## [6] "dispute"   
    ## 
    ## $`Morgan, Arthur (SF)`
    ## [1] "worst"     "worst"     "well"      "corrupt"   "golden"    "protected"
    ## 
    ## $`Cowen, Brian (FF)`
    ## [1] "challenge"      "succeeding"     "challenge"      "oppose"        
    ## [5] "responsibility" "support"
    

    要探索肯定条目的顶部匹配项,我们可以通过将字典设置为 Positive 键来进一步选择它们。

    # top positive matches
    tokens_select(toks, pattern = data_dictionary_LSD2015["positive"]) %>%
      dfm() %>%
      topfeatures()
    ##    benefit    support   recovery       fair     create confidence 
    ##         68         52         44         41         39         37 
    ##    provide       well     credit       help 
    ##         36         33         31         29
    

    对于负数:

    # top negative matches
    tokens_select(toks, pattern = data_dictionary_LSD2015[["negative"]]) %>%
      dfm() %>%
      topfeatures()
    ##    ireland    benefit        not    support     crisis   recovery 
    ##         79         68         52         52         47         44 
    ##       fair     create    deficit confidence 
    ##         41         39         38         37
    

    为什么“Ireland”是否定匹配?因为 LSD2015 包含 ir* 作为否定词,旨在匹配 ireireful 但使用默认的不区分大小写匹配,也匹配 Ireland(本示例语料库中经常使用的术语)。这是一个“误报”匹配的示例,在使用通配符或使用多义词和同形异义词率非常高的语言(例如英语)时,在字典中总是存在风险。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-01
      • 2019-01-27
      • 2017-12-05
      • 2016-10-11
      • 2020-10-08
      • 1970-01-01
      • 2015-09-20
      • 1970-01-01
      相关资源
      最近更新 更多