function bench_enchant($words) { // TODO: check return values!!! echo "Bench Enchant: "; $tag = 'ru_RU'; $r = enchant_broker_init(); if (!enchant_broker_dict_exists($r, $tag)) { echo "{$tag} dict not supported by enchant\n"; return false; } $d = enchant_broker_request_dict($r, $tag); $not_found = 0; $b = microtime(true); foreach ($words as $word) { // if(false === enchant_dict_quick_check($d, $word/*, $sugg*/)) { // this cause segfault if (false === enchant_dict_check($d, $word)) { enchant_dict_suggest($d, $word); $not_found++; } } $e = microtime(true); printf("time = %0.2f sec, words per second = %0.2f, not found = %d\n", $e - $b, count($words) / ($e - $b), $not_found); enchant_broker_free_dict($d); enchant_broker_free($r); }
/** * Spellchecks an array of words. * * @param String $lang Selected language code (like en_US or de_DE). Shortcodes like "en" and "de" work with enchant >= 1.4.1 * @param Array $words Array of words to check. * @return Name/value object with arrays of suggestions. */ public function getSuggestions($lang, $words) { $suggestions = array(); $enchant = enchant_broker_init(); $config = $this->getConfig(); if (isset($config["enchant_dicts_path"])) { enchant_broker_set_dict_path($enchant, ENCHANT_MYSPELL, $config["enchant_dicts_path"]); enchant_broker_set_dict_path($enchant, ENCHANT_ISPELL, $config["enchant_dicts_path"]); } if (!enchant_broker_describe($enchant)) { throw new Exception("Enchant spellchecker not find any backends."); } $lang = $this->normalizeLangCode($enchant, $lang); if (enchant_broker_dict_exists($enchant, $lang)) { $dict = enchant_broker_request_dict($enchant, $lang); foreach ($words as $word) { if (!enchant_dict_check($dict, $word)) { $suggs = enchant_dict_suggest($dict, $word); if (!is_array($suggs)) { $suggs = array(); } $suggestions[$word] = $suggs; } } enchant_broker_free_dict($dict); enchant_broker_free($enchant); } else { enchant_broker_free($enchant); throw new Exception("Enchant spellchecker could not find dictionary for language: " . $lang); } return $suggestions; }
/** * Use enchant to get word suggestions * @param string $word * @return array */ public function suggest($word) { if (!enchant_dict_check($this->enchantBroker, $word)) { return enchant_dict_suggest($this->enchantBroker, $word); } else { return [$word]; } }
/** * Returns suggestions for a specific word. * * @param String $lang Selected language code (like en_US or de_DE). Shortcodes like "en" and "de" work with enchant >= 1.4.1 * @param String $word Specific word to get suggestions for. * @return Array of suggestions for the specified word. */ function &getSuggestions($lang, $word) { $r = enchant_broker_init(); $suggs = array(); if (enchant_broker_dict_exists($r, $lang)) { $d = enchant_broker_request_dict($r, $lang); $suggs = enchant_dict_suggest($d, $word); enchant_broker_free_dict($d); } else { } enchant_broker_free($r); return $suggs; }
/** * Returns suggestions for a specific word. * * @param String $lang Selected language code (like en_US or de_DE). Shortcodes like "en" and "de" work with enchant >= 1.4.1 * @param String $word Specific word to get suggestions for. * @return Array of suggestions for the specified word. */ function &getSuggestions($lang, $word) { $r = enchant_broker_init(); $suggs = array(); if (enchant_broker_dict_exists($r, $lang)) { $d = enchant_broker_request_dict($r, $lang); $suggs = enchant_dict_suggest($d, $word); enchant_broker_free_dict($d); } else { $this->throwError("Language not installed"); } enchant_broker_free($r); return $suggs; }
/** * Returns suggestions for a specific word. * * @param String $lang Selected language code (like en_US or de_DE). Shortcodes like "en" and "de" work with enchant >= 1.4.1 * @param String $word Specific word to get suggestions for. * @return Array of suggestions for the specified word. */ function &getSuggestions($lang, $word) { $r = enchant_broker_init(); if (enchant_broker_dict_exists($r, $lang)) { $d = enchant_broker_request_dict($r, $lang); $suggs = enchant_dict_suggest($d, $word); // enchant_dict_suggest() sometimes returns NULL if (!is_array($suggs)) { $suggs = array(); } enchant_broker_free_dict($d); } else { $suggs = array(); } enchant_broker_free($r); return $suggs; }
} else { $dict = enchant_broker_request_dict($enc, 'en_US'); } $search_result_info .= '<div class="search-suggestions">' . __('Did you mean:') . ' '; $word = strtok($keywords, " \t\n"); $keywords_suggest = array(); while ($word !== false) { // check if we are inside quote if (stripos($word, '"', 0) === true) { $search_result_info .= preg_replace('@[a-z]@i', '', $word); $word = str_replace('"', '', $word); } $wordcorrect = enchant_dict_check($dict, $word); if (!$wordcorrect) { $closest = null; $wordsuggest = enchant_dict_suggest($dict, $word); $shortest = -1; // loop through words to find the closest with levenshtein foreach ($wordsuggest as $wordsg) { $lev = levenshtein($word, $wordsg); if ($lev == 0) { $closest = $wordsg; $shortest = 0; break; } if ($lev <= $shortest || $shortest < 0) { // set the closest match, and shortest distance $closest = $wordsg; $shortest = $lev; } }
/** * Return a collection of suggestion corresponding a query * * @param string $query * @return ArrayCollection An array collection of SearchEngineSuggestion */ private function getSuggestions($query, SearchEngineOptions $options) { // First we split the query into simple words $words = explode(" ", $this->cleanupQuery(mb_strtolower($query))); $tmpWords = []; foreach ($words as $word) { if (trim($word) === '') { continue; } $tmpWords[] = $word; } $words = array_unique($tmpWords); $altVersions = []; foreach ($words as $word) { $altVersions[$word] = [$word]; } // As we got words, we look for alternate word for each of them if (function_exists('enchant_broker_init') && $options->getLocale()) { $broker = enchant_broker_init(); if (enchant_broker_dict_exists($broker, $options->getLocale())) { $dictionnary = enchant_broker_request_dict($broker, $options->getLocale()); foreach ($words as $word) { if (enchant_dict_check($dictionnary, $word) == false) { $suggs = array_merge(enchant_dict_suggest($dictionnary, $word)); } $altVersions[$word] = array_unique($suggs); } enchant_broker_free_dict($dictionnary); } enchant_broker_free($broker); } /** * @todo enhance the trigramm query, as it could be sent in one batch */ foreach ($altVersions as $word => $versions) { $altVersions[$word] = array_unique(array_merge($versions, $this->get_sugg_trigrams($word, $options))); } // We now build an array of all possibilities based on the original query $queries = [$query]; foreach ($altVersions as $word => $versions) { $tmp_queries = []; foreach ($versions as $version) { foreach ($queries as $alt_query) { $tmp_queries[] = $alt_query; $tmp_queries[] = str_replace($word, $version, $alt_query); } $tmp_queries[] = str_replace($word, $version, $query); } $queries = array_unique(array_merge($queries, $tmp_queries)); } $suggestions = []; $max_results = 0; foreach ($queries as $alt_query) { $results = $this->sphinx->Query($alt_query, $this->getQueryIndex($alt_query, $options)); if ($results !== false && isset($results['total_found'])) { if ($results['total_found'] > 0) { $max_results = max($max_results, (int) $results['total_found']); $suggestions[] = new SearchEngineSuggestion($query, $alt_query, (int) $results['total_found']); } } } usort($suggestions, ['self', 'suggestionsHitSorter']); $tmpSuggestions = new ArrayCollection(); foreach ($suggestions as $key => $suggestion) { if ($suggestion->getHits() < $max_results / 100) { continue; } $tmpSuggestions->add($suggestion); } return $tmpSuggestions; }
/** * Wiki-specific search suggestions using enchant library. * Use SphinxSearch_setup.php to create the dictionary */ function suggestWithEnchant() { if (!function_exists('enchant_broker_init')) { return; } $broker = enchant_broker_init(); enchant_broker_set_dict_path($broker, ENCHANT_MYSPELL, dirname( __FILE__ )); if ( enchant_broker_dict_exists( $broker, 'sphinx' ) ) { $dict = enchant_broker_request_dict( $broker, 'sphinx' ); $suggestion_found = false; $full_suggestion = ''; foreach ( $this->mTerms as $word ) { if ( !enchant_dict_check($dict, $word) ) { $suggestions = enchant_dict_suggest($dict, $word); while ( count( $suggestions ) ) { $candidate = array_shift( $suggestions ); if ( strtolower($candidate) != strtolower($word) ) { $word = $candidate; $suggestion_found = true; break; } } } $full_suggestion .= $word . ' '; } enchant_broker_free_dict( $dict ); if ($suggestion_found) { $this->mSuggestion = trim( $full_suggestion ); } } enchant_broker_free( $broker ); }
/** * @return array $listOfSuggestions */ public function suggestions($word) { return enchant_dict_suggest($this->_Dict, $word); }
/** * Returns suggestions for the specified word * * @see rcube_spellcheck_engine::get_words() */ function get_suggestions($word) { $this->init(); if (!$this->enchant_dictionary) { return array(); } $suggestions = enchant_dict_suggest($this->enchant_dictionary, $word); if (sizeof($suggestions) > self::MAX_SUGGESTIONS) { $suggestions = array_slice($suggestions, 0, self::MAX_SUGGESTIONS); } return is_array($suggestions) ? $suggestions : array(); }
/** * Return an array of suggestions if the word is wrongly spelled */ public function suggest($word) { wfProfileIn(__METHOD__); $ret = $this->isLoaded() ? (array) enchant_dict_suggest($this->dict, $word) : false; wfProfileOut(__METHOD__); return $ret; }
public function get_word_suggestions($word = NULL) { return enchant_dict_suggest($this->dictionary, $word); }
<?php $tag = 'en_US'; $r = enchant_broker_init(); $bprovides = enchant_broker_describe($r); echo "Current broker provides the following backend(s):\n"; print_r($bprovides); if (enchant_broker_dict_exists($r, $tag)) { $d = enchant_broker_request_dict($r, $tag); $dprovides = enchant_dict_describe($d); echo "dictionary {$tag} provides:\n"; $spellerrors = enchant_dict_check($d, "soong"); print_r($dprovides); echo "found {$spellerrors} spell errors\n"; if (spellerrors) { $suggs = enchant_dict_suggest($d, "soong"); echo "Suggestions for 'soong':"; print_r($suggs); } enchant_broker_free_dict($d); } else { } enchant_broker_free($r);