|
| 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 System.Collections.Generic; |
| 27 | +using F23.StringSimilarity.Interfaces; |
| 28 | + |
| 29 | +namespace F23.StringSimilarity.Experimental |
| 30 | +{ |
| 31 | + /// <summary> |
| 32 | + /// Sift4 - a general purpose string distance algorithm inspired by JaroWinkler |
| 33 | + /// and Longest Common Subsequence. |
| 34 | + /// Original JavaScript algorithm by siderite, java port by Nathan Fischer 2016. |
| 35 | + /// https://siderite.blogspot.com/2014/11/super-fast-and-accurate-string-distance.html |
| 36 | + /// </summary> |
| 37 | + public class Sift4 : IStringDistance |
| 38 | + { |
| 39 | + private const int DEFAULT_MAX_OFFSET = 10; |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// Gets or sets the maximum distance to search for character transposition. |
| 43 | + /// Compuse cost of algorithm is O(n . MaxOffset) |
| 44 | + /// </summary> |
| 45 | + public int MaxOffset { get; set; } = DEFAULT_MAX_OFFSET; |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Used to store relation between same character in different positions |
| 49 | + /// c1 and c2 in the input strings. |
| 50 | + /// </summary> |
| 51 | + /// <remarks> |
| 52 | + /// .NET port notes: should this be a struct instead? |
| 53 | + /// </remarks> |
| 54 | + private class Offset |
| 55 | + { |
| 56 | + internal readonly int c1; |
| 57 | + internal readonly int c2; |
| 58 | + internal bool trans; |
| 59 | + |
| 60 | + internal Offset(int c1, int c2, bool trans) |
| 61 | + { |
| 62 | + this.c1 = c1; |
| 63 | + this.c2 = c2; |
| 64 | + this.trans = trans; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + /// <summary> |
| 69 | + /// Sift4 - a general purpose string distance algorithm inspired by JaroWinkler |
| 70 | + /// and Longest Common Subsequence. |
| 71 | + /// Original JavaScript algorithm by siderite, java port by Nathan Fischer 2016. |
| 72 | + /// https://siderite.blogspot.com/2014/11/super-fast-and-accurate-string-distance.html |
| 73 | + /// </summary> |
| 74 | + /// <param name="s1"></param> |
| 75 | + /// <param name="s2"></param> |
| 76 | + /// <returns></returns> |
| 77 | + public double Distance(string s1, string s2) |
| 78 | + { |
| 79 | + if (string.IsNullOrEmpty(s1)) |
| 80 | + { |
| 81 | + if (s2 == null) |
| 82 | + { |
| 83 | + return 0; |
| 84 | + } |
| 85 | + |
| 86 | + return s2.Length; |
| 87 | + } |
| 88 | + |
| 89 | + if (string.IsNullOrEmpty(s2)) |
| 90 | + { |
| 91 | + return s1.Length; |
| 92 | + } |
| 93 | + |
| 94 | + int l1 = s1.Length; |
| 95 | + int l2 = s2.Length; |
| 96 | + |
| 97 | + int c1 = 0; //cursor for string 1 |
| 98 | + int c2 = 0; //cursor for string 2 |
| 99 | + int lcss = 0; //largest common subsequence |
| 100 | + int local_cs = 0; //local common substring |
| 101 | + int trans = 0; //number of transpositions ('ab' vs 'ba') |
| 102 | + |
| 103 | + // offset pair array, for computing the transpositions |
| 104 | + var offset_arr = new List<Offset>(); |
| 105 | + |
| 106 | + while ((c1 < l1) && (c2 < l2)) |
| 107 | + { |
| 108 | + if (s1[c1] == s2[c2]) |
| 109 | + { |
| 110 | + local_cs++; |
| 111 | + bool is_trans = false; |
| 112 | + // see if current match is a transposition |
| 113 | + int i = 0; |
| 114 | + while (i < offset_arr.Count) |
| 115 | + { |
| 116 | + Offset ofs = offset_arr[i]; |
| 117 | + if (c1 <= ofs.c1 || c2 <= ofs.c2) |
| 118 | + { |
| 119 | + // when two matches cross, the one considered a |
| 120 | + // transposition is the one with the largest difference |
| 121 | + // in offsets |
| 122 | + is_trans = Math.Abs(c2 - c1) >= Math.Abs(ofs.c2 - ofs.c1); |
| 123 | + |
| 124 | + if (is_trans) |
| 125 | + { |
| 126 | + trans++; |
| 127 | + } |
| 128 | + else |
| 129 | + { |
| 130 | + if (!ofs.trans) |
| 131 | + { |
| 132 | + ofs.trans = true; |
| 133 | + trans++; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + break; |
| 138 | + } |
| 139 | + else |
| 140 | + { |
| 141 | + if (c1 > ofs.c2 && c2 > ofs.c1) |
| 142 | + { |
| 143 | + offset_arr.RemoveAt(i); |
| 144 | + } |
| 145 | + else |
| 146 | + { |
| 147 | + i++; |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + offset_arr.Add(new Offset(c1, c2, is_trans)); |
| 153 | + } |
| 154 | + else |
| 155 | + { |
| 156 | + // s1.charAt(c1) != s2.charAt(c2) |
| 157 | + lcss += local_cs; |
| 158 | + local_cs = 0; |
| 159 | + if (c1 != c2) |
| 160 | + { |
| 161 | + //using min allows the computation of transpositions |
| 162 | + c1 = Math.Min(c1, c2); |
| 163 | + c2 = c1; |
| 164 | + } |
| 165 | + |
| 166 | + // if matching characters are found, remove 1 from both cursors |
| 167 | + // (they get incremented at the end of the loop) |
| 168 | + // so that we can have only one code block handling matches |
| 169 | + for (int i = 0; |
| 170 | + i < MaxOffset && (c1 + i < l1 || c2 + i < l2); |
| 171 | + i++) |
| 172 | + { |
| 173 | + if ((c1 + i < l1) && (s1[c1 + i] == s2[c2])) |
| 174 | + { |
| 175 | + c1 += i - 1; |
| 176 | + c2--; |
| 177 | + break; |
| 178 | + } |
| 179 | + |
| 180 | + if ((c2 + i < l2) && (s1[c1] == s2[c2 + i])) |
| 181 | + { |
| 182 | + c1--; |
| 183 | + c2 += i - 1; |
| 184 | + break; |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | + c1++; |
| 189 | + c2++; |
| 190 | + // this covers the case where the last match is on the last token |
| 191 | + // in list, so that it can compute transpositions correctly |
| 192 | + if ((c1 >= l1) || (c2 >= l2)) |
| 193 | + { |
| 194 | + lcss += local_cs; |
| 195 | + local_cs = 0; |
| 196 | + c1 = Math.Min(c1, c2); |
| 197 | + c2 = c1; |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + lcss += local_cs; |
| 202 | + |
| 203 | + // add the cost of transpositions to the final result |
| 204 | + return Math.Round((double) (Math.Max(l1, l2) - lcss + trans)); |
| 205 | + } |
| 206 | + } |
| 207 | +} |
0 commit comments