【问题标题】:Compute the Euclidean distance using word counts使用字数计算欧几里得距离
【发布时间】:2024-05-07 06:50:01
【问题描述】:

考虑以下两句话。

句子 1:敏捷的棕狐跳过懒狗。

句子 2:一只敏捷的棕色狗胜过一只敏捷的狐狸。

使用字数计算欧几里得距离。

【问题讨论】:

    标签: nlp text-analysis


    【解决方案1】:

    您可以使用包tm 来查找字数,然后计算欧几里得距离

    > library(tm)
    > s1 <- " The quick brown fox jumps over the lazy dog"
    > s2 <- "A quick brown dog outpaces a quick fox"
    > 
    > VS <- VectorSource(c(s1,s2))
    > corp <- Corpus(VS)
    > dtm <- DocumentTermMatrix(corp)
    > d <- dist(t(dtm), method = 'euclidean')
    > d
    
    
    
            brown      dog      fox    jumps     lazy outpaces     over    quick
    dog      0.000000                                                               
    fox      0.000000 0.000000                                                      
    jumps    1.000000 1.000000 1.000000                                             
    lazy     1.000000 1.000000 1.000000 0.000000                                    
    outpaces 1.000000 1.000000 1.000000 1.414214 1.414214                           
    over     1.000000 1.000000 1.000000 0.000000 0.000000 1.414214                  
    quick    1.000000 1.000000 1.000000 2.000000 2.000000 1.414214 2.000000         
    the      1.414214 1.414214 1.414214 1.000000 1.000000 2.236068 1.000000 2.236068
    

    【讨论】: