Jump to content

unscramble


serial

Recommended Posts

I once wanted to make an anagram solver (that unscrambles letters into words) but I never actually made it because I didn't have a word list.

 

This my idea (it worked on a small word list with 1000 words, there shouldn't be a problem to do it with a bigger list):

Take a word list. Make a table with the columns- word, sort.

For every word, enter the word into the "word" column, and in the sort column enter the word when it's letters are sorted alphabetically (see the function attached).

Now, every time you want to unscramble a bunch of letters, you pass then through the function attached, and search in the database for the words that their sort is the sort you made ("SELECT * FROM `table` WHERE sort='$sort'").

And that's it :)

 

The function:

<?php
//This requires php5

function word_sort($word)
{
$letters = str_split($word);
sort($letters);
return implode("",$letters);
}

?>

 

 

I hope I've helped :)

Orio.

Link to comment
https://forums.phpfreaks.com/topic/42012-unscramble/#findComment-203731
Share on other sites

<?php
$file = file('50states.txt'); // make sure your words are on seperate lines this returns an array
$scrambled = file('scrambled.txt'); // same deal as above

......


?>

 

After that I am not sure what you are asking, do you want to match the scrambled words to the one of the 50 state words?

 

--FrosT

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/42012-unscramble/#findComment-204087
Share on other sites

As I suggested...

 

<?php
//This requires php5
function word_sort($word)
{
$letters = str_split($word);
sort($letters);
return implode("",$letters);
}


$scrambled = file("scrambled.txt");
$unscrambled = file("states.txt");
foreach ($scrambled as $w)
{
foreach($unscrambled as $w2)
{
	if(word_sort(trim($w)) == word_sort(trim($w2)))
		echo $w." is ".$w2."<br>";
}
}

?>

 

 

Orio.

Link to comment
https://forums.phpfreaks.com/topic/42012-unscramble/#findComment-204206
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.