Jump to content

spell checking


glenelkins

Recommended Posts

Hi

 

I wrote the following code to spell check an inputted word against a database of words. If not found then the soundex of the word is used to find suggestions. Though in most cases there are alot of similar sounding words but I need to cut the list to display the more relevant ones..any ideas?

 

<?

if ( $_POST['action'] == "check" ) {

/* VARS START */
$c=0;
$correct = false;
$found = false;
$test_word = $_POST['word'];

mysql_connect ( "localhost", "hidden", "hidden" ) or die ( mysql_error() );
mysql_select_db ( "gedev_words" ) or die ( mysql_error() );

/* VARS END */

// Get the soundex key of the word
$test_word_meta = soundex ( $test_word );

// Debug
echo "soundex of test word: $test_word_meta <br>";

// First check to see if our word is in 
// the dictionary
$sql_query = "SELECT * FROM words";
$result = mysql_query ( $sql_query ) or die ( mysql_error() );

while ( $words = mysql_fetch_array ( $result ) ) {

	if ( $words['word'] == $test_word ) {

		// Debug
		echo "The word $test_word is found in our dictionary <br>";

		$found = true;

	}

}

if ( $found == true ) {

	// Debug
	echo "Spelling of $test_word is correct <br>";

	$correct = true;

} else {

	// Debug
	echo "Spelling of $test_word is incorrect or not in our dictionary. Here are some suggestions: <br>";

	$sql_query = "SELECT * FROM words";
	$result = mysql_query ( $sql_query ) or die ( mysql_error() );

	while ( $words = mysql_fetch_array ( $result ) ) {

		$tmp_meta = soundex ( $words['word'] );

		// Debug
		//echo "Word: " . $words['word'] . " soundex: $tmp_meta <br>"; 

		if ( $tmp_meta == $test_word_meta ) {

			$suggestions[$c] = $words['word'];
			$c++;

		}

	}

}

if ( $correct == false ) {

	if ( !empty ( $suggestions ) ) {

		foreach ($suggestions as $var => $suggestion ) {

				echo "$suggestion <br>";

		}

	} else {

		echo "We have no suggestions for this word";

	}

}

}
?>

<html>

<body>

<form action = "<? echo $_SERVER['PHP_SELF']; ?>" method = "POST">
	<input type = "text" name = "word" >
	<input type = "submit" value = "Check">
	<input type = "hidden" name = "action" value = "check">
</form>

</body>

Link to comment
https://forums.phpfreaks.com/topic/39007-spell-checking/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.