2013-02-01 56 views
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; 
     } 
    } 
+3

所以你得到什麼錯誤? – Walter

+0

**。不同的(新DistinctByNameNearComparer ())**我認爲這不是如何實現從IEqualityComparer繼承的定義類的方式,因爲我有一個錯誤..我只是不能讓它工作,這就是爲什麼我打這裏.. :) –

回答

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)推斷出Distinct的參數應該是IEqualityComparer<Item>,並且您需要指定它。但是你DistinctByNameNearComparer被聲明爲

class DistinctByNameNearComparer : IEqualityComparer<Item> 

也就是說,它確實是IEqualityComparer<Item>。你唯一需要做的就是寫

.Distinct(new DistinctByNameNearComparer()) 
+0

它編譯但返回的結果,這不是我所期望的Maggi,Jjamppong v2,Maggi v2,Jjamppong,Jjamppong v3,Jjamppong v4,Quick Chow –

+0

它只是按降序排列項目,但沒有做不同的部分 –

+0

@VincentDagpin對於需要關注的項目而言'GetHashCode'方法應該爲等於項目返回一個值,而您的實現不會。 – horgh