|
| 1 | +// |
| 2 | +// SwiftyLevenshtein.swift |
| 3 | +// Levenshtein distance algorithm written in Swift 2.2. Both a slow and highly optimized version are included. |
| 4 | +// |
| 5 | +// Created by Mark Hamilton on 3/31/16. |
| 6 | +// Copyright © 2016 dryverless. (http://www.dryverless.com) |
| 7 | +// |
| 8 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | +// of this software and associated documentation files (the "Software"), to deal |
| 10 | +// in the Software without restriction, including without limitation the rights |
| 11 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | +// copies of the Software, and to permit persons to whom the Software is |
| 13 | +// furnished to do so, subject to the following conditions: |
| 14 | +// |
| 15 | +// The above copyright notice and this permission notice shall be included in all |
| 16 | +// copies or substantial portions of the Software. |
| 17 | +// |
| 18 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | +// SOFTWARE. |
| 25 | +// |
| 26 | + |
| 27 | +import Foundation |
| 28 | + |
| 29 | +// Minimize 3 |
| 30 | +public func min3(a: Int, b: Int, c: Int) -> Int { |
| 31 | + |
| 32 | + return min( min(a, c), min(b, c)) |
| 33 | + |
| 34 | +} |
| 35 | + |
| 36 | +// In case they ever let subscripts throw |
| 37 | +//public extension String { |
| 38 | +// |
| 39 | +// internal enum SubscriptError: ErrorType { |
| 40 | +// |
| 41 | +// case InvalidFirstChar |
| 42 | +// |
| 43 | +// case InvalidLastChar |
| 44 | +// |
| 45 | +// } |
| 46 | +// |
| 47 | +// |
| 48 | +// subscript(range: Range<Int>) throws -> String { |
| 49 | +// |
| 50 | +// guard let firstChar = startIndex.advancedBy(range.startIndex) else { |
| 51 | +// |
| 52 | +// throw SubscriptError.InvalidFirstChar |
| 53 | +// } |
| 54 | +// |
| 55 | +// guard let lastChar = startIndex.advancedBy(range.endIndex) else { |
| 56 | +// |
| 57 | +// throw SubscriptError.InvalidLastChar |
| 58 | +// |
| 59 | +// } |
| 60 | +// |
| 61 | +// return self[firstChar...<lastChar] |
| 62 | +// |
| 63 | +// |
| 64 | +// } |
| 65 | +// |
| 66 | +//} |
| 67 | + |
| 68 | +public extension String { |
| 69 | + |
| 70 | + subscript(index: Int) -> Character { |
| 71 | + |
| 72 | + return self[startIndex.advancedBy(index)] |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + subscript(range: Range<Int>) -> String { |
| 77 | + |
| 78 | + let char0 = startIndex.advancedBy(range.startIndex) |
| 79 | + |
| 80 | + let charN = startIndex.advancedBy(range.endIndex) |
| 81 | + |
| 82 | + return self[char0..<charN] |
| 83 | + |
| 84 | + } |
| 85 | + |
| 86 | +} |
| 87 | + |
| 88 | +public struct Array2D { |
| 89 | + |
| 90 | + var columns: Int |
| 91 | + var rows: Int |
| 92 | + var matrix: [Int] |
| 93 | + |
| 94 | + |
| 95 | + init(columns: Int, rows: Int) { |
| 96 | + |
| 97 | + self.columns = columns |
| 98 | + |
| 99 | + self.rows = rows |
| 100 | + |
| 101 | + matrix = Array(count:columns*rows, repeatedValue:0) |
| 102 | + |
| 103 | + } |
| 104 | + |
| 105 | + subscript(column: Int, row: Int) -> Int { |
| 106 | + |
| 107 | + get { |
| 108 | + |
| 109 | + return matrix[columns * row + column] |
| 110 | + |
| 111 | + } |
| 112 | + |
| 113 | + set { |
| 114 | + |
| 115 | + matrix[columns * row + column] = newValue |
| 116 | + |
| 117 | + } |
| 118 | + |
| 119 | + } |
| 120 | + |
| 121 | + func columnCount() -> Int { |
| 122 | + |
| 123 | + return self.columns |
| 124 | + |
| 125 | + } |
| 126 | + |
| 127 | + func rowCount() -> Int { |
| 128 | + |
| 129 | + return self.rows |
| 130 | + |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +/* Levenshtein Distance Algorithm |
| 135 | + * Calculates the minimum number of changes (distance) between two strings. |
| 136 | + */ |
| 137 | + |
| 138 | +public func slowlevenshtein(sourceString: String, target targetString: String) -> Int { |
| 139 | + |
| 140 | + let source = Array(sourceString.unicodeScalars) |
| 141 | + let target = Array(targetString.unicodeScalars) |
| 142 | + |
| 143 | + let (sourceLength, targetLength) = (source.count, target.count) |
| 144 | + |
| 145 | + var matrix = Array(count: targetLength + 1, repeatedValue: Array(count: sourceLength + 1, repeatedValue: 0)) |
| 146 | + |
| 147 | + for x in 1..<targetLength { |
| 148 | + |
| 149 | + matrix[x][0] = matrix[x - 1][0] + 1 |
| 150 | + |
| 151 | + } |
| 152 | + |
| 153 | + for y in 1..<sourceLength { |
| 154 | + |
| 155 | + matrix[0][y] = matrix[0][y - 1] + 1 |
| 156 | + |
| 157 | + } |
| 158 | + |
| 159 | + for x in 1..<(targetLength + 1) { |
| 160 | + |
| 161 | + for y in 1..<(sourceLength + 1) { |
| 162 | + |
| 163 | + let penalty = source[y - 1] == target[x - 1] ? 0 : 1 |
| 164 | + |
| 165 | + let (deletions, insertions, substitutions) = (matrix[x - 1][y] + 1, matrix[x][y - 1] + 1, matrix[x - 1][y - 1]) |
| 166 | + |
| 167 | + matrix[x][y] = min3(deletions, b: insertions, c: substitutions + penalty) |
| 168 | + |
| 169 | + } |
| 170 | + |
| 171 | + } |
| 172 | + |
| 173 | + return matrix[targetLength][sourceLength] |
| 174 | + |
| 175 | +} |
| 176 | + |
| 177 | +public func levenshtein(sourceString: String, target targetString: String) -> Int { |
| 178 | + |
| 179 | + let source = Array(sourceString.unicodeScalars) |
| 180 | + let target = Array(targetString.unicodeScalars) |
| 181 | + |
| 182 | + let (sourceLength, targetLength) = (source.count, target.count) |
| 183 | + |
| 184 | + var distance = Array2D(columns: sourceLength + 1, rows: targetLength + 1) |
| 185 | + |
| 186 | + for x in 1...sourceLength { |
| 187 | + |
| 188 | + distance[x, 0] = x |
| 189 | + |
| 190 | + } |
| 191 | + |
| 192 | + for y in 1...targetLength { |
| 193 | + |
| 194 | + distance[0, y] = y |
| 195 | + |
| 196 | + } |
| 197 | + |
| 198 | + for x in 1...sourceLength { |
| 199 | + |
| 200 | + for y in 1...targetLength { |
| 201 | + |
| 202 | + if source[x - 1] == target[y - 1] { |
| 203 | + |
| 204 | + // no difference |
| 205 | + distance[x, y] = distance[x - 1, y - 1] |
| 206 | + |
| 207 | + } else { |
| 208 | + |
| 209 | + distance[x, y] = min3( |
| 210 | + |
| 211 | + // deletions |
| 212 | + distance[x - 1, y] + 1, |
| 213 | + // insertions |
| 214 | + b: distance[x, y - 1] + 1, |
| 215 | + // substitutions |
| 216 | + c: distance[x - 1, y - 1] + 1 |
| 217 | + |
| 218 | + ) |
| 219 | + |
| 220 | + } |
| 221 | + |
| 222 | + } |
| 223 | + |
| 224 | + } |
| 225 | + |
| 226 | + return distance[source.count, target.count] |
| 227 | + |
| 228 | +} |
| 229 | + |
| 230 | +public extension String { |
| 231 | + |
| 232 | + func getSlowLevenshtein(target: String) -> Int { |
| 233 | + |
| 234 | + return slowlevenshtein(self, target: target) |
| 235 | + |
| 236 | + } |
| 237 | + |
| 238 | + func getLevenshtein(target: String) -> Int { |
| 239 | + |
| 240 | + return levenshtein(self, target: target) |
| 241 | + |
| 242 | + } |
| 243 | + |
| 244 | +} |
0 commit comments