【问题标题】:R: Correct strings by distance measure (stringdistmatrix)R:通过距离测量正确字符串(stringdistmatrix)
【发布时间】:2017-12-16 19:30:37
【问题描述】:

我正在处理我需要计算字符串中唯一姓名的问题,但考虑到可能会有轻微的拼写错误。 我的想法是将字符串设置为低于某个阈值(例如,levenshtein 距离低于 2)是相等的。现在我设法计算字符串距离,但没有对我的输入字符串进行任何更改,以使我得到正确数量的唯一名称。

library(stringdist);library(stringr)
names<-"Michael, Liz, Miichael, Maria"
names_split<-strsplit(names, ", ")[[1]]
stringdistmatrix(names_split,names_split)
     [,1] [,2] [,3] [,4]
[1,]    0    6    1    5
[2,]    6    0    7    4
[3,]    1    7    0    6
[4,]    5    4    6    0
(number_of_people<-str_count(names, ",")+1)
[1] 4

number_of_people 的正确值当然应该是 3。

由于我只对唯一名称的数量感兴趣,因此我不担心“Michael”是否会被“Miichael”取代或相反。

【问题讨论】:

  • 不确定问题是否明确。考虑这些名字:Maria、Mara、Sara、Sarah。 Maria 和 Sarah 的距离 >2,但每个连续对的距离为 1。此外,大多数人会认为该名称列表包含 3 个唯一名称。

标签: r stringr stringdist


【解决方案1】:

一种选择是尝试根据距离矩阵对名称进行聚类:

library(stringdist)
# create a 'dist' object (=lower triangular part of distance matrix)
d <- stringdistmatrix(names_split,method="osa")
# use hierarchical clustering to group nearest neighbors
hc <- hclust(d)
# visual inspection: y-axis labels the distance value
plot(hc)
# decide what distance value you find acceptable for grouping.
cutree(hc, h=3)

根据您的实际数据,您需要尝试使用距离类型(qgrams/cosine 可能有用,或者在名称的情况下使用 jaro-winkler 距离)。

【讨论】:

    猜你喜欢
    • 2012-05-18
    • 1970-01-01
    • 2021-01-03
    • 2019-01-21
    • 2013-04-25
    • 2018-11-04
    • 1970-01-01
    • 2015-07-12
    • 1970-01-01
    相关资源
    最近更新 更多