|
| 1 | +/* |
| 2 | + * The MIT License |
| 3 | + * |
| 4 | + * Copyright 2016 feature[23] |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +using System; |
| 26 | +using F23.StringSimilarity.Interfaces; |
| 27 | + |
| 28 | +namespace F23.StringSimilarity |
| 29 | +{ |
| 30 | + public sealed class OptimalStringAlignment : IStringDistance |
| 31 | + { |
| 32 | + /// <summary> |
| 33 | + /// Compute the distance between strings: the minimum number of operations |
| 34 | + /// needed to transform one string into the other (insertion, deletion, |
| 35 | + /// substitution of a single character, or a transposition of two adjacent |
| 36 | + /// characters) while no substring is edited more than once. |
| 37 | + /// </summary> |
| 38 | + /// <param name="s1">the first input string</param> |
| 39 | + /// <param name="s2">the second input string</param> |
| 40 | + /// <returns>the OSA distance</returns> |
| 41 | + public double Distance(string s1, string s2) |
| 42 | + { |
| 43 | + int n = s1.Length, m = s2.Length; |
| 44 | + if (n == 0) return m; |
| 45 | + if (m == 0) return n; |
| 46 | + |
| 47 | + // Create the distance matrix H[0 .. s1.length+1][0 .. s2.length+1] |
| 48 | + int[,] d = new int[s1.Length + 2, s2.Length + 2]; |
| 49 | + |
| 50 | + //initialize top row and leftmost column |
| 51 | + for (int i = 0; i <= n; i++) |
| 52 | + { |
| 53 | + d[i, 0] = i; |
| 54 | + } |
| 55 | + for (int j = 0; j <= m; j++) |
| 56 | + { |
| 57 | + d[0, j] = j; |
| 58 | + } |
| 59 | + |
| 60 | + //fill the distance matrix |
| 61 | + int cost; |
| 62 | + |
| 63 | + for (int i = 1; i <= n; i++) |
| 64 | + { |
| 65 | + for (int j = 1; j <= m; j++) |
| 66 | + { |
| 67 | + |
| 68 | + //if s1[i - 1] = s2[j - 1] then cost = 0, else cost = 1 |
| 69 | + cost = (s1[i - 1] == s2[j - 1]) ? 0 : 1; |
| 70 | + |
| 71 | + d[i, j] = Min( |
| 72 | + d[i - 1, j - 1] + cost, // substitution |
| 73 | + d[i, j - 1] + 1, // insertion |
| 74 | + d[i - 1, j] + 1 // deletion |
| 75 | + ); |
| 76 | + |
| 77 | + //transposition check |
| 78 | + if (i > 1 && j > 1 |
| 79 | + && s1[i - 1] == s2[j - 2] |
| 80 | + && s1[i - 2] == s2[j - 1] |
| 81 | + ) |
| 82 | + { |
| 83 | + d[i, j] = Math.Min(d[i, j], d[i - 2, j - 2] + cost); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return d[n, m]; |
| 89 | + } |
| 90 | + |
| 91 | + private static int Min(int a, int b, int c) |
| 92 | + => Math.Min(a, Math.Min(b, c)); |
| 93 | + } |
| 94 | +} |
0 commit comments