initial commit
This commit is contained in:
81
IrisCompanion/iris/Models/HeuristicRanker.swift
Normal file
81
IrisCompanion/iris/Models/HeuristicRanker.swift
Normal file
@@ -0,0 +1,81 @@
|
||||
//
|
||||
// HeuristicRanker.swift
|
||||
// iris
|
||||
//
|
||||
// Created by Codex.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
struct UserContext: Equatable {
|
||||
var isMoving: Bool
|
||||
var city: String
|
||||
|
||||
init(isMoving: Bool, city: String = "London") {
|
||||
self.isMoving = isMoving
|
||||
self.city = city
|
||||
}
|
||||
}
|
||||
|
||||
final class HeuristicRanker {
|
||||
private let nowProvider: () -> Int
|
||||
private let lastShownAtProvider: (String) -> Int?
|
||||
|
||||
init(now: @escaping () -> Int = { Int(Date().timeIntervalSince1970) },
|
||||
lastShownAt: @escaping (String) -> Int? = { _ in nil }) {
|
||||
self.nowProvider = now
|
||||
self.lastShownAtProvider = lastShownAt
|
||||
}
|
||||
|
||||
func pickWinner(from candidates: [Candidate], now: Int? = nil, context: UserContext) -> Winner {
|
||||
let currentTime = now ?? nowProvider()
|
||||
|
||||
let valid = candidates
|
||||
.filter { !$0.isExpired(at: currentTime) }
|
||||
.filter { $0.confidence >= 0.0 }
|
||||
|
||||
guard !valid.isEmpty else {
|
||||
return WinnerEnvelope.allQuiet(now: currentTime).winner
|
||||
}
|
||||
|
||||
var best: (candidate: Candidate, score: Double)?
|
||||
for candidate in valid {
|
||||
let baseWeight = baseWeight(for: candidate.type)
|
||||
var score = baseWeight * min(max(candidate.confidence, 0.0), 1.0)
|
||||
if let shownAt = lastShownAtProvider(candidate.id),
|
||||
currentTime - shownAt <= 2 * 60 * 60 {
|
||||
score -= 0.4
|
||||
}
|
||||
if best == nil || score > best!.score {
|
||||
best = (candidate, score)
|
||||
}
|
||||
}
|
||||
|
||||
guard let best else {
|
||||
return WinnerEnvelope.allQuiet(now: currentTime).winner
|
||||
}
|
||||
|
||||
let priority = min(max(best.score, 0.0), 1.0)
|
||||
return Winner(
|
||||
id: best.candidate.id,
|
||||
type: best.candidate.type,
|
||||
title: best.candidate.title.truncated(maxLength: TextConstraints.titleMax),
|
||||
subtitle: best.candidate.subtitle.truncated(maxLength: TextConstraints.subtitleMax),
|
||||
priority: priority,
|
||||
ttlSec: max(1, best.candidate.ttlSec)
|
||||
)
|
||||
}
|
||||
|
||||
private func baseWeight(for type: WinnerType) -> Double {
|
||||
switch type {
|
||||
case .weatherWarning: return 1.0
|
||||
case .weatherAlert: return 0.9
|
||||
case .transit: return 0.75
|
||||
case .poiNearby: return 0.6
|
||||
case .info: return 0.4
|
||||
case .nowPlaying: return 0.25
|
||||
case .currentWeather: return 0.0
|
||||
case .allQuiet: return 0.0
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user