Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search update #55

Merged
merged 4 commits into from
Oct 20, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 61 additions & 6 deletions plugins/pico_search.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand All @@ -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 %}
// <p class="excerpt"><em>{{ page.search_snippet }} ...</em></p>
// {% else %}
// <p class="excerpt">{{ page.description }}</p>
// {% 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;
}

Expand Down