我正在为音乐家建立一个社交网站。我想从数据库中获取整个歌曲列表,并根据其与登录用户的相关性对每个歌曲进行排名。这样做的一个原因是,即使用户在45秒前注册,我也希望总是推荐10首歌曲。
我正在使用的因素是:
(子)类型属于更一般的类型,所以我想如果登录的用户和歌曲的(子)类型相同,我可以加权一首歌,如果他们至少属于,则可以稍高一点同一般的流派。
我不确定我是不是应该针对Apriori或贝叶斯算法...它似乎介于两者之间。我从黑客新闻算法的解释中借鉴了几行,我已经调整过了。我不太确定如何将最终结果放在一起以获得我的排名。
这是我到目前为止编写的代码(出于所有意图和目的,工作室是一首歌)。我知道像这样的循环中的查询从来都不是一个好主意,但我这样做是因为我使用了一些memcaching方案,这可能也会被批处理:
function studio_relevance($user_id, $user_genre) {
global $cxn;
$query = "SELECT * FROM studio WHERE project_status='active'";
$result = mysqli_query($cxn, $query) or die($query.': '.mysqli_error($cxn));
$data = array();
while ($studio = mysqli_fetch_object($result)) {
//Find similarities in social connections and band
$query_b = "SELECT * FROM band WHERE studio_id=".$studio->studio_id;
$result_b = mysqli_query($cxn, $query_b) or die($query_b.': '.mysqli_error($cxn));
$the_band = array();
while ($people = mysqli_fetch_array($result_b, MYSQLI_ASSOC)) {
$the_band[] = $people['user_id'];
}
$studio_band_count = count($the_band);
$query = "SELECT * FROM idols WHERE friend_id=".$user_id;
$result_b = mysqli_query($cxn, $query_b) or die($query_b.': '.mysqli_error($cxn));
$idol = array();
while ($people = mysqli_fetch_array($result_b, MYSQLI_ASSOC)) {
$idol[] = $people['artist_id'];
}
$same_band = array_intersect($the_band, $idol);
$same_band_count = count($same_band);
########
$similar_band = $same_band_count / $studio_band_count;
########
//Find the most recent activity
$query_b = "SELECT * FROM studio_feed WHERE studio_id=".$studio->studio_id." ORDER BY feed_id DESC LIMIT 1";
$result_b = mysqli_query($cxn, $query_b) or die($query_b.': '.mysqli_error($cxn));
$tab = mysqli_fetch_object($result_b);
$time_diff = strtotime('now') - strtotime($tab->timestamp);
$hours = $time_diff / 3600;
########
$last_activity = pow(($hours+2), 1.8);
########
//Compare genres
$genre_weight = 1;
if ($studio->songGenre == $user_genre) {
$genre_weight = 3;
}
else {
$query_b = "SELECT * FROM genres";
$result_b = mysqli_query($cxn, $query_b) or die($query_b.': '.mysqli_error($cxn));
$genres = array();
$user_genre_cat = 0;
while ($genre = mysqli_fetch_object($result_b)) {
$genres[$genre->genre_cat][] = $genre->genre_id;
if ($genre->genre_id == $user_genre) {
$user_genre_cat = $genre->genre_cat;
}
}
if (in_array($studio->songGenre, $genres[$celeb_cat])) {
$genre_weight = 2;
}
}
//Find final result
//$final = $similar_band + $last_activity + $genre_weight;
//$hours / pow(($similar_band+2), $genre_weight);
}
return $data;
}
任何建议都将不胜感激!我是在正确的轨道上还是这样做错了?