模糊字符串比较(检查速记匹配)C#

时间:2018-06-03 07:34:33

标签: c# string fuzzy-comparison

我有两个字符串列表,我想从每个列表中提取索引,如果当前索引处的字符串在第二个列表中(反之亦然),字符串cant完全匹配或者可以是另一个列表的简写例如,考虑这两个列表

List<string> aList = new List<string> { "Id", "PartCode", "PartName", "EquipType" };
List<string> bList = new List<string> { "PartCode", "PartName", "PartShortName", "EquipmentType" };

在上面的例子中,我希望从aList索引:1,2,3

bList索引0,1,3

来自aList的索引1,2显然是完全匹配的字符串,但有趣的部分是“EquipType”“EquipmentType” 匹配 becuse “EquipType”“EquipmentType”的缩写

但是“PartName” 不是“PartShortName”的简写,因此不需要索引

这些是我的代码

List<string> aList = new List<string> { "Id", "PartCode", "PartName", "EquipType" };// 1, 2 , 3
List<string> bList = new List<string> { "PartCode", "PartName", "PartShortName", "EquipmentType" };//0, 1 ,3 

List<int> alistIndex = new List<int>();
List<int> blistIndex = new List<int>();

for (int i = 0; i < aList.Count; i++)           
{
    string a = aList[i];
    for (int j = 0; j < bList.Count(); j++)               
    {
        string b = bList[j];

        string bigger, smaller;
        int biggerCount, smallerCount;
        if (a.Length > b.Length)
        {
            bigger = a; smaller = b;
            biggerCount = a.Length ; smallerCount = b.Length ;    
        }
        else
        {
            bigger = b; smaller = a;
            biggerCount = b.Length; smallerCount = a.Length ;
        }

        int countCheck = 0;
        for (int k = 0; k < biggerCount; k++)
        {
            if (smaller.Length != countCheck)
            {
                if (bigger[k] == smaller[countCheck])
                    countCheck++;
             }
         }

        if (countCheck == smaller.Length)
        {
            alistIndex.Add(i);
            blistIndex.Add(j);
            res = true;
            break;
        }
        else
            res = false;  
    }
}

alistIndex.ForEach(i => Console.Write(i));
Console.WriteLine(Environment.NewLine);
blistIndex.ForEach(i => Console.Write(i));
Console.ReadKey();

上面的代码工作得很好,看起来非常类似于solution

但是如果像这样更改第二个列表的顺序

 List<string> bList = new List<string> { "PartCode", "PartShortName", "PartName", "EquipmentType" };

我将得到索引0,1和3(但我想要0 2和3)

我应该检查每对的距离并返回最低?或者我应该以不同的方法工作

由于

P.S 我还找到了this GitHub,但我不知道它是否会为我做这个伎俩

1 个答案:

答案 0 :(得分:2)

我觉得你想要做的是一个坏主意... Id Idiotic 的缩写,只是举个例子:-)仍然...我想在Unicode上做一些实验。

现在,此代码将在大写字母上拆分单词。 PartNamePart + Name,因为N为大写。它不支持IDIdentifier(因为它应该是IDentifier)但它确实支持NSANotSuchAgency :-)所以完整的首字母缩略词是可以的虽然FDA不等同于FoodAndDrugAdministration,但连词的首字母缩略词是KO。

public static bool ShorthandCompare(string str1, string str2)
{
    if (str1 == null)
    {
        throw new ArgumentNullException(nameof(str1));
    }

    if (str2 == null)
    {
        throw new ArgumentNullException(nameof(str2));
    }

    if (str1 == string.Empty)
    {
        return str2 == string.Empty;
    }

    if (object.ReferenceEquals(str1, str2))
    {
        return true;
    }

    var ee1 = StringInfo.GetTextElementEnumerator(str1);
    var ee2 = StringInfo.GetTextElementEnumerator(str2);

    bool eos1, eos2 = true;

    while ((eos1 = ee1.MoveNext()) && (eos2 = ee2.MoveNext()))
    {
        string ch1 = ee1.GetTextElement(), ch2 = ee2.GetTextElement();

        // The string.Compare does some nifty tricks with unicode
        // like string.Compare("ì", "i\u0300") == 0
        if (string.Compare(ch1, ch2) == 0)
        {
            continue;
        }

        UnicodeCategory uc1 = char.GetUnicodeCategory(ch1, 0);
        UnicodeCategory uc2 = char.GetUnicodeCategory(ch2, 0);

        if (uc1 == UnicodeCategory.UppercaseLetter)
        {
            while (uc2 != UnicodeCategory.UppercaseLetter && (eos2 = ee2.MoveNext()))
            {
                ch2 = ee2.GetTextElement();
                uc2 = char.GetUnicodeCategory(ch2, 0);
            }

            if (!eos2 || string.Compare(ch1, ch2) != 0)
            {
                return false;
            }

            continue;
        }
        else if (uc2 == UnicodeCategory.UppercaseLetter)
        {
            while (uc1 != UnicodeCategory.UppercaseLetter && (eos1 = ee1.MoveNext()))
            {
                ch1 = ee1.GetTextElement();
                uc1 = char.GetUnicodeCategory(ch1, 0);
            }

            if (!eos1 || string.Compare(ch1, ch2) != 0)
            {
                return false;
            }

            continue;
        }

        // We already know they are different!
        return false;
    }

    if (eos1)
    {
        while (ee1.MoveNext())
        {
            string ch1 = ee1.GetTextElement();
            UnicodeCategory uc1 = char.GetUnicodeCategory(ch1, 0);

            if (uc1 == UnicodeCategory.UppercaseLetter)
            {
                return false;
            }
        }
    }
    else if (eos2)
    {
        while (ee2.MoveNext())
        {
            string ch2 = ee2.GetTextElement();
            UnicodeCategory uc2 = char.GetUnicodeCategory(ch2, 0);

            if (uc2 == UnicodeCategory.UppercaseLetter)
            {
                return false;
            }
        }
    }

    return true;
}

然后

List<string> aList = new List<string> { "Id", "PartCode", "PartName", "EquipType" };
List<string> bList = new List<string> { "PartCode", "PartName", "PartShortName", "EquipmentType" };

List<List<int>> matches = new List<List<int>>();

for (int i = 0; i < aList.Count; i++)
{
    var lst = new List<int>();
    matches.Add(lst);

    for (int j = 0; j < bList.Count; j++)
    {
        if (ShorthandCompare(aList[i], bList[j]))
        {
            lst.Add(j);
        }
    }
}

请注意,结果是List<List<int>>,因为您可以为aList的单个词创建多个匹配项!

现在...... ShorthandCompare的有趣部分是它试图“智能”并处理非BMP Unicode字符(通过使用StringInfo.GetTextElementEnumerator)并处理分解的Unicode字符( ì字符可以通过i + \u0300以Unicode形式获得,即它的死亡率。它通过使用string.Compare来实现它,与string.Equals不同,它是支持Unicode的(string.CompareOrdinal更类似于string.Equals而不支持Unicode)。

bool cmp1 = ShorthandCompare("IdìoLe\u0300ss", "Idi\u0300oticLèsser"); // true