Add fuzzify and fromCrispSet
This commit is contained in:
parent
186d94ff17
commit
38477208d3
|
@ -25,3 +25,17 @@ public struct DiscreteFuzzySet<Universe: Hashable>: FuzzySet {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public extension DiscreteFuzzySet {
|
||||||
|
static func fromCrispSet(_ set: Set<Universe>) -> Self {
|
||||||
|
let gradeTuples = set.map { ($0, 1.0 ) }
|
||||||
|
let gradeDictionary = Dictionary(uniqueKeysWithValues: gradeTuples)
|
||||||
|
return .init(elementToGradeMap: gradeDictionary)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public extension Set {
|
||||||
|
func fuzzified() -> DiscreteFuzzySet<Set.Element> {
|
||||||
|
DiscreteFuzzySet.fromCrispSet(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -60,4 +60,24 @@ final class DiscreteFuzzySetTests: XCTestCase {
|
||||||
assertExpectedGrade(element: element, expectedGrade: grade, sut: sut2)
|
assertExpectedGrade(element: element, expectedGrade: grade, sut: sut2)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func test_fuzzify_gradesAreCorrect() throws {
|
||||||
|
let cs: Set<String> = ["a", "b", "c"]
|
||||||
|
let expected = [
|
||||||
|
"a": 1.0,
|
||||||
|
"b": 1.0,
|
||||||
|
"c": 1.0,
|
||||||
|
"d": 0.0,
|
||||||
|
" ": 0.0,
|
||||||
|
"": 0.0,
|
||||||
|
]
|
||||||
|
|
||||||
|
let fs1 = cs.fuzzified()
|
||||||
|
let fs2 = DiscreteFuzzySet.fromCrispSet(cs)
|
||||||
|
|
||||||
|
for (element, grade) in expected {
|
||||||
|
assertExpectedGrade(element: element, expectedGrade: grade, sut: fs1)
|
||||||
|
assertExpectedGrade(element: element, expectedGrade: grade, sut: fs2)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue