with modifications by Mikko Saari * @see https://www.relevanssi.com/ */ /* *************************************************************************** * Copyright (C) 2008 by Felipe Ribeiro * * felipernb@gmail.com * * http://www.feliperibeiro.com * * * * Permission is hereby granted, free of charge, to any person obtaining * * a copy of this software and associated documentation files (the * * "Software"), to deal in the Software without restriction, including * * without limitation the rights to use, copy, modify, merge, publish, * * distribute, sublicense, and/or sell copies of the Software, and to * * permit persons to whom the Software is furnished to do so, subject to * * the following conditions: * * * * The above copyright notice and this permission notice shall be * * included in all copies or substantial portions of the Software. * * * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.* * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR * * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * * OTHER DEALINGS IN THE SOFTWARE. * *************************************************************************** */ /** * Spell correcting feature. * * This class implements the Spell correcting feature, useful for the * "Did you mean" functionality on the search engine. Using a dictionary of * words extracted from the product catalog. * * Based on the concepts of Peter Norvig: http://norvig.com/spell-correct.html * * @author Felipe Ribeiro * @date September 18th, 2008 */ class Relevanssi_SpellCorrector { /** * Dictionary of words. * * @var array $dictionary Array of words, containing the dictionary. */ private static $dictionary; /** * Reads a text and extracts the list of words. * * @param string $text Source text for the words. * @return array The list of words */ private static function words( $text ) { $matches = array(); $text = relevanssi_strtolower( $text ); preg_match_all( '/[a-z]+/', $text, $matches ); return $matches[0]; } /** * Generates a list of possible "disturbances" on the passed string. * * @param string $word Word to disturb. * * @return array A list of variations. */ private static function edits1( $word ) { /** * Filters the alphabet used for Did you mean suggestions. * * In order to use the Did you mean suggestions with non-Latin alphabets * (or even European languages with a wider range of characters than * English), Relevanssi needs to be provided with the alphabet used. * * @param string A string containing the alphabet as a string of * characters without spaces. */ $alphabet = apply_filters( 'relevanssi_didyoumean_alphabet', 'abcdefghijklmnopqrstuvwxyzäöåü' ); $alphabet = preg_split( '/(? $max ) { $max = $value; $word = $c; } } return $word; } }