zzyyjj1 2022-05-28 17:51 采纳率: 75%
浏览 32
已结题

多个英文人名和多个邮箱前缀如何模糊匹配(算法)

问题遇到的现象和发生背景

有个10个英文人名(每次不同),
比如Daniel Frumhoff、Timothy Urista、每个名字2-3个单词,找到多个邮箱比如 frumhofd@newschool.edu,需要邮箱前缀和人名做匹配,判断出是哪个人的邮箱,不要求100%准备,正确率比较高的运算又相对比较简单算法是什么样的呢?

操作环境、软件版本等信息

C# 需要最简单的思路和算法

尝试过的解决方法

人工匹配

我想要达到的结果

把每个邮箱都匹配出最适合的姓名,比如frumhofd@newschool.edu 和Daniel Frumhoff最匹配

  • 写回答

2条回答 默认 最新

  • 码老头 2022-05-28 21:22
    关注

    可以考虑使用 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%
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 6月6日
  • 已采纳回答 5月29日
  • 创建了问题 5月28日

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?