2016-08-30 21:44:58 +03:00
|
|
|
/**
|
|
|
|
* Tae Won Ha - http://taewon.de - @hataewon
|
|
|
|
* See LICENSE
|
|
|
|
*/
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
class Scorer {
|
2016-12-17 11:35:07 +03:00
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
static func score(_ target: String, pattern: String) -> Int {
|
2016-09-07 23:06:17 +03:00
|
|
|
let fuzzy = Matcher.fuzzyIgnoringCase(target, pattern: pattern)
|
2016-08-30 21:44:58 +03:00
|
|
|
let upper = Matcher.numberOfUppercaseMatches(target, pattern: pattern)
|
|
|
|
let exactMatch = Matcher.exactMatchIgnoringCase(target, pattern: pattern)
|
2016-12-17 11:35:07 +03:00
|
|
|
let wf = Matcher.wagnerFisherDistance(target, pattern: pattern)
|
2016-08-30 21:44:58 +03:00
|
|
|
|
2016-09-08 21:16:37 +03:00
|
|
|
let exactScore: Int
|
2016-08-30 21:44:58 +03:00
|
|
|
switch exactMatch {
|
|
|
|
case .none:
|
|
|
|
exactScore = 0
|
|
|
|
case .exact:
|
2016-12-17 11:35:07 +03:00
|
|
|
return 1000
|
2016-08-30 21:44:58 +03:00
|
|
|
case .prefix, .contains, .suffix:
|
|
|
|
exactScore = 5
|
|
|
|
}
|
2016-09-08 21:16:37 +03:00
|
|
|
|
|
|
|
return exactScore
|
2016-12-17 11:35:07 +03:00
|
|
|
+ 10 * fuzzy
|
|
|
|
+ 5 * upper
|
|
|
|
- wf
|
2016-08-30 21:44:58 +03:00
|
|
|
}
|
|
|
|
}
|