可以考虑使用 StringSimilarity.NET NuGet程序包,里面集成了多种字符串相似度的算法,根据你的需求写了一个示例程序,如下:
using F23.StringSimilarity;
// 模拟N个名字
var nameList = new List<string>
{
"Daniel Frumhoff",
"Timothy Urista",
"Fernandina Poltone",
"Rhys Maus",
"Lanie Hayle",
"Orelee Ebbitt",
"Fair Jiles",
"Codi Standering",
"Vyky Borley",
"Dierdre MacAscaidh",
"Ardelle Northage",
"Karie Riden"
};
// 模拟M个邮箱
var emailList = new List<string>
{
"frumhofd@newschool.edu",
"uriste@people.com.cn",
"fernandinna@slate.com",
"mausla@hibu.com",
"lan@bloglovin.com",
"stander@quantcast.com"
};
// MetricLCS算法实例
var d = new MetricLCS();
/*
* 存储每个邮箱与所有名字比对的结果的集合
* 结构类似如下:
* frumhofd@newschool.edu
* frumhofd==>>Daniel Frumhoff, 相似度:46.67%
* frumhofd==>>Rhys Maus, 相似度:22.22%
* frumhofd==>>Fair Jiles, 相似度:20.00%
* ...
* */
var results = new List<Result>();
// 循环所有邮箱
foreach (var email in emailList)
{
var r = new Result
{
Email = email
};
// 用邮箱的前缀循环比对每个名字,并计算出最短编辑距离
foreach (var name in nameList)
{
var item = new Item
{
Name = name,
Percentage = 1 - d.Distance(name.ToLower(), r.EmailName.ToLower())
};
r.Items.Add(item);
}
results.Add(r);
}
// 打印出每个邮箱对应的比对结果
foreach (var result in results)
{
Console.WriteLine($"邮箱{result.Email}的比对结果:");
foreach (var item in result.Items.OrderByDescending(x => x.Percentage))
{
Console.WriteLine($"{result.EmailName}==>>{item.Name}, 相似度:{item.Percentage:P}");
}
}
// 打印出相似度最高的结果
var final = results.Select(x => new
{
x.EmailName,
x.Email,
Result = x.Items.OrderByDescending(p => p.Percentage).First()
});
Console.WriteLine("最终比对的结果...");
foreach (var f in final)
{
Console.WriteLine($"邮箱:{f.Email},最匹配的名字:{f.Result.Name},匹配度:{f.Result.Percentage:P}");
}
Console.ReadKey();
public class Result
{
public Result()
{
Items = new List<Item>();
}
public string Email { get; set; }
public string EmailName => Email.Split('@')[0];
public List<Item> Items { get; set; }
}
public class Item
{
public string Name { get; set; }
public double Percentage { get; set; }
}
运行结果:
邮箱frumhofd@newschool.edu的比对结果:
frumhofd==>>Daniel Frumhoff, 相似度:46.67%
frumhofd==>>Rhys Maus, 相似度:22.22%
frumhofd==>>Fair Jiles, 相似度:20.00%
frumhofd==>>Karie Riden, 相似度:18.18%
frumhofd==>>Fernandina Poltone, 相似度:16.67%
frumhofd==>>Dierdre MacAscaidh, 相似度:16.67%
frumhofd==>>Timothy Urista, 相似度:14.29%
frumhofd==>>Codi Standering, 相似度:13.33%
frumhofd==>>Ardelle Northage, 相似度:12.50%
frumhofd==>>Lanie Hayle, 相似度:9.09%
frumhofd==>>Vyky Borley, 相似度:9.09%
frumhofd==>>Orelee Ebbitt, 相似度:7.69%
邮箱uriste@people.com.cn的比对结果:
uriste==>>Timothy Urista, 相似度:35.71%
uriste==>>Fair Jiles, 相似度:30.00%
uriste==>>Karie Riden, 相似度:27.27%
uriste==>>Codi Standering, 相似度:26.67%
uriste==>>Orelee Ebbitt, 相似度:23.08%
uriste==>>Fernandina Poltone, 相似度:22.22%
uriste==>>Rhys Maus, 相似度:22.22%
uriste==>>Ardelle Northage, 相似度:18.75%
uriste==>>Lanie Hayle, 相似度:18.18%
uriste==>>Vyky Borley, 相似度:18.18%
uriste==>>Daniel Frumhoff, 相似度:13.33%
uriste==>>Dierdre MacAscaidh, 相似度:11.11%
邮箱fernandinna@slate.com的比对结果:
fernandinna==>>Fernandina Poltone, 相似度:55.56%
fernandinna==>>Lanie Hayle, 相似度:36.36%
fernandinna==>>Karie Riden, 相似度:36.36%
fernandinna==>>Codi Standering, 相似度:33.33%
fernandinna==>>Fair Jiles, 相似度:27.27%
fernandinna==>>Ardelle Northage, 相似度:25.00%
fernandinna==>>Dierdre MacAscaidh, 相似度:22.22%
fernandinna==>>Timothy Urista, 相似度:21.43%
fernandinna==>>Daniel Frumhoff, 相似度:20.00%
fernandinna==>>Rhys Maus, 相似度:18.18%
fernandinna==>>Orelee Ebbitt, 相似度:15.38%
fernandinna==>>Vyky Borley, 相似度:9.09%
邮箱mausla@hibu.com的比对结果:
mausla==>>Rhys Maus, 相似度:44.44%
mausla==>>Timothy Urista, 相似度:28.57%
mausla==>>Dierdre MacAscaidh, 相似度:22.22%
mausla==>>Fair Jiles, 相似度:20.00%
mausla==>>Ardelle Northage, 相似度:18.75%
mausla==>>Lanie Hayle, 相似度:18.18%
mausla==>>Daniel Frumhoff, 相似度:13.33%
mausla==>>Codi Standering, 相似度:13.33%
mausla==>>Fernandina Poltone, 相似度:11.11%
mausla==>>Vyky Borley, 相似度:9.09%
mausla==>>Karie Riden, 相似度:9.09%
mausla==>>Orelee Ebbitt, 相似度:7.69%
邮箱lan@bloglovin.com的比对结果:
lan==>>Lanie Hayle, 相似度:27.27%
lan==>>Karie Riden, 相似度:18.18%
lan==>>Daniel Frumhoff, 相似度:13.33%
lan==>>Codi Standering, 相似度:13.33%
lan==>>Ardelle Northage, 相似度:12.50%
lan==>>Fernandina Poltone, 相似度:11.11%
lan==>>Rhys Maus, 相似度:11.11%
lan==>>Fair Jiles, 相似度:10.00%
lan==>>Vyky Borley, 相似度:9.09%
lan==>>Orelee Ebbitt, 相似度:7.69%
lan==>>Timothy Urista, 相似度:7.14%
lan==>>Dierdre MacAscaidh, 相似度:5.56%
邮箱stander@quantcast.com的比对结果:
stander==>>Codi Standering, 相似度:46.67%
stander==>>Lanie Hayle, 相似度:27.27%
stander==>>Karie Riden, 相似度:27.27%
stander==>>Daniel Frumhoff, 相似度:26.67%
stander==>>Ardelle Northage, 相似度:25.00%
stander==>>Fernandina Poltone, 相似度:22.22%
stander==>>Rhys Maus, 相似度:22.22%
stander==>>Timothy Urista, 相似度:21.43%
stander==>>Fair Jiles, 相似度:20.00%
stander==>>Dierdre MacAscaidh, 相似度:16.67%
stander==>>Vyky Borley, 相似度:9.09%
stander==>>Orelee Ebbitt, 相似度:7.69%
最终比对的结果...
邮箱:frumhofd@newschool.edu,最匹配的名字:Daniel Frumhoff,匹配度:46.67%
邮箱:uriste@people.com.cn,最匹配的名字:Timothy Urista,匹配度:35.71%
邮箱:fernandinna@slate.com,最匹配的名字:Fernandina Poltone,匹配度:55.56%
邮箱:mausla@hibu.com,最匹配的名字:Rhys Maus,匹配度:44.44%
邮箱:lan@bloglovin.com,最匹配的名字:Lanie Hayle,匹配度:27.27%
邮箱:stander@quantcast.com,最匹配的名字:Codi Standering,匹配度:46.67%