JSON:
[
{
"text": "sun",
"label": "RED"
},
{
"text": "",
"label": "RED"
},
{
"text": "citrus",
"label": "ORG"
},
{
"text": "warm",
"label": "ORG"
},
{
"text": "sunlight",
"label": "YLW"
},
{
"text": "cheese",
"label": "YLW"
},
{
"text": "foliage",
"label": "GRN"
},
{
"text": "organic",
"label": "GRN"
},
{
"text": "mint",
"label": "MNT"
},
{
"text": "freshness",
"label": "MNT"
},
{
"text": "sky",
"label": "BLU"
},
{
"text": "sea",
"label": "BLU"
},
{
"text": "magic",
"label": "VLT"
},
{
"text": "jealousy",
"label": "VLT"
}
]
And I updated my code to @ebhanson 's suggestion:
var body: some View { // view
TextField("Text...", text: $text).padding().background(color).onReceive(timer) { _ in
color = predict(for: text)?.color
print(color)
}.task {
do {
model = try ChromaClassifier(configuration: .init()).model
} catch {
print("NIL MDL")
model = nil
}
if let model = model {
predictor = try? NLModel(mlModel: model)
} else {
predictor = nil
}
}
var model: MLModel? = nil
var predictor: NLModel? = nil
func predict(for string: String) -> SingleColor? { // function
if let predictor = predictor {
let colorKeys = predictor.predictedLabelHypotheses(for: string, maximumCount: 1) // 1..7
print(colorKeys)
var color: SingleColor = .init(red: 0, green: 0, blue: 0)
for i in colorKeys {
color.morphing((ColorKeys.init(rawValue: i.key) ?? .white).toColor().percentage(of: i.value))
print(color)
}
return color
} else {
return nil
}
}