-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNaiveLevenshteinDistance.cs
More file actions
57 lines (48 loc) · 2.02 KB
/
NaiveLevenshteinDistance.cs
File metadata and controls
57 lines (48 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System;
namespace NaiveLevenshteinDistance
{
/// <summary>
/// A naive implementation that calculates the Levenshtein Distance between two strings.
/// Note: Inefficient because it repeatedly compares the same substrings.
/// </summary>
public class NaiveLevenshteinDistanceCalculator
{
public int Distance(string str1, string str2)
{
return DistanceHelper(str1, str1.Length, str2, str2.Length);
}
private static int DistanceHelper(string str1, int length1, string str2, int length2)
{
Console.WriteLine(String.Format("Comparing: {0} - {1}\n",
(length1 > 0) ? str1[length1 - 1] : '\0',
(length2 > 0) ? str2[length2 - 1] : '\0'));
// Check base case
if (length1 == 0 || length2 == 0)
return 0;
// Calculate distance of last character
var cost = (str1[length1 - 1] == str2[length2 - 1]) ? 0 : 1;
return Math.Min(
Math.Min((DistanceHelper(str1, length1 - 1, str2, length2) + 1), // Delete last char from str1
(DistanceHelper(str1, length1, str2, length2 - 1) + 1)), // Delete last char from str2
(DistanceHelper(str1, length1 - 1, str2, length2 - 1) + cost)); // Delete last char from both
}
}
public static class Program
{
public static void Main()
{
var calculator = new NaiveLevenshteinDistanceCalculator();
var a = "go";
var b = "to";
var abDist = calculator.Distance(a, b);
Console.WriteLine("Distance between {0} and {1} is: {2}", a, b, abDist);
var c = "kitten";
var d = "sitting";
var cdDist = calculator.Distance(c, d);
Console.WriteLine("Distance between {0} and {1} is: {2}", c, d, cdDist);
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("[Press enter to exit]");
Console.ReadLine();
}
}
}