Zoek Suggestie
Een korte functie om zoek suggesties te maken. Deze werkt alleen als je zoekt op tags. Dus kan erg handig zijn voor tutorial sites, of blogs, die ook met tags werken. Hij maakt gebruik van het Levenshtein algoritme, dit is eigenlijk degene die het echt werkt doet. Voor de rest moeten de comments het allemaal wel duidelijk maken. De database achter het systeem bevat momenteel maar 237 key words. dit zijn: * Landen in het Engels. Het config bestand laat ik buiten beschouwing, omdat dit toch geen code is die je geheel zal gaan gebruiken. Alleen de functie kan handig zijn. Graag al het commentaar dat jullie hebben. EDIT 1: MaxDistance automatisch terug naar 5, dit om beter te zoeken EDIT 2: Levensthein Costs geoptimaliseerd. EDIT 3: Nu een lijst van landen gebruikt, wel Engelstalig, maar wel een stuk groter. Ook een klein usability foutje aangepast in de zoek functie.
//searchSuggestion.php
[code]
<?php
/**
* @author Nico Kaag
* @copyright 2009
*/
error_reporting(E_ALL);
/**
* Give a list of search suggestions for a speciffic key word
* @param string $keyword The keyword to search for
* @param string $table The table in wich to search
* @param string $field The field in wich to search
* @param int $maxDistance The maximum difference between the keyword and suggestion
**/
function searchSuggestion($keyword, $table, $field, $maxDistance = 5)
{
//Variable to store the closest match in
//distance is -1 because it can't be an exact match, and it can be a possitive match
$closestMatch = array('tag' => '', 'distance' => '-1');
$return = array();
$testKeyword = strtolower($keyword);
$query = "SELECT ". $field ." FROM ". $table;
$result = mysql_query($query);
if (!$result || mysql_num_rows($result) == 0)
{
return false;
}
else
{
while ($row = mysql_fetch_assoc($result))
{
$tag = $row[$field];
$testTag = strtolower($row[$field]);
// calculate the distance between the input word,
// and the current word
$levenstheinDistance = levenshtein($testKeyword, $testTag, 1, 2, 1);
//$levenstheinRatio = max(array(strlen($testTag), strlen($testKeyword))) / $levenstheinDistance;
// if this distance is less than the next found shortest
// distance, OR if a next shortest word has not yet been found
if ($levenstheinDistance <= $closestMatch['distance'] || $closestMatch['distance'] < 0 && $levenstheinDistance <= $maxDistance) {
// set the closest match, and shortest distance
$closestMatch['tag'] = $tag;
$closestMatch['distance'] = $levenstheinDistance;
}
}
//If a tag is found, return the array with the closest match
if (!empty($closestMatch['tag']))
{
return $closestMatch['tag'];
}
//If no match is found, return false
else
{
return false;
}
}
}
?>
[/code]
//index.php
[code]
<?php
/**
* @author Nico Kaag
* @copyright 2009
*/
error_reporting(E_ALL);
include 'config.php';
include 'searchSuggestion.php';
?>
<form method="post" action="index.php">
Zoek: <input type="text" name="searchWord"><br />
<input type="submit" value="Zoeken!">
</form>
<br /><br />
<?php
if (isset($_POST['searchWord']))
{
$keyword = mysql_real_escape_string($_POST['searchWord']);
$query = "SELECT tag FROM tags WHERE tag = '". $keyword ."' LIMIT 0,1";
$result = mysql_query($query);
if (mysql_num_rows($result) == 1)
{
$row = mysql_fetch_assoc($result);
echo 'Ik vond: '. $row['tag'];
}
else
{
$closestMatch = searchSuggestion($keyword, "tags", "tag");
if (!$closestMatch)
{
echo 'Ik kon helaas niks voor u vinden.';
}
else
{
echo 'Zoekt u soms: <b>'. $closestMatch .'</b>?';
}
}
}
[/code]
Reacties
0