/** * Check spelling * * @param array of QueryTerms $query_terms */ public function checkSpelling(array $query_terms) { $config = Config::getInstance(); $id = $config->getConfig("SUMMON_ID", false); $key = $config->getConfig("SUMMON_KEY", false); $suggestion = new Suggestion(); if ($id != null && $key != null) { $client = new SummonClient($id, $key, Factory::getHttpClient()); // @todo: see if we can't collapse multiple terms into a single spellcheck query foreach ($query_terms as $term_original) { $term = clone $term_original; $query = $term->phrase; $query = urlencode(trim($query)); $correction = null; // get spell suggestion try { $correction = $client->checkSpelling($query); } catch (\Exception $e) { throw $e; // @todo: remove after testing trigger_error('Could not process spelling suggestion: ' . $e->getTraceAsString(), E_USER_WARNING); } // got one if ($correction != null) { $term->phrase = $correction; $suggestion->addTerm($term); } } } return $suggestion; }
/** * Check spelling * * @param QueryTerms[] $query_terms */ public function checkSpelling(array $query_terms) { $registry = Registry::getInstance(); $app_id = $registry->getConfig('BING_ID', true); $suggestion = new Suggestion(); $client = Factory::getHttpClient(); // @todo: see if we can't collapse multiple terms into a single spellcheck query foreach ($query_terms as $term) { $query = $term->phrase; $query = urlencode(trim($query)); $correction = null; // get spell suggestion try { $url = "http://api.search.live.net/xml.aspx?Appid={$app_id}&sources=spell&query={$query}"; $response = $client->getUrl($url); // process it $xml = Parser::convertToDOMDocument($response); // echo header("Content-Type: text/xml"); echo $xml->saveXML(); exit; $suggestion_node = $xml->getElementsByTagName('Value')->item(0); if ($suggestion_node != null) { $correction = $suggestion_node->nodeValue; } } catch (\Exception $e) { trigger_error('Could not process spelling suggestion: ' . $e->getTraceAsString(), E_USER_WARNING); } // got one if ($correction != null) { $term->phrase = $suggestion_node->nodeValue; $suggestion->addTerm($term); } } return $suggestion; }
/** * Check spelling * * @param QueryTerms[] $query_terms */ public function checkSpelling(array $query_terms) { return null; $registry = Registry::getInstance(); $suggestion = new Suggestion(); $consumer_key = $registry->getConfig('YAHOO_BOSS_CONSUMER_KEY', true); $consumer_secret = $registry->getConfig('YAHOO_BOSS_CONSUMER_SECRET', true); $client = new Client('http://yboss.yahooapis.com/'); $oauth = new OauthPlugin(array('consumer_key' => $consumer_key, 'consumer_secret' => $consumer_secret)); $client->addSubscriber($oauth); // @todo: see if we can't collapse multiple terms into a single spellcheck query foreach ($query_terms as $term) { $query = $term->phrase; $query = trim($query); $escaped_query = urlencode(urlencode($query)); // yes, double-escaped $correction = null; // get spell suggestion try { $response = $client->get('ysearch/spelling?q=' . $escaped_query . ' &format=xml')->send(); // process it $xml = simplexml_load_string($response->getBody()); // echo header("Content-Type: text/xml"); echo $xml->saveXML(); exit; $suggestions = $xml->xpath('//result/suggestion'); if (count($suggestions) > 0) { $correction = (string) $suggestions[0]; $correction = urldecode($correction); $correction = htmlspecialchars_decode($correction, ENT_QUOTES); } } catch (\Exception $e) { trigger_error('Could not process spelling suggestion: ' . $e->getTraceAsString(), E_USER_WARNING); } echo $correction; // got one if ($correction != null) { $term->phrase = $correction; $suggestion->addTerm($term); } } return $suggestion; }
public function addSpellingLink(Suggestion $spelling = null) { if ($spelling == null) { return; } // link to corrected spelling if ($spelling->hasSuggestions()) { $term = $spelling->getTerm(0); $params = $this->currentParams(); $params["field"] = $term->field; $params["query"] = $term->phrase; $spelling->url = $this->request->url_for($params); } }