Skip to content

Instantly share code, notes, and snippets.

@Kerollmops
Last active May 11, 2024 13:07
Show Gist options
  • Save Kerollmops/e5548d09b47127ff5bddb23eaf43c992 to your computer and use it in GitHub Desktop.
Save Kerollmops/e5548d09b47127ff5bddb23eaf43c992 to your computer and use it in GitHub Desktop.
Computes and updates a five-stars ranking score on the MusicBrainz dataset
// This script can literally replace the whole changes I made on this Rust program to alterate
// my dataset to be able to compute scores. You can find the function by follozing this link:
// <https://github.com/Kerollmops/musicbrainz-artist-scoring/blob/901ea2d35707477880f328211c76945a15b2410b/src/main.rs>
// The file extension is .rhai.rs just to be able to highlight the code.
// You can run this Rhai script on the Rhai Playground:
// <https://rhai.rs/playground/stable>
// Here is the context and document Meilisearch provides.
// It's just the setup part of the script
let doc = #{
artist_rating: 96,
// artist_rating: (),
artist_rating_count: 77,
track_rating: 100
// track_rating: ()
};
let context = #{
best_score: 739200,
smallest_top_score: 93960
};
// -- Actual script starts here... --
fn raw_rating() {
try {
this.artist_rating
* this.artist_rating_count
* (this.track_rating ?? 1)
} catch {
0
}
}
/// smallest top score -> 4/5 stars
/// biggest score -> 5/5 stars
fn rating(smallest_top_score, best_score) {
try {
let smallest = smallest_top_score / 10;
let smallest_float = smallest_top_score.to_float() / 10.0;
let raw_rating_float = this.raw_rating.to_float();
if this.raw_rating >= smallest {
let remaining = best_score.to_float() - smallest_float / 5.0;
let base = raw_rating_float - smallest_float;
4.0 + base / remaining
} else {
raw_rating_float * 4.0 / smallest_float
}
} catch {
()
}
}
doc.raw_rating = doc.raw_rating();
doc.new_rating = doc.rating(context.smallest_top_score, context.best_score);
// -- ...and ends here --
doc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment