R 基于正则表达式对字符串距离度量进行加权

R 基于正则表达式对字符串距离度量进行加权,r,R,是否可以对字符串距离度量(如Damerau-Levenshtein距离)进行加权,其中权重根据字符类型而变化 我希望创建一个模糊的地址匹配,并需要以不同的方式对数字和字母进行加权,以便地址如下: “5詹姆斯街”和“5杰迈斯街”被认为是相同的和 “詹姆斯街5号”和“詹姆斯街6号”被视为不同的 在应用字符串距离之前,我考虑将地址拆分为数字和字母,但是这将错过“5a”和“5b”的单位。数据集之间的顺序也不一致,因此一个条目可能是“James Street 5” 目前,我在stringdist包中使用R

是否可以对字符串距离度量(如Damerau-Levenshtein距离)进行加权,其中权重根据字符类型而变化

我希望创建一个模糊的地址匹配,并需要以不同的方式对数字和字母进行加权,以便地址如下:

“5詹姆斯街”和“5杰迈斯街”被认为是相同的

“詹姆斯街5号”和“詹姆斯街6号”被视为不同的

在应用字符串距离之前,我考虑将地址拆分为数字和字母,但是这将错过“5a”“5b”的单位。数据集之间的顺序也不一致,因此一个条目可能是“James Street 5”

目前,我在stringdist包中使用R,但不限于此


谢谢

这里有一个想法。这需要一些手动处理,但这可能是一个很好的起点。首先,我们使用
adist()

m <- adist(v) 
rownames(m) <- v

> m
#                     [,1] [,2] [,3] [,4] [,5] [,6] [,7]
#5 James Street          0    2    3    1    4   17   17
#5 Jmaes Street          2    0    4    3    6   17   17
#5#Jam#es Str$eet        3    4    0    4    6   17   17
#6 James Street          1    3    4    0    4   17   17
#James Street 5          4    6    6    4    0   16   17
#10a Cold Winter Road   17   17   17   17   16    0    1
#10b Cold Winter Road   17   17   17   17   17    1    0
library(dplyr)
res <- data.frame(cluster = cutree(cl, 2)) %>%
  tibble::rownames_to_column("address") %>%
  mutate(
    # Extract all components of the address
    lst = stringi::stri_extract_all_words(address),
    # Identify the component containing the street number and return it
    num = purrr::map_chr(lst, .f = ~ grep("\\d+", .x, value = TRUE))) %>% 
  # For each cluster, tag matching street numbers
  mutate(group = group_indices_(., .dots = c("cluster", "num")))

然后,我们将每条街道标记为其对应的相似性簇,遍历它们并检查匹配的街道编号

m <- adist(v) 
rownames(m) <- v

> m
#                     [,1] [,2] [,3] [,4] [,5] [,6] [,7]
#5 James Street          0    2    3    1    4   17   17
#5 Jmaes Street          2    0    4    3    6   17   17
#5#Jam#es Str$eet        3    4    0    4    6   17   17
#6 James Street          1    3    4    0    4   17   17
#James Street 5          4    6    6    4    0   16   17
#10a Cold Winter Road   17   17   17   17   16    0    1
#10b Cold Winter Road   17   17   17   17   17    1    0
library(dplyr)
res <- data.frame(cluster = cutree(cl, 2)) %>%
  tibble::rownames_to_column("address") %>%
  mutate(
    # Extract all components of the address
    lst = stringi::stri_extract_all_words(address),
    # Identify the component containing the street number and return it
    num = purrr::map_chr(lst, .f = ~ grep("\\d+", .x, value = TRUE))) %>% 
  # For each cluster, tag matching street numbers
  mutate(group = group_indices_(., .dots = c("cluster", "num")))
然后,您可以使用
distinct()
,基于
pull()
唯一地址:


数据

v <- c("5 James Street", "5 Jmaes Street", "5#Jam#es Str$eet", "6 James Street",
       "James Street 5", "10a Cold Winter Road", "10b Cold Winter Road")

v您考虑过为这个项目尝试地理编码API吗?@AndrewBrēza感谢您的建议。我确实考虑过这一点,但仍然希望匹配一个地址,该地址可能写得不恰当,API无法识别。例如“5#Jam#es Str$eet”仍然匹配,很抱歉这里没有更具体!谷歌地图API应该仍然能够识别拼写错误的地名。当我在谷歌地图上搜索5#Jam#es Str$eet时,结果中会显示正确的位置。这是一个很好的建议。这是一个很好的基础。我认为adist将错过一些匹配,但我可以使用一些字符串距离度量来捕获它们!谢谢
v <- c("5 James Street", "5 Jmaes Street", "5#Jam#es Str$eet", "6 James Street",
       "James Street 5", "10a Cold Winter Road", "10b Cold Winter Road")