Last active
April 20, 2020 12:50
-
-
Save bguidolim/2fcd99b7ccba72e5dda7862c2da79e3d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
extension String { | |
/// Check if a string contains in other string considering a minimum match percentage. | |
/// - Parameters: | |
/// - string: String that should be tested. | |
/// - matchPercentage: The minimum match percentage. | |
/// - Returns: Boolean indication if the requeriment was matched. | |
public func contains(_ string: String, matchPercentage: Float) -> Bool { | |
let set1 = Set<String>(self.components(separatedBy: .whitespaces)) | |
let set2 = Set<String>(string.components(separatedBy: .whitespaces)) | |
let match: Set<String> = set1.intersection(set2) | |
let percentage = Float(match.count) / ((Float(set1.count) + Float(set2.count)) / 2) | |
return percentage >= matchPercentage | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment