Add fuzzify and fromCrispSet

This commit is contained in:
Alexander Ignatov 2021-12-17 00:58:10 +02:00
parent 186d94ff17
commit 38477208d3
2 changed files with 34 additions and 0 deletions

View File

@ -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)
}
}

View File

@ -60,4 +60,24 @@ final class DiscreteFuzzySetTests: XCTestCase {
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)
}
}
}