Film library discovery: Chair scoring of unassigned films (env-gated, read-only)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-30 12:57:34 +00:00
co-authored by Claude Opus 5.5
parent 7192e7c89c
commit 17870cdd20
@@ -0,0 +1,74 @@
package tv.orbit.classic
import org.json.JSONObject
import org.junit.Assume.assumeTrue
import org.junit.Test
import java.io.File
/**
* The film library discovery (Paul, 30 November 2026), read-only. Every library film not already on an Orbit channel is scored
* by the Director's Chair against each film channel's brief. Stage 1 (FILMS=films-local.jsonl, the local catalogue: genres,
* studios, rating, year, no descriptions) writes stage1.tsv; stage 2 (FILMS=films-verified.jsonl, the strongest re-read from the
* server with descriptions and community ratings, and SIM_SNAP for each channel's description profile) writes stage2.tsv.
* Env: DISCOVERY (folder), FILMS, optional SIM_SNAP.
*/
class FilmDiscoveryTest {
@Test fun score() {
val dir = System.getenv("DISCOVERY")?.let(::File); val name = System.getenv("FILMS")
assumeTrue(dir != null && dir.isDirectory && name != null)
val lines = File(dir, name).readLines().map { JSONObject(it) }
// Already on Orbit: every film any channel lists, by IMDb ID where the list knows it, else by name and year.
val entries = SmartChannels.flatMap { p -> (if (p.filmRestMs != null) p.searches else emptyList()) + p.channelFilms }.distinct().map(FilmIdentity::of)
val onImdb = entries.mapNotNull { it.imdb?.lowercase() }.toSet()
val onName = entries.groupBy { seriesKey(it.title) }
val home = HashMap<String, String>()
SmartChannels.forEach { p -> ((if (p.filmRestMs != null) p.searches else emptyList()) + p.channelFilms).forEach { e -> FilmIdentity.of(e).let { f -> home.putIfAbsent(seriesKey(f.title) + "|" + (f.year ?: ""), p.name) } } }
fun onOrbit(o: JSONObject): Boolean {
if (o.optString("imdb").lowercase() in onImdb) return true
val y = o.optInt("year", 0)
return onName[seriesKey(o.getString("name"))].orEmpty().any { f -> f.year == null || y == 0 || kotlin.math.abs(f.year - y) <= 1 }
}
val briefs = Briefs.filter { "Movie" in it.kinds }
val snap = System.getenv("SIM_SNAP")?.let(::File)?.takeIf { it.isDirectory }
val text = if (snap == null) ChannelText(emptyMap(), emptyMap()) else {
val user = snap.listFiles()!!.first { it.name.startsWith("episodes-") && it.name.endsWith(".json") }.let { JSONObject(it.readText()).getString("user") }
val store = ScheduleStore(snap)
val ev = File(snap, "evidence-0.jsonl").readLines().mapNotNull { runCatching { TitleEvidence.of(JSONObject(it)) }.getOrNull() }.associateBy { it.id }
ChannelText(briefs.associate { b -> b.number to store.loadPool(user, b.number)?.series.orEmpty().map { it.id } }, ev + lines.associate { it.getString("id") to TitleEvidence.of(it) })
}
val out = StringBuilder("id\timdb\ttitle\tyear\trating\tminutes\tcopies\tcommunity\tbest\tbestScore\tsecond\tsecondScore\twhy\tplayable\n")
var skipped = 0
for (o in lines) {
if (onOrbit(o)) { skipped++; continue }
val e = TitleEvidence.of(o)
val scored = briefs.map { b -> b to channelScore(e, b, text) }.sortedByDescending { it.second.first }
val best = scored[0]; val second = scored[1]
if (best.second.first <= 0.0) continue
fun clean(s: String) = s.replace('\t', ' ').replace('\n', ' ')
out.append("${o.getString("id")}\t${o.optString("imdb")}\t${clean(o.getString("name"))}\t${o.optInt("year")}\t${o.optString("rating")}\t${o.optInt("minutes")}\t${o.optInt("copies")}\t" +
"${o.optDouble("community").takeIf { !it.isNaN() } ?: ""}\t${best.first.number} ${best.first.name}\t${"%.2f".format(best.second.first)}\t${second.first.number} ${second.first.name}\t${"%.2f".format(second.second.first)}\t" +
"${clean(best.second.second.take(4).joinToString("; "))}\t${o.optString("playable")}\n")
}
File(dir, if (name.contains("verified")) "stage2.tsv" else "stage1.tsv").writeText(out.toString())
if (!name.contains("verified")) {
// The strongest per channel by what the catalogue can tell (genre, era, rating), most-copied first: those go to the server.
val pick = StringBuilder("channel\tid\ttitle\tyear\tcopies\tlocal\n")
val free = lines.filter { !onOrbit(it) }
for (b in briefs) {
val (lo, hi) = b.era
val cands = free.mapNotNull { o ->
val e = TitleEvidence.of(o); val g = e.genreSet
if (g.none { it in b.genresAny }) return@mapNotNull null
if (b.needAny.isNotEmpty() && g.none { it in b.needAny }) return@mapNotNull null
val y = e.year ?: return@mapNotNull null
if (y < lo - 2 || y > hi + 1) return@mapNotNull null
if (b.kids && e.rating.orEmpty() in Adult) return@mapNotNull null
Triple(o, o.optInt("copies"), channelScore(e, b, text).first)
}.filter { it.third > 0.0 }.sortedWith(compareByDescending<Triple<JSONObject, Int, Double>> { it.second }.thenByDescending { it.third }).take(60)
cands.forEach { (o, c, sc) -> pick.append("${b.number} ${b.name}\t${o.getString("id")}\t${o.getString("name").replace('\t', ' ')}\t${o.optInt("year")}\t$c\t${"%.2f".format(sc)}\n") }
}
File(dir, "select.tsv").writeText(pick.toString())
}
println("scored ${lines.size - skipped}, already on Orbit $skipped")
}
}