Skip to content

Commit 73b5f65

Browse files
committed
tokens set ratio
1 parent 7eafa01 commit 73b5f65

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

Fuzzywuzzy_swift/String_Fuzzywuzzy.swift

+48
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,52 @@ public extension String {
8181
static public func fuzzPartialTokenSortRatio(str1 str1: String, str2: String, fullProcess: Bool = true) -> Int {
8282
return _fuzzTokenSort(str1: str1, str2: str2, partial: true, fullProcess: fullProcess)
8383
}
84+
85+
static func _token_set(str1 str1: String, str2: String, partial: Bool = true, fullProcess: Bool = true) -> Int {
86+
var p1 = str1
87+
var p2 = str2
88+
if fullProcess {
89+
p1 = StringProcessor.process(p1)
90+
p2 = StringProcessor.process(p2)
91+
}
92+
93+
let tokens1 = Set(p1.componentsSeparatedByString(" "))
94+
let tokens2 = Set(p2.componentsSeparatedByString(" "))
95+
96+
let intersection = tokens1.intersect(tokens2)
97+
let diff1to2 = tokens1.subtract(tokens2)
98+
let diff2to1 = tokens2.subtract(tokens1)
99+
100+
var sorted_sect = intersection.sort().joinWithSeparator(" ")
101+
let sorted_1to2 = diff1to2.sort().joinWithSeparator(" ")
102+
let sorted_2to1 = diff2to1.sort().joinWithSeparator(" ")
103+
104+
var combined_1to2 = sorted_sect + " " + sorted_1to2
105+
var combined_2to1 = sorted_sect + " " + sorted_2to1
106+
107+
sorted_sect = sorted_sect.stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: " "))
108+
combined_1to2 = combined_1to2.stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: " "))
109+
combined_2to1 = combined_2to1.stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: " "))
110+
111+
let pariwise = [(sorted_sect, combined_1to2),
112+
(sorted_sect, combined_2to1),
113+
(combined_1to2, combined_2to1)]
114+
let ratios = pariwise.map { (str1, str2) -> Int in
115+
if partial {
116+
return String.fuzzPartialRatio(str1: str1, str2: str2)
117+
} else {
118+
return String.fuzzRatio(str1: str1, str2: str2)
119+
}
120+
}
121+
122+
return ratios.maxElement()!
123+
}
124+
125+
static func fuzzTokenSetRatio(str1 str1: String, str2: String, fullProcess: Bool = true) -> Int {
126+
return _token_set(str1: str1, str2: str2, partial: false, fullProcess: fullProcess)
127+
}
128+
129+
static func fuzzPartialTokenSetRatio(str1 str1: String, str2: String, fullProcess: Bool = true) -> Int {
130+
return _token_set(str1: str1, str2: str2, partial: true, fullProcess: fullProcess)
131+
}
84132
}

0 commit comments

Comments
 (0)