Advertisement
sylviapsh

Minimum Edit Distance With Weigths

Jun 21st, 2013
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.83 KB | None | 0 0
  1. namespace MinimumEditDistance
  2. {
  3.     using System;
  4.  
  5.     /// <summary>
  6.     /// Write a program to calculate the "Minimum Edit Distance" (MED) between two words. MED(x, y) is the minimal sum of costs of edit operations used to transform x to y. Sample costs are given below:
  7.     /// cost (replace a letter) = 1
  8.     /// cost (delete a letter) = 0.9
  9.     /// cost (insert a letter) = 0.8
  10.     /// Example: x = "developer", y = "enveloped" -> cost = 2.7
  11.     /// delete ‘d’:  "developer" -> "eveloper", cost = 0.9
  12.     /// insert ‘n’:  "eveloper" -> "enveloper", cost = 0.8
  13.     /// replace ‘r’ -> ‘d’:  "enveloper" -> "enveloped", cost = 1
  14.     ///
  15.     /// Link with explanation: http://www.let.rug.nl/kleiweg/lev/
  16.     /// </summary>
  17.     public class MinimumEditDistance
  18.     {
  19.         private const decimal CostDelete = 0.9M;
  20.         private const decimal CostInsert = 0.8M;
  21.         private const decimal CostReplace = 1M;
  22.         private static decimal[,] table;
  23.  
  24.         public static void Main()
  25.         {
  26.             var result1 = Compute("developer", "enveloped");
  27.             Console.WriteLine("Words: developer -> enveloped");
  28.             Console.WriteLine("Distance = {0}", result1);
  29.             PrintCostsTable();
  30.             Console.WriteLine();
  31.  
  32.             var result2 = Compute("developer", "eveloper");
  33.             Console.WriteLine("Words: developer -> eveloper");
  34.             Console.WriteLine("Distance = {0}", result2);
  35.             PrintCostsTable();
  36.             Console.WriteLine();
  37.  
  38.             var result3 = Compute("eveloper", "enveloper");
  39.             Console.WriteLine("Words: eveloper -> enveloper");
  40.             Console.WriteLine("Distance = {0}", result3);
  41.             PrintCostsTable();
  42.             Console.WriteLine();
  43.  
  44.             var result4 = Compute("eveloper", "");
  45.             Console.WriteLine("Words: eveloper ->  ");
  46.             Console.WriteLine("Distance = {0}", result4);
  47.             PrintCostsTable();
  48.             Console.WriteLine();
  49.  
  50.             var result5 = Compute("", "eveloper");
  51.             Console.WriteLine("Words:  -> enveloper");
  52.             Console.WriteLine("Distance = {0}", result5);
  53.             PrintCostsTable();
  54.             Console.WriteLine();
  55.  
  56.             var result6 = Compute("", "");
  57.             Console.WriteLine("Words:  -> ");
  58.             Console.WriteLine("Distance = {0}", result6);
  59.             PrintCostsTable();
  60.         }
  61.  
  62.         /// <summary>
  63.         /// Compute the distance between two words.
  64.         /// </summary>
  65.         public static decimal Compute(string word1, string word2)
  66.         {
  67.             int n = word1.Length;
  68.             int m = word2.Length;
  69.             table = new decimal[n + 1, m + 1];
  70.  
  71.             // Step 1: Fill cost of deletions
  72.             for (int row = 0; row <= n; row++)
  73.             {
  74.                 table[row, 0] = row * CostDelete;
  75.             }
  76.  
  77.             // Step 2: Fill cost of insertions
  78.             for (int col = 0; col <= m; col++)
  79.             {
  80.                 table[0, col] = col * CostInsert;
  81.             }
  82.  
  83.             // Step 3: Calculate and choose min cost operation
  84.             // ----------------------------------
  85.             // |diag Cell  | above Cell           |
  86.             // -----------------------------------
  87.             // |           | min (above + delete, |
  88.             // |left Cell  |      diag + replace, |
  89.             // |           |      left + insert)  |
  90.             // ----------------------------------
  91.             for (int row = 1; row <= n; row++)
  92.             {
  93.                 // Step 4
  94.                 for (int col = 1; col <= m; col++)
  95.                 {
  96.                     // Step 5: Calculate the cost of replacing. If the letters are equal it is 0, otherwise its the replace operation cost
  97.                     decimal cost = (word2[col - 1] == word1[row - 1]) ? 0 : CostReplace;
  98.  
  99.                     // Step 6: Find the minimal cost operation of the 3 possible
  100.                     decimal delete = table[row - 1, col] + CostDelete;
  101.                     decimal replace = table[row - 1, col - 1] + cost;
  102.                     decimal insert = table[row, col - 1] + CostInsert;
  103.  
  104.                     table[row, col] = Math.Min(
  105.                         Math.Min(delete, insert),
  106.                         replace);
  107.                 }
  108.             }
  109.  
  110.             // Step 7: Take and return the result (most down-right cell)
  111.             return table[n, m];
  112.         }
  113.  
  114.         public static void PrintCostsTable()
  115.         {
  116.             Console.WriteLine("Costs table");
  117.             for (int i = 0; i < table.GetLength(0); i++)
  118.             {
  119.                 for (int j = 0; j < table.GetLength(1); j++)
  120.                 {
  121.                     Console.Write("{0, 4} ", table[i, j]);
  122.                 }
  123.  
  124.                 Console.WriteLine();
  125.             }
  126.         }
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement