Further tweaks guess semantics, a=chris

Chris Pollett [2019-06-26 18:Jun:th]
Further tweaks guess semantics, a=chris
Filename
src/models/PhraseModel.php
diff --git a/src/models/PhraseModel.php b/src/models/PhraseModel.php
index d21c2bf53..429b75bb1 100755
--- a/src/models/PhraseModel.php
+++ b/src/models/PhraseModel.php
@@ -365,8 +365,7 @@ class PhraseModel extends ParallelModel
                             $dis_cnt++;
                         }
                         list($word_struct, $format_words) =
-                            $this->parseWordStructConjunctiveQuery($disjunct,
-                            $guess_semantics);
+                            $this->parseWordStructConjunctiveQuery($disjunct);
                         if ($word_struct != null) {
                             $word_structs[] = $word_struct;
                         }
@@ -515,12 +514,9 @@ class PhraseModel extends ParallelModel
      * @param string& $phrase string to extract struct from, if the phrase
      *  semantics is guessed or an if condition is processed the value of
      *  phrase will be altered. (Helps for feeding to network queries)
-     * @param boolean $guess_semantics whether to add lang: and safe:
-     *  meta word even if not present in orginal query based on guessed values.
      * @return array struct representing the conjunctive query
      */
-    public function parseWordStructConjunctiveQuery(&$phrase,
-        $guess_semantics = true)
+    public function parseWordStructConjunctiveQuery(&$phrase)
     {
         $query = $phrase;
         $indent= "  ";
@@ -533,7 +529,7 @@ class PhraseModel extends ParallelModel
         list($found_metas, $disallow_phrases,
             $phrase_string, $query_string, $index_name, $weight,
             $locale_tag) =
-            $this->extractMetaWordInfo($phrase, $guess_semantics);
+            $this->extractMetaWordInfo($phrase);
         /*
             we search using the stemmed/char-grammed words, but we format
             snippets in the results by bolding either
@@ -715,12 +711,10 @@ class PhraseModel extends ParallelModel
      * the query
      *
      * @param string $phrase the query string
-     * @param boolean $guess_semantics whether to add lang: and safe:
-     *  meta word even if not present in orginal query based on guessed values.
      * @return array containing items listed above in the description of this
      *     method
      */
-    public function extractMetaWordInfo($phrase, $guess_semantics = true)
+    public function extractMetaWordInfo($phrase)
     {
         $index_name = $this->index_name;
         $weight = 1;
@@ -764,17 +758,6 @@ class PhraseModel extends ParallelModel
                 && substr($matches[0], 0, 11) == 'media:image') {
                 $found_media_image = true;
             }
-            if ($guess_semantics && !empty($matches[0])) {
-                if ($meta_word == 'site:') {
-                    $found_site = true;
-                }
-                if ($meta_word == 'safe:') {
-                    $found_safe = true;
-                }
-                if ($meta_word == 'lang:') {
-                    $found_lang = true;
-                }
-            }
             $phrase_string = preg_replace($pattern, "", $phrase_string);
         }
         $locale_tag = L\guessLocaleFromString($phrase_string);
@@ -784,18 +767,6 @@ class PhraseModel extends ParallelModel
             $phrase_string = "";
         }
         $found_metas = array_unique($found_metas);
-        if ($guess_semantics && !$found_media_image && !$found_site &&
-            (!empty($found_metas) || !empty($phrase_string))) {
-            if (!$found_lang) {
-                $lang_parts = explode("-", $locale_tag);
-                $found_metas[] = "lang:" . $lang_parts[0];
-            }
-            if (!$found_safe) {
-                $found_metas[] = (!isset($_SESSION['SAFE_SEARCH']) ||
-                    $_SESSION['SAFE_SEARCH'] == "true") ? "safe:true" :
-                    "safe:all";
-            }
-        }
         $disallow_phrases = array_unique($disallow_phrases);
         $phrase_string = mb_ereg_replace("&", "_and_", $phrase_string);
         $query_string = mb_ereg_replace(C\PUNCT, " ", $phrase_string);
@@ -839,7 +810,8 @@ class PhraseModel extends ParallelModel
         if (isset($this->programming_language_map[$tag])) {
             $this->program_indicator = true;
         }
-        $main_tag = substr($tag, 0, 2);
+        $tag_parts = explode("-", $tag);
+        $main_tag = $tag_parts[0];
         if ($len == 1) {
             $letter = "";
             switch ($main_tag) {
@@ -905,6 +877,16 @@ class PhraseModel extends ParallelModel
                 $phrase = $generated_question['RAW'][0];
             }
         }
+        if ($len > 0) {
+            if (!preg_match("/safe\:|site\:/", $phrase)) {
+                $phrase .= " " . ((!isset($_SESSION['SAFE_SEARCH']) ||
+                    $_SESSION['SAFE_SEARCH'] == "true") ? "safe:true" :
+                    "safe:all") . " ";
+            }
+            if (!preg_match("/media\:image|lang\:/", $phrase)) {
+                $phrase .= " " . "lang:" . $main_tag . " ";
+            }
+        }
         return $phrase;
     }
     /**
@@ -944,9 +926,9 @@ class PhraseModel extends ParallelModel
             }
             if ($tag == $lang_tag && $not_check) {
                 $body = substr($match, $start_pos);
-                $result_phrase .= " ".$new_prefix.$body.$suffix;
+                $result_phrase .= " " . $new_prefix . $body . $suffix;
             } else {
-                $result_phrase .= " ".$match;
+                $result_phrase .= " " . $match;
             }
         }
         return $result_phrase;
ViewGit