Advertisement
nex036ara

Levenshtein

Oct 26th, 2013
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.04 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace StringMatching
  7. {
  8.     public class Levenshtein
  9.     {
  10.         public static double LevenshteinDistance(string s, string t)
  11.         {
  12.             int m = s.Length;
  13.             int n = t.Length;
  14.             bool do_djodjo = false; // case1 'dj'
  15.             bool do_dot = false; //case2 '.'
  16.  
  17.             double[,] d = new double[m + 1, n + 1];
  18.            
  19.             for (int i = 0; i < m + 1; i++)
  20.                 for (int j = 0; j < n + 1; j++)
  21.                     d[i, j] = 0;
  22.            
  23.             for (int i = 1; i <= m; i++)
  24.             {
  25.                 d[i, 0] = i;
  26.             }
  27.  
  28.             for (int j = 1; j < n + 1; j++)
  29.             {
  30.                 d[0, j] = j;
  31.             }
  32.  
  33.  
  34.  
  35.  
  36.  
  37.             for (int j = 1; j <= n; j++)
  38.             {
  39.                 for (int i = 1; i <= m; i++)
  40.                 {
  41.  
  42.  
  43.         /***********SPECIJALNI SLUCAJ*****************/
  44.    
  45.                 if (do_djodjo && s[i - 1] == 'j' && s[i - 2] == 'd')
  46.                     {
  47.                         d[i, j] = d[i - 1, j - 1] - 1;
  48.                     }
  49.  
  50.         /***********JEDNAKI SU*****************/
  51.                 else if (
  52.                          (s[i - 1] == t[j - 1])
  53.                         || (s[i - 1] == 'c' && t[j - 1] == 'ć')
  54.                         || (s[i - 1] == 's' && t[j - 1] == 'š')
  55.                         || (s[i - 1] == 'c' && t[j - 1] == 'č')
  56.                         || (s[i - 1] == 'z' && t[j - 1] == 'ž'))
  57.                 {
  58.                      
  59.                         d[i, j] = d[i - 1, j - 1];
  60.  
  61.                         if ( (i - 1 == 0) && (j - 1 == 0) ) //equal at 0 position
  62.                                 do_dot = true;
  63.                    
  64.                  
  65.                   }
  66.  
  67.  
  68.         /**************NISU JEDNAKI**********************/    
  69.  
  70.                   else
  71.                   {
  72.                       if (s[i - 1] == 'd' && t[j - 1] == 'đ')
  73.                              do_djodjo = true;
  74.  
  75.                    
  76.  
  77.  
  78.                       if ((do_dot && s[i - 1] == ' ' && s[i - 2] == '.'))
  79.                       {
  80.  
  81.                           for (int target_counter = j; target_counter < t.Length; target_counter++)
  82.                           {
  83.  
  84.                               if (t[target_counter] == ' ')
  85.                               {
  86.                                   j = target_counter; //take new target position(for example pos 69)
  87.                                  
  88.                                   d[i, j] = 0;
  89.                                   break;
  90.                               }
  91.  
  92.                           }
  93.                           do_dot = false; //resume normal computation(normal fuck)
  94.  
  95.                       }
  96.  
  97.  
  98.                       if (i-1==2 && do_dot && s[i - 1] !=' ' && s[i - 2] == '.')
  99.                       {
  100.  
  101.                           for (int target_counter = j; target_counter < t.Length; target_counter++)
  102.                           {
  103.  
  104.                               if (t[target_counter] == ' ')
  105.                               {
  106.                                   i = i - 2;
  107.                                   j = target_counter; //take new target position(for example pos 69)
  108.                                  
  109.                                   d[i, j] = 0;
  110.                                   break;
  111.                               }
  112.  
  113.                           }
  114.                           do_dot = false; //resume normal computation(normal fuck)
  115.  
  116.                       }
  117.  
  118.                      
  119.                      
  120.                       else{
  121.                           d[i, j] = Math.Min(Math.Min(d[i - 1, j] + 1,
  122.                                               d[i, j - 1] + 1),
  123.                                               d[i - 1, j - 1] + 1);
  124.                        
  125.                             }
  126.  
  127.  
  128.  
  129.                   }
  130.                  
  131.  
  132.  
  133.                 }
  134.             }
  135.             return d[m, n];
  136.         }
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement