Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace StringMatching
- {
- public class Levenshtein
- {
- public static double LevenshteinDistance(string s, string t)
- {
- int m = s.Length;
- int n = t.Length;
- bool do_djodjo = false; // case1 'dj'
- bool do_dot = false; //case2 '.'
- double[,] d = new double[m + 1, n + 1];
- for (int i = 0; i < m + 1; i++)
- for (int j = 0; j < n + 1; j++)
- d[i, j] = 0;
- for (int i = 1; i <= m; i++)
- {
- d[i, 0] = i;
- }
- for (int j = 1; j < n + 1; j++)
- {
- d[0, j] = j;
- }
- for (int j = 1; j <= n; j++)
- {
- for (int i = 1; i <= m; i++)
- {
- /***********SPECIJALNI SLUCAJ*****************/
- if (do_djodjo && s[i - 1] == 'j' && s[i - 2] == 'd')
- {
- d[i, j] = d[i - 1, j - 1] - 1;
- }
- /***********JEDNAKI SU*****************/
- else if (
- (s[i - 1] == t[j - 1])
- || (s[i - 1] == 'c' && t[j - 1] == 'ć')
- || (s[i - 1] == 's' && t[j - 1] == 'š')
- || (s[i - 1] == 'c' && t[j - 1] == 'č')
- || (s[i - 1] == 'z' && t[j - 1] == 'ž'))
- {
- d[i, j] = d[i - 1, j - 1];
- if ( (i - 1 == 0) && (j - 1 == 0) ) //equal at 0 position
- do_dot = true;
- }
- /**************NISU JEDNAKI**********************/
- else
- {
- if (s[i - 1] == 'd' && t[j - 1] == 'đ')
- do_djodjo = true;
- if ((do_dot && s[i - 1] == ' ' && s[i - 2] == '.'))
- {
- for (int target_counter = j; target_counter < t.Length; target_counter++)
- {
- if (t[target_counter] == ' ')
- {
- j = target_counter; //take new target position(for example pos 69)
- d[i, j] = 0;
- break;
- }
- }
- do_dot = false; //resume normal computation(normal fuck)
- }
- if (i-1==2 && do_dot && s[i - 1] !=' ' && s[i - 2] == '.')
- {
- for (int target_counter = j; target_counter < t.Length; target_counter++)
- {
- if (t[target_counter] == ' ')
- {
- i = i - 2;
- j = target_counter; //take new target position(for example pos 69)
- d[i, j] = 0;
- break;
- }
- }
- do_dot = false; //resume normal computation(normal fuck)
- }
- else{
- d[i, j] = Math.Min(Math.Min(d[i - 1, j] + 1,
- d[i, j - 1] + 1),
- d[i - 1, j - 1] + 1);
- }
- }
- }
- }
- return d[m, n];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement