我编写了一个脚本来对公司名称进行模糊匹配。我正在匹配一些不完全正确的公司名称(即可能存在小的拼写错误或“inc。”后缀丢失)与“正确的”公司名称和ID的语料库相对应。显然,重点是将ID正确地附加到不总是正确的公司名称上。
以下是我匹配的数据集的一些非常简化的版本(我还没有使用zip-part,但稍后会再次使用它):
df <- data.frame(zip = c("4760","5445", "2200"), company = c("company x", "company y", "company z"))
corpus <- data.frame(zip = c("4760","5445", "2200", "2200", "2200"), company = c("company x inc.", "company y inc.", "company z inc.", "company a inc.", "company b inc."), id = c(12121212, 23232323, 34343434, 56565656, 67676767))
df
zip company
1 4760 company x
2 5445 company y
3 2200 company z
corpus
zip company id
1 4760 company x inc. 12121212
2 5445 company y inc. 23232323
3 2200 company z inc. 34343434
4 2200 company a inc. 56565656
5 2200 company b inc. 67676767
然后我使用以下代码来创建字符串距离矩阵
library(stringdist)
distance.method <- c("jw")
string.dist.matrix <- stringdistmatrix(tolower(corpus$company),
tolower(df$company),
method = distance.method,
nthread = getOption("sd_num_thread"))
string.dist.matrix
[,1] [,2] [,3]
[1,] 0.1190476 0.1798942 0.1798942
[2,] 0.1798942 0.1190476 0.1798942
[3,] 0.1798942 0.1798942 0.1190476
[4,] 0.1798942 0.1798942 0.1798942
[5,] 0.1798942 0.1798942 0.1798942
然后我继续匹配最小距离对。通常情况下,我希望将4000家公司与4.5万人的语料库相匹敌。公司,至少可以说需要一些计算能力。我的想法是,不是计算所有可能的对之间的字符串距离,而是仅为共享邮政编码的人计算它。正如我所看到的那样,对于更复杂的情况,结果将是一种较小的计算量和更精确的模拟匹配,而不是我在这里用简化数据说明的那些。
简而言之,我想要的结果矩阵是这样的:
[,1] [,2] [,3]
[1,] 0.1190476 NA NA
[2,] NA 0.1190476 NA
[3,] NA NA 0.1190476
[4,] NA NA 0.1798942
[5,] NA NA 0.1798942
我似乎无法找到一种方法来做到这一点。有什么想法吗?
答案 0 :(得分:3)
以下方法使用dplyr
并从phiver的joining
两个数据帧方法开始,然后继续生成类似于string.dist.matrix
的数据框或数据框中的数据框浓缩的“关键价值”形式。我已在您的df
数据框中添加了另一家公司,以包含具有相同df zip
的多家公司的案例。
距离矩阵版本 是:
df <- data.frame(zip = c("4760","5445", "2200","2200"), company = c("company x", "company y", "company z","company a"))
corpus <- data.frame(zip = c("4760","5445", "2200", "2200", "2200"), company = c("company x inc.", "company y inc.", "company z inc.", "company a inc.", "company b inc."),
id = c(12121212, 23232323, 34343434, 56565656, 67676767))
# large matrix version
library(dplyr)
dist_mat <- inner_join(corpus, df, by = "zip") %>%
mutate(corpus_co=tolower(as.character(company.x)), df_co=tolower(as.character(company.y)), company.x=NULL, company.y=NULL) %>%
group_by(zip) %>%
do( { dist_df=data.frame(unique(.$corpus_co),
stringdistmatrix(unique(.$corpus_co), unique(.$df_co), method=distance.method), stringsAsFactors=FALSE);
colnames(dist_df) = c("corpus_co", unique(.$df_co));
dist_df})
结果
zip corpus_co company z company a company x company y
(fctr) (chr) (dbl) (dbl) (dbl) (dbl)
1 2200 company z inc. 0.1190476 0.1798942 NA NA
2 2200 company a inc. 0.1798942 0.1190476 NA NA
3 2200 company b inc. 0.1798942 0.1798942 NA NA
4 4760 company x inc. NA NA 0.1190476 NA
5 5445 company y inc. NA NA NA 0.1190476
然而,在df
矩阵中有4000行,完整的字符串距离矩阵非常大,有许多NA。更高效的版本使用gather
包中的tidyr
函数生成key value
格式的结果。在这种方法中,一些变量形成唯一的密钥,然后具有相关的值。 tidyr
包的插图更详细地解释了这一点。在您的情况下,corpus
公司名称和df
公司名称构成key
,其名称之间的字符串距离为value
。这是针对每个邮政编码完成的,因此永远不会存储完整的字符串距离矩阵。您可能还会发现这更容易用于后续分析。代码与前一版本的不同之处仅在于最后一行。
library(tidyr)
dist_keyval <- inner_join(corpus, df, by = "zip") %>%
mutate(corpus_co=tolower(as.character(company.x)), df_co=tolower(as.character(company.y)), company.x=NULL, company.y=NULL) %>%
group_by(zip) %>%
do( { dist_df=data.frame(unique(.$corpus_co),
stringdistmatrix(unique(.$corpus_co), unique(.$df_co), method=distance.method), stringsAsFactors=FALSE);
colnames(dist_df) = c("corpus_co", unique(.$df_co));
gather(dist_df, key=df_co, value=str_dist, -corpus_co)})
给出结果
zip corpus_co df_co str_dist
(fctr) (chr) (chr) (dbl)
1 2200 company z inc. company z 0.1190476
2 2200 company a inc. company z 0.1798942
3 2200 company b inc. company z 0.1798942
4 2200 company z inc. company a 0.1798942
5 2200 company a inc. company a 0.1190476
6 2200 company b inc. company a 0.1798942
7 4760 company x inc. company x 0.1190476
8 5445 company y inc. company y 0.1190476
<强>被修改强>
找到与corpus_co
最小距离df_co
的代码是:
dist_min <- dist_keyval %>% group_by(zip, df_co) %>%
slice(which.min(str_dist))
要在最终结果中添加列,您可以加入用于进行字符串距离计算的公司名称表格(即小写名称),如下所示:
final_result <- corpus %>% mutate(lower_co = tolower(as.character(company))) %>%
right_join(dist_min, by = c("zip", "lower_co" = "corpus_co") ) %>%
select(c(df_co, company, id), everything(), -lower_co)
给出了
df_co company id zip str_dist
1 company a company a inc. 56565656 2200 0.1190476
2 company z company z inc. 34343434 2200 0.1190476
3 company x company x inc. 12121212 4760 0.1190476
4 company y company y inc. 23232323 5445 0.1190476
最后一个select
显示了如何将列重新排列为特定顺序。
答案 1 :(得分:1)
我有一些想法。如果你不需要你的距离矩阵,你可以像这样解决它。我使用dplyr因为我知道那个更好。您可以将代码拆分为多个而不是一个dplyr命令。或者使用data.table。这甚至可能更快。
采取的步骤:
这些步骤避免使用首先创建矩阵,然后寻找最小值或将其他值放入NA。
library(stringdist)
library(dplyr)
df <- data.frame(zip = c("4760","5445", "2200"), company = c("company x", "company y", "company z"))
corpus <- data.frame(zip = c("4760","5445", "2200", "2200", "2200"), company = c("company x inc.", "company y inc.", "company z inc.", "company a inc.", "company b inc."), id = c(12121212, 23232323, 34343434, 56565656, 67676767))
distance.method <- c("jw")
combined_min_distance <- inner_join(df, corpus, by = "zip" ) %>%
mutate(distance = stringdist(tolower(combined$company.x),
tolower(combined$company.y),
method = distance.method,
nthread = getOption("sd_num_thread"))) %>%
group_by(company.x) %>%
filter(distance == min(distance))
combined_min_distance
zip company.x company.y id distance
(fctr) (fctr) (fctr) (dbl) (dbl)
1 2200 company z company z inc. 34343434 0.1190476
2 4760 company x company x inc. 12121212 0.1190476
3 5445 company y company y inc. 23232323 0.1190476
答案 2 :(得分:1)
您可以使用stringdist::amatch
并避免计算完整的stringdist矩阵。
df <- data.frame(zip = c("4760","5445", "2200"), company = c("company x", "company y", "company z"))
corpus <- data.frame(zip = c("4760","5445", "2200", "2200", "2200"), company = c("company x inc.", "company y inc.", "company z inc.", "company a inc.", "company b inc."), id = c(12121212, 23232323, 34343434, 56565656, 67676767))
i <- stringdist::amatch(df$company,corpus$company,maxDist=5)
merged <- data.frame(df$company,corpus$company[i])
merged
> merged
df.company corpus.company.i.
1 company x company x inc.
2 company y company y inc.
3 company z company z inc.
之前做一些字符串清理会更好,所以你知道距离只是由实际的拼写错误造成的(请注意较低的maxDist
)。
lookup <- gsub(" inc.$","",corpus$company)
i2 <- stringdist::amatch(df$company,lookup,maxDist=2)
merged2 <- data.frame(df$company,corpus$company[i2])