LINQ搜索类似的字符串(名称)。

4

例如名字:

  • 阿卜杜拉(abdulah)
  • 阿卜杜拉(abdullah)
  • 阿卜友拉(abdola)
  • 阿卜杜拉(abdollah)
  • 萨阿卜杜拉(S abdullah)
  • 阿卜杜勒(abdul)
  • 阿阿卜杜拉(aabdullah)

现在,我将在C#中使用Linq查询,字符串例如:textString = "abdolah"。预期结果是返回所有这些名字。

var data = db.TableList.where(a=>a.Name.Contains(textString).ToList();

那么问题是如何比较名称,或者是否有内置库可用于在 .Net 中进行比较名字。

这取决于你如何定义“相似”... - Maciej Los
https://nugetmusthaves.com/Tag/fuzzy - aybe
在C#中,如果我计算字符串的大小或使用某些技巧,这样做可以吗? - SAR
不确定它是否适用于所有类型的单词。我已经尝试了jon和john,效果还不错,但是对于“abdul”和“aabdullah”不确定。值得一试。 - Allen King
1
模糊匹配在文化差异较大时不会完美。你有名字,名字有已知的变体,而且这与文化有关。西班牙语中的Juan比中文的Jwan更接近Juancho。我将从已知的变体列表中构建简单的树,针对常见文化进行匹配,并基于Levenshtein算法来确定匹配的深度。 - Drag and Drop
显示剩余7条评论
1个回答

2
如我上面所建议的,最快入门的方法是尝试我提到的库之一。
这里是一个使用 https://github.com/JakeBayer/FuzzySharp 编写的程序,只花了2分钟: 结果:

enter image description here

代码:
using System;
using System.IO;
using FuzzySharp;
using FuzzySharp.PreProcess;

namespace zzzzzzzz
{
    internal static class Program
    {
        private static void Main(string[] args)
        {
            var referenceName = "Abdallah";
            var referenceGoal = 80;

            var names = @"
abdulah
abdullah
abdola
abdollah
S abdullah
abdul
aabdullah";

            using (var reader = new StringReader(names))
            {
                while (true)
                {
                    var line = reader.ReadLine();

                    if (line == null)
                        break;

                    var ratio = Fuzz.Ratio(referenceName, line, PreprocessMode.Full);
                    var success = ratio >= referenceGoal;

                    Console.Write($"Current = '{line}', Ratio = {ratio}, Result = ");

                    var color = Console.ForegroundColor;
                    Console.ForegroundColor = success ? ConsoleColor.Green : ConsoleColor.Red;
                    Console.Write($"{(success ? "PASS" : "FAIL")}{Environment.NewLine}");
                    Console.ForegroundColor = color;
                }
            }
        }
    }
}

这段话的翻译如下:
NuGet软件库中的包:https://www.nuget.org/packages/FuzzySharp/2.0.2 编辑:
我在这里尝试了你的例子,结果得到了100%(显然)。

enter image description here


我已经尝试了这个链接中的内容,您有什么建议吗? - SAR
你一定要尝试使用我建议的库,编写自己的库或基于代码片段进行开发很可能无法正常工作。如果你不能使用库,那么你可以学习它们是如何实现的! - aybe
我的结果和你的不一样,我也使用了模糊匹配。 例如(var ratio = Fuzz.Ratio("Abdallah","Abdallah") = 57),但是你的是71。 - SAR
看看我的编辑,这里完全没问题。 - aybe

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接