_binary From 4189c06c8b50f26bb162cb6de038705a3c7dab44 Mon Sep 17 00:00:00 2001
From: SalilShenoy <salilshenoy@gmail.com>
Date: Tue, 7 Nov 2017 10:18:57 -0800
Subject: [PATCH] Adding the Best Answer at the top of the Seacrh Results

---
 src/controllers/SearchController.php |  2 ++
 src/models/PhraseModel.php           | 17 +++++++++++++++++
 src/views/SearchView.php             |  5 +++++
 3 files changed, 24 insertions(+)

diff --git a/src/controllers/SearchController.php b/src/controllers/SearchController.php
index 8d73176e..82aa203f 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 71a911ad..2c9d6392 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 6e4a9681..addd6a52 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|") {
-- 
2.15.0.windows.1

