使用Levenstein算法结果对字符串数组进行排序

时间:2009-06-30 19:41:33

标签: c# search sorting

我一直在使用C#中的Access文件编辑器,我一直在尝试将一个搜索功能添加到我的程序中。到目前为止,我有数据库文件填充一个2D数组,然后我用它来填充另一个窗口中的ListView框。在这个新窗口中,我希望能够按型号搜索每个条目。到目前为止,我已经成功地采用了Levenstein算法,这似乎有很多用处。我可以让算法在每个条目和搜索键盘之间分配距离值,并将该值分配给另一个整数数组。我也可以按顺序对结果进行排序。

然而,我目前的问题是,我希望将模型编号与Levenstein算法的距离值进行相同的排序,以便最相关的结果成为ListView框中的第一个选择。任何人的想法??!?!

这是我到目前为止所得到的:

 private void OnSearch(object sender, System.EventArgs e)
    {

        string a;
        string b;
        int[] result = new int[1000];
        int[] sorted = new int[1000];

            for (int i = 0; i < rowC; i++)
            {
                a = PartNum[i];         // Array to search
                b = SearchBox1.Text;        // keyword to search with

                if (GetDistance(a, b) == 0)
                {
                    return;
                }

                result[i] = GetDistance(a, b);  //add each distance result into array 

            }

            int index;
            int x;

            for (int j = 1; j < rowC; j++)      //quick insertion sort
            {
                index = result[j];
                x = j;

                while ((x > 0) && (result[x - 1] > index))
                {
                    result[x] = result[x - 1];
                    x = x - 1;
                }
                result[x] = index;
            }

        }


  public static int GetDistance(string s, string t)
    {
        if (String.IsNullOrEmpty(s) || String.IsNullOrEmpty(t))
        {
            MessageBox.Show("Please enter something to search!!");
            return 0;

        }

        int n = s.Length;
        int m = t.Length;
        if (n == 0)
        {
            return m;
        }

        else if (m == 0)
        {
            return n;
        }

        int[] p = new int[n + 1];
        int[] d = new int[n + 1];
        int[] _d;
        char t_j;
        int cost;

        for (int i = 0; i <= n; i++)
        {
            p[i] = i;
        }

        for (int j = 1; j <= m; j++)
        {
            t_j = t[j - 1];
            d[0] = j;

            for (int i = 1; i <= n; i++)
            {
                cost = (s[i - 1] == t_j) ? 0 : 1;
                d[i] = Math.Min(Math.Min(d[i - 1] + 1, p[i] + 1), p[i - 1] + cost);
            }
            _d = p;
            p = d;
            d = _d;
        }
        return p[n];
    }

2 个答案:

答案 0 :(得分:0)

你有LINQ可用吗?如果是这样的话:

var ordered = PartNum.OrderBy(x => GetDistance(x, SearchBox1.Text))
                     .ToList();

// Do whatever with the ordered list

请注意,如果您发现完全匹配,并且没有使实际距离可用,则这样做的缺点是不会提前中止 - 但是,无论如何您都不清楚如何使用结果......

另一种选择是:

var ordered = (from word in PartNum
              let distance = GetDistance(word, SearchBox1.Text))
              orderby distance
              select new { word, distance }).ToList();

然后你也有距离。

答案 1 :(得分:0)

为了按Levenstein距离对数组进行排序,您需要将模型编号作为数组的一部分包含在内,这样,当您按照Levenstein数字对数组进行排序时,模型编号将随之进行。

为此,请创建一个表示每个部分的类:

public class Part
{
    public string PartNumber;
    public int LevensteinDistance;
}

然后创建一个Part:

数组
Part[] parts;

然后您可以像这样引用每个元素:

parts[n].LevensteinDistance
parts[n].PartNumber