Last active
April 1, 2020 03:54
-
-
Save rjchatfield/fe4b57183c66266c23cb396cadd37118 to your computer and use it in GitHub Desktop.
Levenshtein Distance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// LevenshteinDistance.swift | |
// | |
// Created by Rob Chatfield on 1 Apr 2020 | |
// Copyright 2020 Rob Chatfield. All rights reserved. | |
// | |
// This code is an updated Swift implementation based on "LevenshteinSwift" Created by Cory Alder. | |
// Which was based on the Objective-C "NSString+Levenshtein" Created by Mark Aufflick. | |
// Which was based loosely on the NSString(Levenshtein) code by Rick Bourner | |
// | |
// Improvements and suggestions welcome. | |
/* | |
Copyright (c) 2009, Mark Aufflick | |
All rights reserved. | |
Redistribution and use in source and binary forms, with or without | |
modification, are permitted provided that the following conditions are met: | |
* Redistributions of source code must retain the above copyright | |
notice, this list of conditions and the following disclaimer. | |
* Redistributions in binary form must reproduce the above copyright | |
notice, this list of conditions and the following disclaimer in the | |
documentation and/or other materials provided with the distribution. | |
* Neither the name of the Mark Aufflick nor the | |
names of contributors may be used to endorse or promote products | |
derived from this software without specific prior written permission. | |
THIS SOFTWARE IS PROVIDED BY MARK AUFFLICK ''AS IS'' AND ANY | |
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
DISCLAIMED. IN NO EVENT SHALL MARK AUFFLICK BE LIABLE FOR ANY | |
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
*/ | |
import Foundation | |
public extension String { | |
func asciiLevenshteinDistance(string stringBOrig: String, skippingCharset charset: CharacterSet? = nil) -> Int { | |
let stringA: String | |
let stringB: String | |
if let charset = charset { | |
stringA = self.components(separatedBy: charset).joined() | |
stringB = stringBOrig.components(separatedBy: charset).joined() | |
} else { | |
stringA = self | |
stringB = stringBOrig | |
} | |
guard !stringA.isEmpty, !stringB.isEmpty else { | |
return 0 | |
} | |
let aCountPlus1 = stringA.count + 1 | |
let bCountPlus1 = stringB.count + 1 | |
var d = Array(repeating: 0, count: bCountPlus1 * aCountPlus1) | |
for k in 0..<aCountPlus1 { | |
d[k] = k | |
} | |
for k in 0..<bCountPlus1 { | |
d[k * aCountPlus1] = k | |
} | |
for (charAIndex, charA) in stringA.enumerated() { | |
for (charBIndex, charB) in stringB.enumerated() { | |
let cost = charA == charB ? 0 : 1 | |
let v1 = d[charBIndex * aCountPlus1 + (charAIndex + 1)] + 1 | |
let v2 = d[(charBIndex + 1) * aCountPlus1 + charAIndex] + 1 | |
let v3 = d[charBIndex * aCountPlus1 + charAIndex ] + cost | |
d[(charBIndex + 1) * aCountPlus1 + (charAIndex + 1)] = min(min(v1, v2), v3) | |
} | |
} | |
return d[aCountPlus1 * bCountPlus1 - 1] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment