Skip to content

Instantly share code, notes, and snippets.

@bguidolim
Last active April 20, 2020 12:50
Show Gist options
  • Save bguidolim/2fcd99b7ccba72e5dda7862c2da79e3d to your computer and use it in GitHub Desktop.
Save bguidolim/2fcd99b7ccba72e5dda7862c2da79e3d to your computer and use it in GitHub Desktop.
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