【问题标题】:Identify WHICH words in a document have been matched by dictionary lookup and how many times通过字典查找识别文档中的哪些单词以及匹配了多少次
【发布时间】:2020-07-20 13:21:45
【问题描述】:

Quanteda 问题。

对于语料库中的每个文档,我试图找出字典类别中的哪些单词对该类别的总计数有贡献,以及多少。

换句话说,我想获得每个字典类别中已使用 tokens_lookup 和 dfm_lookup 函数匹配的特征的矩阵,以及它们在每个文档中的频率。因此,不是该类别中所有单词的总频率,而是每个单词的总频率。

有没有简单的方法可以得到这个?

【问题讨论】:

    标签: r dictionary quanteda


    【解决方案1】:

    执行此操作的最简单方法是遍历字典“键”(您称之为“类别”)并选择匹配项以为每个键创建一个 dfm。处理不匹配和复合字典值(例如“not fail”)需要几个步骤。

    我可以使用内置的就职地址语料库和 LSD2015 字典来演示这一点,该字典有四个键并包含多词值。

    循环遍历字典键以建立一个列表,每次执行以下操作:

    • 选择标记,但为未选择的标记留一个垫子;
    • 将多词标记组合成单个标记;
    • 将pad("")重命名为OTHER,这样我们就可以统计不匹配了;和
    • 创建 dfm。
    library("quanteda")
    ## Package version: 2.1.0
    
    toks <- tokens(tail(data_corpus_inaugural, 3))
    
    dfm_list <- list()
    for (key in names(data_dictionary_LSD2015)) {
      this_dfm <- tokens_select(toks, data_dictionary_LSD2015[key], pad = TRUE) %>%
        tokens_compound(data_dictionary_LSD2015[key]) %>%
        tokens_replace("", "OTHER") %>%
        dfm(tolower = FALSE)
      dfm_list <- c(dfm_list, this_dfm)
    }
    names(dfm_list) <- names(data_dictionary_LSD2015)
    

    现在我们有了 dfm 对象列表中每个键的所有字典匹配项:

    dfm_list
    ## $negative
    ## Document-feature matrix of: 3 documents, 180 features (60.0% sparse) and 4 docvars.
    ##             features
    ## docs         clouds raging storms crisis war against violence hatred badly
    ##   2009-Obama      1      1      2      4   2       1        1      1     1
    ##   2013-Obama      0      1      1      1   3       1        0      0     0
    ##   2017-Trump      0      0      0      0   0       1        0      0     0
    ##             features
    ## docs         weakened
    ##   2009-Obama        1
    ##   2013-Obama        0
    ##   2017-Trump        0
    ## [ reached max_nfeat ... 170 more features ]
    ## 
    ## $positive
    ## Document-feature matrix of: 3 documents, 256 features (53.0% sparse) and 4 docvars.
    ##             features
    ## docs         grateful trust mindful thank well generosity cooperation
    ##   2009-Obama        1     2       1     1    2          1           2
    ##   2013-Obama        0     0       0     0    4          0           0
    ##   2017-Trump        1     0       0     1    0          0           0
    ##             features
    ## docs         prosperity peace skill
    ##   2009-Obama          3     4     1
    ##   2013-Obama          1     3     1
    ##   2017-Trump          1     0     0
    ## [ reached max_nfeat ... 246 more features ]
    ## 
    ## $neg_positive
    ## Document-feature matrix of: 3 documents, 2 features (33.3% sparse) and 4 docvars.
    ##             features
    ## docs         not_apologize OTHER
    ##   2009-Obama             1  2687
    ##   2013-Obama             0  2317
    ##   2017-Trump             0  1660
    ## 
    ## $neg_negative
    ## Document-feature matrix of: 3 documents, 5 features (53.3% sparse) and 4 docvars.
    ##             features
    ## docs         not_fight not_sap not_grudgingly not_fail OTHER
    ##   2009-Obama         0       0              1        0  2687
    ##   2013-Obama         1       1              0        0  2313
    ##   2017-Trump         0       0              0        1  1658
    

    【讨论】:

    • 谢谢,这太完美了!
    猜你喜欢
    • 2010-10-11
    • 2016-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-09
    • 1970-01-01
    • 2020-09-26
    • 2017-04-15
    相关资源
    最近更新 更多