/** *选择相似度高的用户评价高但是目标用户还未进行评价的书籍进行推荐 */ function getRecommend($i) { getDatabaseConnect($con); $a = array(); //存储相似用户User-ID-相似度的映射 $sql = "select `User-ID` from `bx-book-ratings` where `ISBN` in (select `ISBN` from `bx-book-ratings` where `User-ID`={$i})"; //与目标用户评过相同书的用户 $result = $con->query($sql); while ($row = $result->fetch_array()) { $userid = 'User-ID'; $uid = $row[$userid]; $a[$uid] = sim($i, $uid); //计算相似度 } arsort($a); //按照相似度降序排序 //print_r($a);//打印数组,调试时用 $b = array(); //存储ISBN-ISBN映射 存储前n个相似用户评过的书 $c = array(); //存储ISBN-评分映射 存储前n个相似用户评过的书以及他们对这些书的评分 $n = 0; foreach ($a as $keys => $values) { //数组遍历 遍历每个数组元素时拆分为键-值对 $sql = "select `ISBN`,`Book-Rating` from `bx-book-ratings` where `User-ID`={$keys}"; //该用户评过的所有书 $result = $con->query($sql); while ($row = $result->fetch_array()) { $isbn = $row['ISBN']; $b[$isbn] = $isbn; $c[$isbn] = $row['Book-Rating']; } if ($n == 5) { break; } //取最相似的前5个用户 $n++; } $e = array(); //存储目标用户评过的书 $sql = "select `ISBN` from `bx-book-ratings` where `User-ID`={$i}"; $result = $con->query($sql); while ($row = $result->fetch_array()) { $isbn = $row['ISBN']; $e[$isbn] = $isbn; } $d = array_diff($b, $e); //取数组b和e的差集为d,即为相似用户们评过但是目标用户没评过的书 foreach ($d as $keys => $values) { if ($c[$values] < 5) { unset($d[$keys]); } } //只取相似用户们评分高的书籍,可以假设大于5分为评分高 $div = ""; //print_r($d); $n = 0; foreach ($d as $keys => $values) { $bookname_query = "select * from `bx-books` where `ISBN`={$values}"; $bookname_result = $con->query($bookname_query); if ($bookname_result) { $bookname_row = $bookname_result->fetch_array(); if ($bookname_row) { $div = $div . createImgDivByRow($bookname_row); } } if ($n > 10) { break; } //取前10本书 $n++; } //通过评分数据表,获取j评分高,但i未评分的书籍,可以假设大于5分为评分高 return $div; }
} ?> </p> <h3>Results</h3> <p> <button id='btn-download-sim'>Download Results</button> <?php $db = pg_connect("user={$db_user} dbname={$db_name} host={$db_host} port={$db_port}"); if (!$db) { die("Error in connection: " . pg_last_error()); } $pagenum = $_GET["pagenum"]; if (empty($pagenum)) { $_SESSION['tmp_table_sim'] = sim($fingerprint, $similarity, $query); deleteTmpTables(); $pagenum = 1; } $sqlCont = "SELECT count(*) FROM " . $_SESSION['tmp_table_sim']; $resultCont = pg_query($db, $sqlCont); $rowOne = pg_fetch_row($resultCont); $rows = $rowOne[0]; if (!$resultCont || $rows == 0) { echo '<b>No Results, please search again</b>'; } else { $page_rows = 11; $last = ceil($rows / $page_rows); if ($pagenum < 1) { $pagenum = 1; } elseif ($pagenum > $last) {