diff --git a/plugins/pico_search.php b/plugins/pico_search.php index a7c427f4..629cd030 100644 --- a/plugins/pico_search.php +++ b/plugins/pico_search.php @@ -11,6 +11,18 @@ class Pico_Search { private $pages = array(); + private function search_loop($search_target, $search_query, $assign_score, &$results){ + $words = explode(" ", preg_replace('/[^a-zA-Z0-9]+/', ' ', $search_target)); + + foreach ($search_query as $q) { + foreach ($words as $word) { + if (levenshtein($q, $word)/strlen($word) < $assign_score/strlen($word) ) { + $results += $assign_score - levenshtein($q, $word); + } + } + } + } + public function before_read_file_meta(&$headers) { $headers['purpose'] = 'Purpose'; @@ -41,6 +53,8 @@ public function before_render(&$twig_vars, &$twig) $this->pages[$k]["score"] = 0; $title = strtoupper($page["title"]); $content = strtoupper($page["content"]); + $content_stripped = strip_tags($page["content"]); + $tags = strtoupper($page["tags"]); if (strstr($title, $q)) $this->pages[$k]["score"]+= 10; @@ -62,17 +76,58 @@ public function before_render(&$twig_vars, &$twig) } break; case 'OR': - if (count(array_intersect($qs, explode(" ", $title))) > 0){ - $this->pages[$k]["score"]+= 3; + + // TODO: Rewrite the loops below (DRY). + + // $this->search_loop($title, $qs, 10, $this->pages[$k]["score"]); + // $this->search_loop($content, $qs, 5, $this->pages[$k]["score"]); + // $this->search_loop($tags, $qs, 5, $this->pages[$k]["score"]); + + $words = explode(" ", preg_replace('/[^a-zA-Z0-9]+/', ' ', $title)); + + foreach ($qs as $q) { + foreach ($words as $word) { + if (levenshtein($q, $word)/strlen($word) < 10/strlen($word) ) { + $this->pages[$k]["score"]+= 5 - levenshtein($q, $word); + } + } } - if (count(array_intersect($qs, explode(" ", $content))) > 0){ - $this->pages[$k]["score"]+= 3; + + $words = explode(" ", preg_replace('/[^a-zA-Z0-9]+/', ' ', $content)); + + foreach ($qs as $q) { + foreach ($words as $word) { + if (levenshtein($q, $word)/strlen($word) < 3/strlen($word) ) { + $this->pages[$k]["score"]+= 3 - levenshtein($q, $word); + // TODO: + // + // if (strpos($content_stripped, $q) > -1 ){ + // $this->pages[$k]["search_snippet"] = substr($content_stripped, strpos($content_stripped, $q), 100); + // } + // + // Once the search snippet is set up to work, update + // search_results.html as below: + // + // {% if page.search_snippet|length > 0 %} + //

{{ page.search_snippet }} ...

+ // {% else %} + //

{{ page.description }}

+ // {% endif %} + } + } } - if (count(array_intersect($qs, explode(",", $tags))) > 0){ - $this->pages[$k]["score"]+= 3; + $words = explode(" ", preg_replace('/[^a-zA-Z0-9]+/', ' ', $tags)); + + foreach ($qs as $q) { + foreach ($words as $word) { + if (levenshtein($q, $word)/strlen($word) < 3/strlen($word) ) { + $this->pages[$k]["score"]+= 3 - levenshtein($q, $word); + } + } } + break; }