phpSpellCheck



# code examples copyLeft totsp.com
# charlie collins
# temple of the screaming penguin
# http://screaming-penguin.com
# 02.08.2002
#
# please forgive the formatting, this code was written in valid GNU code style, but when pasted into this HTML file and
# used with pre tags it did not retain its formatting, its readable mind you, but it aint pretty.
#


Please see the accompanying article on screaming-penguin for background.





PHP spell.inc include function library

This file is used by the other php pages as an include. This file contains the functions to check spelling on words and add words to the custom dictionary.

This file creates a table of possible misspelled words and includes links to the spellCheck_process.php page with needed HTTP_GET variables for the needed functions (replace word and add word.)

See the other files, spellCheck.php and spellCheck_process.php for more info.

<?php
// spell.inc

function spellChecker($wordString)
{
$personal_pspell_dict="/opt/www/htdocs/screaming-penguin/includes/dictionaries/customTotSP.pws";
$pspell_config = pspell_config_create ("en","american");
pspell_config_mode($pspell_config,"PSPELL_FAST");
pspell_config_personal ($pspell_config, $personal_pspell_dict);
$spell = pspell_new_config ($pspell_config);

$words = split("[^[:alpha:]']+", $wordString);
$counter = 0;
echo "<table>\n";
foreach($words as $var)
    {
        
        if (!pspell_check($spell,$var))
            {                
                echo "<tr>\n";
				echo "<td><font face=\"arial, helvetica\"><b>Possible misspelled word:</b></font></td><td><font face=\"arial, helvetica\" color=\"red\"><b>$var</b></font></td><td><font face=\"arial, helvetica\" size=\"-1\"><a href=\"spellCheck_process.php?addWord=$var\">ADD THIS WORD</a>.</font></td>\n";
				echo "</tr>\n";	
                $suggestions = pspell_suggest ($spell, $var);
				echo "<tr>";
				echo "<td><font face=\"arial, helvetica\">possible spellings:</font></td>\n";
				echo "<td>";
                $counter = counter + 1;
                $tester = 0; 					 
                foreach ($suggestions as $suggestion)
                    {
					    $tester = $tester + 1;
					    if ($tester%4 != 0 )
					       {
				              echo "<a href=\"spellCheck_process.php?replaceWord=$var&replaceWordReplacement=$suggestion\"><font face=\"arial, helvetica\">$suggestion</font></a>   ";
					       }
					    else
					       {
					          echo "<a href=\"spellCheck_process.php?replaceWord=$var&replaceWordReplacement=$suggestion\"><font face=\"arial, helvetica\">$suggestion</font></a><br />";
					       }
                    }
				echo "</td>\n";
				echo "<td> </td>\n";
			    echo "</tr>\n";	 
				echo "<tr><td colspan=\"3\"> </td></tr>\n";		 				
            }
          
    }
if ($counter == 0)
{
echo "<tr><td colspan=\"3\"><font face=\"arial, helvetica\">no spelling errors detected</font></td></tr>\n";				
}
else
{
echo "<tr><td colspan=\"3\"> </td></tr>\n";				
}
echo "</table>\n";		
echo "<br />";
}


function addWord($word)
{
$personal_pspell_dict="/opt/www/htdocs/screaming-penguin/includes/dictionaries/customTotSP.pws";
$pspell_config = pspell_config_create ("en","american");
pspell_config_mode($pspell_config,"PSPELL_FAST");
pspell_config_personal ($pspell_config, $personal_pspell_dict);
$spell = pspell_new_config ($pspell_config);

  if (!file_exists($personal_pspell_dict))
      {
          // create personal dict if not present
          $fp = fopen ($personal_pspell_dict, "a");
          fclose($fp);
      }
pspell_add_to_personal($spell, $word);
pspell_save_wordlist($spell);
}

?>




PHP spellCheck.php
I dont have the entire spellCheck page here for brevity. Basically, you just submit the form fields you want checked, in my case $storybody (which I have in the session, but HTTP_POST_VARS would work fine as well) to the spellChecker() function.

The spellChecker funtion is included via include("spell.inc"); on the spellCheck.php page. This page displays HTTP_GET link choices to the user. These are not created here but rather are supplied by the table and links created on the spellChecker() function.

If a user submits either of the GET variables by clicking a link the spellCheck_process.php page is invoked with the GET values and it in turn changes or adds words to the original form field (again, in my case the $storybody value which is in the session, here also it would be possible to simply use the form values via HTTP_POST or HTTP_GET as opposed to the session, but it would require more forms with hidden fields and is more difficult to maintain, if you can it is advisable to use the session features of PHP.)

I also then provide a link back to the page the form was submitted on. When the user is done selecting to change or add words in the form field then they click the link to go back to the original form and submit it with all of the replaced words prepopulated (via the session.)

Below is a very rudimentary example of using spellChecker().


<?php

session_start();

include("spell.inc");
		  
spellChecker($HTTP_SESSION_VARS['storybody']);
	  
echo "<form name=\"spellCheckMod\" method=\"post\" action=\"originalFormPage.php\">";
echo "<input type=\"submit\" name=\"submit\" value=\"BACK to original form\" />";
echo "</form>";	 

?>




PHP spellCheck_process.php
Finally there is the spellCheck_process.php page that uses the HTTP_GET variables supplied to it via the links on the spellCheck.php page (which are created using the spellChecker() function from spell.inc.)

This page calls the addWord() function in spell.inc to add words to the custom dictionary and it has a simple php based replacement routine that is uses if the user has selected to replace a word by clicking on one of the suggestion links. (It could be argued that this replacement logic should be put in the spell.inc file, ie create a function in spell.inc to do this, but I decided not to since it is not really spelling related.)


<?php
session_start();

include("spell.inc");

// check referer and make sure it was one of approved spellCheck pages 
// (this is a rudimentary effort to defeat and direct DoS attack on the dictionary)
$expectedReferer1 = "spellCheck.php";
$referer = $HTTP_REFERER;
if (stristr($referer,$expectedReferer1))
{
  // add word using spell functions
  if (isset($HTTP_GET_VARS['addWord']))
    {
	  $addWord = $HTTP_GET_VARS['addWord'];
      addWord($addWord);
    }
  // replace word in session held storybody if selected
  elseif (isset($HTTP_GET_VARS['replaceWord']))
    {        
	  $replaceWord = $HTTP_GET_VARS['replaceWord'];
	  $replaceWordReplacement = $HTTP_GET_VARS['replaceWordReplacement'];
	  $storyBody = $HTTP_SESSION_VARS['storybody'];
	  if (stristr($storybody,$replaceWord))
	    {
		  $storybody = str_replace($replaceWord, $replaceWordReplacement, $storybody);
		}  		
     }
  // redirect here back to spelling page
  header("location: spellCheck.php");
}
else
{
// invalid request, cant come here directly
echo "INVALID REQUEST, this page cannot be accessed directly, sorry.";
}



?>