This may be a little hard to explain but, for the heck of it I decided to look up code that precisely compares two strings and returns a distance percentage between those strings. Here is the problem: I am trying to take two strings and precisely compare them using precision equality, for example:
The best way I can describe what I am trying to accomplish is: taking two strings and seeing how far away (in distance) they are from each other (like the quoted example). I know that there is a Double method for this in swift called the Jaro Winkler Distance calculation but that is where the problem comes in, I implement the Jaro Winkler solution that I found online into my project but, when I try to compare these two strings:
Here is the code I found online, it is an extension of string
Now, by precision standards these strings distance from each other would equal to be 50%. I am trying to do that with ALL strings.String 1 = "Test string"
String 2 = "Test"
The best way I can describe what I am trying to accomplish is: taking two strings and seeing how far away (in distance) they are from each other (like the quoted example). I know that there is a Double method for this in swift called the Jaro Winkler Distance calculation but that is where the problem comes in, I implement the Jaro Winkler solution that I found online into my project but, when I try to compare these two strings:
I get that the total distance between those strings is: 93.48837% when in fact, the strings are identical and should give me the resulting distance of 100%. Am I missing something because if I input "Test" into field 1 and "Test" into field two, I get a result of "100%" as the distance comparison output.String 1 = "This is a test string of text - test string"
String 2 = "This is a test string of text - test string"
Here is the code I found online, it is an extension of string
Code Block extension String { public func distanceJaroWinkler(between target: String) -> Double { var stringOne = self var stringTwo = target if stringOne.count > stringTwo.count { stringTwo = self stringOne = target } let stringOneCount = stringOne.count let stringTwoCount = stringTwo.count if stringOneCount == 0 && stringTwoCount == 0 { return 1.0 } let matchingDistance = stringTwoCount / 2 var matchingCharactersCount: Double = 0 var transpositionsCount: Double = 0 var previousPosition = -1 // Count matching characters and transpositions. for (i, stringOneChar) in stringOne.enumerated() { for (j, stringTwoChar) in stringTwo.enumerated() { if max(0, i - matchingDistance)..<min(stringTwoCount, i + matchingDistance) ~= j { if stringOneChar == stringTwoChar { matchingCharactersCount += 1 if previousPosition != -1 && j < previousPosition { transpositionsCount += 1 } previousPosition = j break } } } } if matchingCharactersCount == 0.0 { return 0.0 } // Count common prefix (up to a maximum of 4 characters) let commonPrefixCount = min(max(Double(self.commonPrefix(with: target).count), 0), 4) let jaroSimilarity = (matchingCharactersCount / Double(stringOneCount) + matchingCharactersCount / Double(stringTwoCount) + (matchingCharactersCount - transpositionsCount) / matchingCharactersCount) / 3 // Default is 0.1, should never exceed 0.25 (otherwise similarity score could exceed 1.0) let commonPrefixScalingFactor = 0.1 return jaroSimilarity + commonPrefixCount * commonPrefixScalingFactor * (1 - jaroSimilarity) } }