Location: PHPKode > scripts > lib_dictionary > lib_dictionary/example_single.php
<?php
/***************************************************************************
 *   example.php
 *   Copyright (c) 2007 David Frendin (hide@address.com)
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version. See the GNU General Public License
 *   for more details.
 *
 ***************************************************************************/

/***************************************************************************
 *   Example: Single word
 *   This example will do a more advanced spell-check of a single word, and if the word
 *   is wrongly spelled - suggest alterations ("did you mean <word>?")
 *
 ***************************************************************************/

/* ___[ Below is example code ]_____________________ */
require_once('mysql4.php');	//from phpbb2
require_once('lib_dictionary.php');

define('DICTIONARY_TABLE', 'dictionary');

//connect to db
$db = new sql_db('localhost', 'username', 'password', 'table', false);
if(!$db->db_connect_id)
{
	die("Could not connect to the database");
}

$dict = new dictclass();


//since we want to test the speed of the script, we use microtime
//call microtime a few times - since the first load of microtime requires extra processing
$t1 = microtime(true);
$t2 = microtime(true);
$t1 = microtime(true);
$t2 = microtime(true);
 

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Step 1 - Show user what we will be doing
$t1 = microtime(true);
$word = "retireed";			//what word are we going to check?
$correct = "retired";		//the correct word? (this just goes to the page output, it isnt really used)
$metap = metaphone($correct);
echo "Looking for word: <b>$word</b> (correct in this case is: $correct [$metap])<br>";


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Step 2 - Correct the word

if ($dict->does_word_exist($word) == -1)			//step 1 - does the word exist?
{
	$dict->load_dictionary($word, 1);				//step 2 - find dictionary words similar to $word
	if ($dict->is_loaded == false)
	{
		echo "> Im sorry, but i have no clue what <i>$word</i> might be..<p>";
	}
	else
	{
		$suggestions = $dict->checkhighest($word);	//step 3 - sort suggestions on what the correct word might be
		echo "Maybe you ment: \"".$suggestions[0]['word']."\"? (alt. ".$suggestions[1]['word'].", ".$suggestions[2]['word'].")<p>";
	}
}
else
{
	echo "The word is correctly spelled<p>";
}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Step 3 - Show user some statistics

$t2 = microtime(true);
echo "It took me <b>".sprintf('%.1f', ($t2 - $t1)*100 ) . "ms</b> (".sprintf('%.2f', ($t2 - $t1) )."s) to complete<br>";
echo "I compared your word with and sorted from ".count($dict->dictionary)." dictionary words (from 34 831 words in total)<br>";
echo "I am to ".sprintf('%.1f',$suggestions[0]['p'])."% sure that this was the word you were looking for";
die(); 

/* ___[ End of example code ]_____________________ */
?>
Return current item: lib_dictionary