viewgit/inc/functions.php:22 Function utf8_encode() is deprecated [8192]

Adding the Best Answer at the top of the Seacrh Results

SalilShenoy [2017-11-07 18:Nov:th]
Adding the Best Answer at the top of the Seacrh Results

Signed-off-by: Chris Pollett <chris@pollett.org>
Filename
src/controllers/SearchController.php
src/models/PhraseModel.php
src/views/SearchView.php
diff --git a/src/controllers/SearchController.php b/src/controllers/SearchController.php
index 8d73176e5..82aa203f1 100755
--- a/src/controllers/SearchController.php
+++ b/src/controllers/SearchController.php
@@ -847,6 +847,8 @@ class SearchController extends Controller implements CrawlConstants
         $data['THESAURUS_VARIANTS'] =
             (isset($phrase_results['THESAURUS_VARIANTS'])) ?
              $phrase_results['THESAURUS_VARIANTS']: [];
+        $data['BEST_ANSWER'] = (isset($phrase_results['BEST_ANSWER'])) ?
+            $phrase_results['BEST_ANSWER'] : null;
         $data['SAVE_POINT'] = (isset($phrase_results["SAVE_POINT"])) ?
              $phrase_results["SAVE_POINT"]: [ 0 => 1];
         if (isset($phrase_results["HARD_QUERY"])) {
diff --git a/src/models/PhraseModel.php b/src/models/PhraseModel.php
index 71a911ad3..2c9d6392b 100755
--- a/src/models/PhraseModel.php
+++ b/src/models/PhraseModel.php
@@ -227,6 +227,7 @@ class PhraseModel extends ParallelModel
             $start_time = microtime(true);
         }
         $results = null;
+        $answer_score_map = array();
         $word_structs = [];
         /*
             this is a quick and dirty parsing and will usually work,
@@ -416,6 +417,17 @@ class PhraseModel extends ParallelModel
                                 if (isset($triplets_with_answer[$question])) {
                                     $out_results['PAGES'][$out_count]['ANSWER']=
                                         $triplets_with_answer[$question];
+                                    $answer = $triplets_with_answer[$question];
+                                    if (array_key_exists(
+                                        $answer, $answer_score_map)) {
+                                        $new_score = $answer_score_map[$answer]
+                                        + $out_results['PAGES'][$out_count]
+                                            ['OUT_SCORE'];
+                                    } else {
+                                        $answer_score_map[$answer] =
+                                        $out_results['PAGES'][$out_count]
+                                            ['OUT_SCORE'];
+                                    }
                                 }
                             }
                             $results['PAGES'][$i] =
@@ -485,6 +497,11 @@ class PhraseModel extends ParallelModel
                 $output['THESAURUS_VARIANTS'] =
                     $out_results['THESAURUS_VARIANTS'];
             }
+            if (isset($answer_score_map) == TRUE) {
+                arsort($answer_score_map);
+                reset($answer_score_map);
+                $output['BEST_ANSWER'] = key($answer_score_map);
+            }
         } else {
             $output = $results;
         }
diff --git a/src/views/SearchView.php b/src/views/SearchView.php
index 6e4a9681b..addd6a52f 100755
--- a/src/views/SearchView.php
+++ b/src/views/SearchView.php
@@ -239,6 +239,11 @@ class SearchView extends View implements CrawlConstants
         if (!$is_landing) {
             $this->element("displayadvertisement")->render($data);
         }
+        if (isset($data['BEST_ANSWER']) == TRUE) { ?>
+            <div id="best-answer" class="echo-link">
+                 <?= $data['BEST_ANSWER'] ?>
+            </div>
+        <?php }
         foreach ($data['PAGES'] as $page) {
             if (isset($page[self::URL])) {
                 if (substr($page[self::URL], 0, 4) == "url|") {
ViewGit