【问题标题】:Interpretation question: Textstat_similarity Quanteda解读题:Textstat_similarity Quanteda
【发布时间】:2020-11-08 19:34:05
【问题描述】:

我有一个包含 310,225 条推文的数据集。我想知道有多少推文是相同或相似的。我使用 Quanteda 的 textstat 频率计算了推文之间的相似度。我发现相似度矩阵中距离1和0.9999的频率如下:

0.9999            1 
 2288           162743 

这是我的代码:

dfmat_users <- dfm_data %>% 
dfm_select(min_nchar = 2) %>% 
dfm_trim(min_termfreq = 10) 

dfmat_users <- dfmat_users[ntoken(dfmat_users) > 10,]

tstat_sim <- textstat_simil(dfmat_users, method = "cosine", margin = "documents", min_simil = 0.9998)

table(tstat_sim@x) #result of this code is given above.

我需要找出数据集中相似或相同推文的数量。我应该如何解释上面的结果?

【问题讨论】:

    标签: r text cosine-similarity quanteda sentence-similarity


    【解决方案1】:

    最简单的方法是将textstat_simil() 输出转换为唯一对的data.frame,然后过滤余弦值高于阈值(此处为.9999)的那些。

    为了说明,我们可以将内置的就职地址语料重塑成句子,然后在这些上计算相似度矩阵,然后对 data.frame 进行强制,使用 dplyr 过滤你想要的结果。

    library("quanteda", warn.conflicts = FALSE)
    ## Package version: 2.1.0.9000
    ## Parallel computing: 2 of 8 threads used.
    ## See https://quanteda.io for tutorials and examples.
    
    sim_df <- data_corpus_inaugural %>%
      corpus_reshape(to = "sentences") %>%
      dfm() %>%
      textstat_simil(method = "cosine") %>%
      as.data.frame()
    
    nrow(sim_df)
    ## [1] 12508670
    

    您可以将以下数据的条件调整为 0.9999 - 这里我使用 0.99 作为说明。

    library("dplyr", warn.conflicts = FALSE)
    filter(sim_df, cosine > .99)
    ##            document1       document2 cosine
    ## 1    1861-Lincoln.69 1861-Lincoln.71      1
    ## 2    1861-Lincoln.69 1861-Lincoln.73      1
    ## 3    1861-Lincoln.71 1861-Lincoln.73      1
    ## 4  1953-Eisenhower.6   1985-Reagan.6      1
    ## 5  1953-Eisenhower.6    1989-Bush.15      1
    ## 6      1985-Reagan.6    1989-Bush.15      1
    ## 7      1989-Bush.140  2009-Obama.108      1
    ## 8      1989-Bush.140   2013-Obama.87      1
    ## 9     2009-Obama.108   2013-Obama.87      1
    ## 10     1989-Bush.140    2017-Trump.9      1
    ## 11    2009-Obama.108    2017-Trump.9      1
    ## 12     2013-Obama.87    2017-Trump.9      1
    

    (而且:是的,这是对 1250 万句对之间余弦相似度的非常快速的计算!)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-08
      • 1970-01-01
      • 1970-01-01
      • 2018-10-21
      • 2022-08-13
      • 2012-06-28
      • 1970-01-01
      相关资源
      最近更新 更多