change scoring system to discourage long feeds of keywords

This commit is contained in:
xfnw 2020-12-16 10:58:08 -05:00
parent 6a1b3d66d8
commit 96a12ecd4b

View file

@ -53,7 +53,9 @@ if (isset($_GET['q']) && preg_replace('/\s+/', '', $_GET['q']) != '') {
while ($row = $stmt->fetch()) { while ($row = $stmt->fetch()) {
$score = 0; $score = 0;
foreach ($terms as $param) foreach ($terms as $param)
$score = $score + substr_count(strtolower($row['content']),strtolower($param)); $score = $score + 100*(substr_count(strtolower($row['content']),strtolower($param)) / strlen($row['content']));
$score = $score + 5000*(substr_count(strtolower($row['url']),strtolower($param)) / strlen($row['url']));
$score = $score + 3000*(substr_count(strtolower($row['title']),strtolower($param)) / strlen($row['title']));
array_push($scores, $score); array_push($scores, $score);
$row['score'] = $score; $row['score'] = $score;
array_push($rows, $row); array_push($rows, $row);
@ -70,12 +72,12 @@ if (isset($_GET['q']) && preg_replace('/\s+/', '', $_GET['q']) != '') {
<div class='box'> <div class='box'>
<a href="<?php echo htmlspecialchars($row['url']); ?>"><?php echo htmlspecialchars($row['title']); ?></a> <a href="<?php echo htmlspecialchars($row['url']); ?>"><?php echo htmlspecialchars($row['title']); ?></a>
<br> <br>
<small>(score: <?php echo $row['score']; ?>) <?php echo htmlspecialchars($row['url']); ?></small> <small>(score: <?php echo round($row['score']); ?>) <?php echo htmlspecialchars($row['url']); ?></small>
<br> <br>
...<?php ...<?php
$content = $row['content']; $content = $row['content'];
foreach ($terms as $param) { foreach ($terms as $param) {
$pos = strpos($content, $param); $pos = strpos(strtolower($content), strtolower($param));
if ($pos !== false) { if ($pos !== false) {
echo htmlspecialchars(substr($content,$pos-50,50)); echo htmlspecialchars(substr($content,$pos-50,50));
echo '<strong>'.htmlspecialchars($param).'</strong>'; echo '<strong>'.htmlspecialchars($param).'</strong>';