PHP Classes

File: fwphp/glomodul/z_examples/book_video/03steinmetz_2008/ch05_txt_html/suggest.php

Recommend this page to a friend!
  Classes of Slavko Srakocic  >  B12 PHP FW  >  fwphp/glomodul/z_examples/book_video/03steinmetz_2008/ch05_txt_html/suggest.php  >  Download  
File: fwphp/glomodul/z_examples/book_video/03steinmetz_2008/ch05_txt_html/suggest.php
Role: Auxiliary script
Content type: text/plain
Description: Auxiliary script
Class: B12 PHP FW
Manage database records with a PDO CRUD interface
Author: By
Last change:
Date: 3 months ago
Size: 1,445 bytes
 

Contents

Class file image Download
<?php

function suggest_spelling($string) {
   
//suggest words for misspelled strings
   
$dict_config = pspell_config_create('en', 'american');
   
pspell_config_personal($dict_config, '/tmp/custom.pws');
   
pspell_config_ignore($dict_config, 2);
   
pspell_config_mode($dict_config, PSPELL_FAST);
   
$dictionary = pspell_new_config($dict_config);

   
// pspell_add_to_personal($dictionary, "doofburger");
    // pspell_save_wordlist($dictionary);

   
$dictionary = pspell_new_config($dict_config);
   
   
//Make sure we know whether we've suggested anything
   
$suggested_replacement = false;

   
//Now we've got it set up, break it up by words
   
$string = explode(' ', $string);
    foreach (
$string as $key=>$value) {
       
$value = trim(str_replace(',', '', $value));
        if ( (
strlen($value) > 3) && (! pspell_check($dictionary, $value)) ) {
           
//If we can't find a suggestion
           
$suggestion = pspell_suggest($dictionary, $value);
         
//Suggestions are case sensitive, so check first
           
if (strtolower($suggestion[0]) != strtolower($value)) {
               
$string[$key] = $suggestion[0];
               
$suggested_replacement = true;
            }
        }
    }

    if (
$suggested_replacement) {
       
//We had a suggestion, so return the data
       
return implode(' ', $string);
    } else {
        return
null;
    }
}

print
suggest_spelling("doofburger");

print
"\n";

?>

For more information send a message to info at phpclasses dot org.