Channel affinity (share of the artist's DR plays per station before t, never played there) and genre fit (DR genre x station) in the per-station rows; Best week: any date range up to 12 months
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+70
-1
@@ -81,6 +81,12 @@ pub struct Panel {
|
||||
pub listed_on: Vec<Vec<NaiveDate>>,
|
||||
/// New Music Friday Denmark placements per artist: (Friday, position if known), sorted.
|
||||
pub nmf_dk: Vec<Vec<(NaiveDate, Option<i64>)>>,
|
||||
/// All-time DR plays per station group (P3, P4, P5, P6, P8) from the artist page's most played
|
||||
/// tracks (P4: the national count, or the regional average when only regions are given).
|
||||
pub alltime: Vec<[f64; GROUPS]>,
|
||||
/// How much DR plays the artist's genre on each station group, relative to all music: ln of
|
||||
/// (genre's share of plays on the group / the group's share of all plays); 0 = genre unknown.
|
||||
pub genre_fit: Vec<[f64; GROUPS]>,
|
||||
/// Season index per ISO week (1..=53): new songs reaching the Airplay Top-20 that week of the
|
||||
/// year, relative to an average week, 2002–2024 (before the DR panel), smoothed over 5 weeks.
|
||||
pub season: [f64; 54],
|
||||
@@ -151,7 +157,8 @@ impl Panel {
|
||||
let (archive_rate, listed_on) = load_long_history(db, &id_of, &names, artists.len())?;
|
||||
let nmf_dk = load_nmf_dk(db, &id_of, &artists)?;
|
||||
let season = load_season(db)?;
|
||||
Ok(Panel { weeks, artists, y, chart, list_tau, releases, archive_rate, listed_on, nmf_dk, season })
|
||||
let (alltime, genre_fit) = load_affinity(db, &id_of, artists.len())?;
|
||||
Ok(Panel { weeks, artists, y, chart, list_tau, releases, archive_rate, listed_on, nmf_dk, season, alltime, genre_fit })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -336,3 +343,65 @@ fn load_season(db: &Db) -> Result<[f64; 54]> {
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// Broad genre from DR's description ("Sanger (Afrobeats/Pop/R&B)" -> the first genre that maps).
|
||||
pub fn genre_bucket(description: &str) -> Option<usize> {
|
||||
let inner = description.split('(').nth(1)?.split(')').next()?;
|
||||
for tok in inner.split('/') {
|
||||
let t = tok.trim().to_lowercase();
|
||||
let b = if ["hip hop", "hiphop", "rap", "grime", "trap", "drill"].iter().any(|k| t.contains(k)) { 1 }
|
||||
else if ["jazz", "fusion", "bebop", "swing"].iter().any(|k| t.contains(k)) { 4 }
|
||||
else if ["klassisk", "filmmusik", "musical", "opera", "kor"].iter().any(|k| t.contains(k)) { 7 }
|
||||
else if ["r&b", "soul", "funk", "gospel", "afrobeat", "dancehall", "reggae"].iter().any(|k| t.contains(k)) { 5 }
|
||||
else if ["house", "techno", "elektronisk", "electronica", "electro", "dubstep", "dance", "disco"].iter().any(|k| t.contains(k)) { 3 }
|
||||
else if ["rock", "punk", "metal", "indie", "new wave", "blues", "grunge"].iter().any(|k| t.contains(k)) { 2 }
|
||||
else if ["folk", "country", "vise", "singer"].iter().any(|k| t.contains(k)) { 6 }
|
||||
else if ["latin", "world", "verdens"].iter().any(|k| t.contains(k)) { 8 }
|
||||
else if ["pop", "dansktop", "schlager", "underholdning"].iter().any(|k| t.contains(k)) { 0 }
|
||||
else { continue };
|
||||
return Some(b);
|
||||
}
|
||||
None
|
||||
}
|
||||
const GENRES: usize = 9;
|
||||
|
||||
fn load_affinity(db: &Db, id_of: &HashMap<String, usize>, n: usize) -> Result<(Vec<[f64; GROUPS]>, Vec<[f64; GROUPS]>)> {
|
||||
let mut alltime = vec![[0.0f64; GROUPS]; n];
|
||||
let mut genre: Vec<Option<usize>> = vec![None; n];
|
||||
for r in db.conn.prepare("SELECT artist_id, raw FROM dr_artists")?.query_map([], |r| Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?)))? {
|
||||
let (id, raw) = r?;
|
||||
let Some(&a) = id_of.get(&id) else { continue };
|
||||
let v: Value = serde_json::from_str(&raw).unwrap_or(Value::Null);
|
||||
genre[a] = v["artist"]["description"].as_str().and_then(genre_bucket);
|
||||
let (mut p4_nat, mut p4_reg, mut n_reg) = (0.0, 0.0, 0.0);
|
||||
for t in v["mostPlayed"].as_array().into_iter().flatten() {
|
||||
for s in t["statisticsByChannels"].as_array().into_iter().flatten() {
|
||||
let cnt = s["count"].as_f64().unwrap_or(0.0);
|
||||
match s["channel"]["slug"].as_str().unwrap_or("") {
|
||||
"p3" => alltime[a][0] += cnt, "p5" => alltime[a][2] += cnt, "p6beat" => alltime[a][3] += cnt, "p8jazz" => alltime[a][4] += cnt,
|
||||
"p4" => p4_nat += cnt,
|
||||
s if s.starts_with("p4") => { p4_reg += cnt; n_reg += 1.0; }
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
alltime[a][1] = p4_nat.max(if n_reg > 0.0 { p4_reg / 10.0 } else { 0.0 });
|
||||
}
|
||||
// Genre x station group: share of the genre's plays on each group vs the group's share overall.
|
||||
let mut by = [[0.0f64; GROUPS]; GENRES];
|
||||
let mut all = [0.0f64; GROUPS];
|
||||
for a in 0..n {
|
||||
for g in 0..GROUPS { all[g] += alltime[a][g]; if let Some(b) = genre[a] { by[b][g] += alltime[a][g]; } }
|
||||
}
|
||||
let tot: f64 = all.iter().sum();
|
||||
let mut fit = vec![[0.0f64; GROUPS]; n];
|
||||
for a in 0..n {
|
||||
let Some(b) = genre[a] else { continue };
|
||||
let tb: f64 = by[b].iter().sum();
|
||||
if tb <= 0.0 || tot <= 0.0 { continue; }
|
||||
for g in 0..GROUPS {
|
||||
fit[a][g] = (((by[b][g] + 1.0) / (tb + 5.0)) / ((all[g] + 1.0) / (tot + 5.0))).ln().clamp(-4.0, 2.0);
|
||||
}
|
||||
}
|
||||
Ok((alltime, fit))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user