2

我有这个集合,我想根据评分对其进行排序,然后使用 Levenshtein 算法根据最近的拼写距离删除重复的名称。

到目前为止,这是我的代码

我的预期结果是

 /* 
  * Expected result:
  * 
  * Jjamppong v2
  * Maggi
  * Quick Chow
  * 
  */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Item> _items = new List<Item>();

            _items.Add(new Item() { ItemID = 1, Name = "Jjamppong", Rating = 4 });
            _items.Add(new Item() { ItemID = 2, Name = "Jjamppong v2", Rating = 6 });
            _items.Add(new Item() { ItemID = 3, Name = "Jjamppong v3", Rating = 3 });
            _items.Add(new Item() { ItemID = 4, Name = "Jjamppong v4", Rating = 2 });
            _items.Add(new Item() { ItemID = 5, Name = "Maggi", Rating = 8 });
            _items.Add(new Item() { ItemID = 6, Name = "Quick Chow", Rating = 1 });
            _items.Add(new Item() { ItemID = 7, Name = "Maggi v2", Rating = 5 });


            _items.OrderByDescending(i => i.Rating)
                .Distinct(new DistinctByNameNearComparer<Item>())
                .Select(i => i)
                .ToList()
                .ForEach(i =>
                {
                    Console.WriteLine(i.Name);
                });

            Console.ReadKey();


            /* 
             * Expected result:
             * 
             * Jjamppong v2
             * Maggi
             * Quick Chow
             * 
             */
        }
    }

    class Item
    {
        public int ItemID { get; set; }
        public string Name { get; set; }
        public int Rating { get; set; }
    }

    class DistinctByNameNearComparer : IEqualityComparer<Item>
    {

        public bool Equals(Item x, Item y)
        {
            int _distance = LevenshteinDistance.Compute(x.Name, y.Name);
            int _maxLen = Math.Max(x.Name.Length, y.Name.Length);

            return (_distance > (_maxLen - 5));
        }

        public int GetHashCode(Item obj)
        {
            return obj.GetHashCode();
        }
    }

    class LevenshteinDistance
    {
        /// <summary>
        /// Compute the distance between two strings.
        /// </summary>
        public static int Compute(string s, string t)
        {
            int n = s.Length;
            int m = t.Length;
            int[,] d = new int[n + 1, m + 1];

            // Step 1
            if (n == 0)
            {
                return m;
            }

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

            // Step 2
            for (int i = 0; i <= n; d[i, 0] = i++)
            {
            }

            for (int j = 0; j <= m; d[0, j] = j++)
            {
            }

            // Step 3
            for (int i = 1; i <= n; i++)
            {
                //Step 4
                for (int j = 1; j <= m; j++)
                {
                    // Step 5
                    int cost = (t[j - 1] == s[i - 1]) ? 0 : 1;

                    // Step 6
                    d[i, j] = Math.Min(
                        Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1),
                        d[i - 1, j - 1] + cost);
                }
            }
            // Step 7
            return d[n, m];
        }
    }
}

我在这里遇到错误

_items.OrderByDescending(i => i.Rating)
                    .Distinct(new DistinctByNameNearComparer<Item>())
                    .Select(i => i)
                    .ToList()
                    .ForEach(i =>
                    {
                        Console.WriteLine(i.Name);
                    });

任何帮助,将不胜感激。

回答:

class DistinctByNameNearComparer : IEqualityComparer<Item>
    {

        public bool Equals(Item x, Item y)
        {
            int _distance = LevenshteinDistance.Compute(x.Name, y.Name);
            int _maxLen = Math.Max(x.Name.Length, y.Name.Length);
            bool _comp = _distance < 4;

            return _comp;
        }

        public int GetHashCode(Item obj)
        {
            return 1;
        }
    }
4

1 回答 1

1

我想你收到Compiler Error CS0308,说

非泛型类型或方法“标识符”不能与类型参数一起使用。

方法或类型不是通用的,但它与类型参数一起使用。为避免此错误,请删除尖括号和类型参数,或将方法或类型重新声明为泛型方法或类型。

所以正确的linq是:

_items.OrderByDescending(i => i.Rating)
    .Distinct(new DistinctByNameNearComparer())
    .Select(i => i)
    .ToList()
    .ForEach(i =>
    {
        Console.WriteLine(i.Name);
    });

当你指定

.Distinct(new DistinctByNameNearComparer<Item>())

错误是DistinctByNameNearComparer不是通用的,因此您不能为其指定类型参数。编译器从中推断出_items.OrderByDescending(i => i.Rating)参数 forDistinct应该是类型IEqualityComparer<Item>,并且您应该指定它。但是您DistinctByNameNearComparer的声明为

class DistinctByNameNearComparer : IEqualityComparer<Item>

也就是说,它确实是IEqualityComparer<Item>。你唯一需要做的就是写

.Distinct(new DistinctByNameNearComparer())
于 2013-02-01T01:41:21.143 回答