Index: trunk/extensions/SphinxSearch/SphinxMWSearch.php |
— | — | @@ -210,8 +210,10 @@ |
211 | 211 | $this->mSuggestion = ''; |
212 | 212 | if ( $wgSphinxSuggestMode == 'enchant' ) { |
213 | 213 | $this->suggestWithEnchant(); |
214 | | - } else { |
| 214 | + } elseif ( $wgSphinxSuggestMode == 'soundex' ) { |
215 | 215 | $this->suggestWithSoundex(); |
| 216 | + } elseif ( $wgSphinxSuggestMode == 'aspell' ) { |
| 217 | + $this->suggestWithAspell(); |
216 | 218 | } |
217 | 219 | if ($this->mSuggestion) { |
218 | 220 | return true; |
— | — | @@ -232,7 +234,6 @@ |
233 | 235 | $suggestion_found = false; |
234 | 236 | $full_suggestion = ''; |
235 | 237 | foreach ( $this->mTerms as $word ) { |
236 | | - $suggestions = array(); |
237 | 238 | if ( !enchant_dict_check($dict, $word) ) { |
238 | 239 | $suggestions = enchant_dict_suggest($dict, $word); |
239 | 240 | while ( count( $suggestions ) ) { |
— | — | @@ -280,6 +281,56 @@ |
281 | 282 | } |
282 | 283 | } |
283 | 284 | |
| 285 | + function suggestWithAspell() { |
| 286 | + global $wgUser, $wgSphinxSearchPersonalDictionary, $wgSphinxSearchAspellPath; |
| 287 | + |
| 288 | + // aspell will only return mis-spelled words, so remember all here |
| 289 | + $words = $this->mTerms; |
| 290 | + $word_suggestions = array(); |
| 291 | + foreach ( $words as $word ) { |
| 292 | + $word_suggestions[ $word ] = $word; |
| 293 | + } |
| 294 | + |
| 295 | + // prepare the system call with optional dictionary |
| 296 | + $aspellcommand = 'echo ' . escapeshellarg( join( ' ', $words ) ) . |
| 297 | + ' | ' . escapeshellarg( $wgSphinxSearchAspellPath ) . |
| 298 | + ' -a --ignore-accents --ignore-case'; |
| 299 | + if ( $wgUser ) { |
| 300 | + $aspellcommand .= ' --lang=' . $wgUser->getDefaultOption( 'language' ); |
| 301 | + } |
| 302 | + if ( $wgSphinxSearchPersonalDictionary ) { |
| 303 | + $aspellcommand .= ' --home-dir=' . dirname( $wgSphinxSearchPersonalDictionary ); |
| 304 | + $aspellcommand .= ' -p ' . basename( $wgSphinxSearchPersonalDictionary ); |
| 305 | + } |
| 306 | + |
| 307 | + // run aspell |
| 308 | + $shell_return = shell_exec( $aspellcommand ); |
| 309 | + |
| 310 | + // parse return line by line |
| 311 | + $returnarray = explode( "\n", $shell_return ); |
| 312 | + $suggestion_needed = false; |
| 313 | + foreach ( $returnarray as $key => $value ) { |
| 314 | + // lines with suggestions start with & |
| 315 | + if ( $value[0] === '&' ) { |
| 316 | + $correction = explode( ' ', $value ); |
| 317 | + $word = $correction[ 1 ]; |
| 318 | + $suggestions = substr( $value, strpos( $value, ':' ) + 2 ); |
| 319 | + $suggestions = explode( ', ', $suggestions ); |
| 320 | + if ( count( $suggestions ) ) { |
| 321 | + $guess = array_shift( $suggestions ); |
| 322 | + if ( strtolower( $word ) != strtolower( $guess ) ) { |
| 323 | + $word_suggestions[ $word ] = $guess; |
| 324 | + $suggestion_needed = true; |
| 325 | + } |
| 326 | + } |
| 327 | + } |
| 328 | + } |
| 329 | + |
| 330 | + if ( $suggestion_needed ) { |
| 331 | + $this->mSuggestion = join( ' ', $word_suggestions ); |
| 332 | + } |
| 333 | + } |
| 334 | + |
284 | 335 | /** |
285 | 336 | * @return String: suggested query, null if none |
286 | 337 | */ |
Index: trunk/extensions/SphinxSearch/SphinxSearch.php |
— | — | @@ -25,20 +25,19 @@ |
26 | 26 | |
27 | 27 | # To completely disable the default search and replace it with SphinxSearch, |
28 | 28 | # set this BEFORE including SphinxSearch.php in LocalSettings.php |
29 | | -# $wgSearchType = 'SphinxSearch'; |
| 29 | +# $wgSearchType = 'SphinxMWSearch'; |
30 | 30 | |
31 | | -# prior to version 0.8.0 there was a SphinxSearch search type |
| 31 | +# Prior to version 0.8.0 there was a SphinxSearch search type |
32 | 32 | if ( $wgSearchType == 'SphinxSearch' ) { |
33 | 33 | $wgSearchType == 'SphinxMWSearch'; |
34 | 34 | } |
35 | 35 | |
36 | | -# To use the new approach (added in 0.7.2) set it to SphinxMWSearch |
37 | 36 | if ( $wgSearchType == 'SphinxMWSearch' ) { |
38 | 37 | $wgAutoloadClasses['SphinxMWSearch'] = $dir . 'SphinxMWSearch.php'; |
39 | 38 | $wgDisableSearchUpdate = true; |
40 | 39 | } |
41 | 40 | |
42 | | -# this assumes you have copied sphinxapi.php from your Sphinx |
| 41 | +# This assumes you have copied sphinxapi.php from your Sphinx |
43 | 42 | # installation folder to your SphinxSearch extension folder |
44 | 43 | # not needed if you install http://pecl.php.net/package/sphinx |
45 | 44 | if ( !class_exists( 'SphinxClient' ) ) { |
— | — | @@ -75,23 +74,16 @@ |
76 | 75 | # Set to true to use MW's default search snippets and highlighting |
77 | 76 | $wgSphinxSearchMWHighlighter = false; |
78 | 77 | |
79 | | -# ######################################################### |
80 | | -# Use Aspell to suggest possible misspellings. This can be provided via |
81 | | -# PHP pspell module (http://www.php.net/manual/en/ref.pspell.php) |
82 | | -# or command line insterface to ASpell |
| 78 | +# Should the suggestion (Did you mean?) mode be enabled? |
| 79 | +# Possible values: enchant, soundex, aspell |
| 80 | +$wgSphinxSuggestMode = ''; |
83 | 81 | |
84 | | -# Should the suggestion mode be enabled? |
85 | | -$wgSphinxSuggestMode = false; |
| 82 | +# Path to aspell, adjust value if not in the system path |
| 83 | +$wgSphinxSearchAspellPath = 'aspell'; |
86 | 84 | |
87 | | -# Path to personal dictionary (for example personal.en.pws.) Needed only if using a personal dictionary |
| 85 | +# Path to (optional) personal aspell dictionary |
88 | 86 | $wgSphinxSearchPersonalDictionary = ''; |
89 | 87 | |
90 | | -# Path to Aspell. Used only if your PHP does not have the pspell extension. |
91 | | -$wgSphinxSearchAspellPath = "/usr/bin/aspell"; |
92 | | - |
93 | | -# Path to aspell location and language data files. Do not set if not sure. |
94 | | -$wgSphinxSearchPspellDictionaryDir = ''; |
95 | | - |
96 | 88 | # How many matches searchd will keep in RAM while searching |
97 | 89 | $wgSphinxSearch_maxmatches = 1000; |
98 | 90 | |