如何衡量字符串之间的相似性? [英] How to measure similarity between strings?

查看:75
本文介绍了如何衡量字符串之间的相似性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆名字,我想获得唯一的名字.但是,由于拼写错误和数据不一致,名称可能会写错.我正在寻找一种方法来检入字符串向量,如果其中两个是相似的话.

I have a bunch of names, and I want to obtain the unique names. However, due to spelling errors and inconsistencies in the data the names might be written down wrong. I am looking for a way to check in a vector of strings if two of them are similair.

例如:

pres <- c(" Obama, B.","Bush, G.W.","Obama, B.H.","Clinton, W.J.")

我想发现" Obama, B.""Obama, B.H."非常相似.有办法吗?

I want to find that " Obama, B." and "Obama, B.H." are very similar. Is there a way to do this?

推荐答案

例如,可以基于Levenshtein距离来完成.在不同的程序包中有多种实现.在以下问题的答案中可以找到一些解决方案和软件包:

This can be done based on eg the Levenshtein distance. There are multiple implementations of this in different packages. Some solutions and packages can be found in the answers of these questions:

  • agrep: only return best match(es)
  • In R, how do I replace a string that contains a certain pattern with another string?
  • Fast Levenshtein distance in R?

但是大多数情况下agrep会做您想要的事情:

But most often agrep will do what you want :

> sapply(pres,agrep,pres)
$` Obama, B.`
[1] 1 3

$`Bush, G.W.`
[1] 2

$`Obama, B.H.`
[1] 1 3

$`Clinton, W.J.`
[1] 4

这篇关于如何衡量字符串之间的相似性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆