anagram solving problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cmd
Forum Newbie
Posts: 2
Joined: Sat May 20, 2006 11:55 am

anagram solving problem

Post by cmd »

I am creating a php script to solve anagrams for a certain thing. The word list contains 41611 words.
The user enters a word, and the word's letter are arranged alphabetically:
user enters 'tpdcie', the letters are rearranged like this 'cdeipt'.
I have a huge switch statement, like this:

Code: Select all

function wordo($input)
{
switch ($input) {
case 'ddeenp': $rt = 'depend'; break;
case 'dddeeenp': $rt = 'depended'; break;
case 'ddeenps': $rt = 'depends'; break;
case 'cdeipt': $rt = 'depict'; break;
case 'cddeeipt': $rt = 'depicted'; break;
case 'cdeipst': $rt = 'depicts'; break;
case 'adeelnp': $rt = 'deplane'; break;
case 'addeelnp': $rt = 'deplaned'; break;
case 'adeelnps': $rt = 'deplanes'; break;
}
return $rt;
}
With 41611 different cases. This is where the problem begins. If I have fewer cases, like the first 20 (it can probably handle more than that), it works perfectly. But if I have the full switch statement in the php file or having the function in an include, Firefox tries to download the file, and I get no output. If I echo something at the start of the php file, I don't see it.

This is the main php file:

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>

<?php
//echo "start";
$t = $HTTP_GET_VARS[a];
if ($t != "") 
{
$retstr = "";
for ($i = 0; $i <= strlen($t)-1; $i++) {
  $retstr .= $t{$i}." ";
}
$t = $retstr;
$arr = explode(" ",$t);

sort($arr);
$ec = implode("", $arr);
//echo $ec;

include 'v4.inc';
echo wordo($ec);
}


?>
</body>
</html>
v4.inc:

Code: Select all

<?php
function wordo($input)
{
switch ($input) {
//.. lots of other cases..
case 'ddeenp': $rt = 'depend'; break;
case 'dddeeenp': $rt = 'depended'; break;
case 'ddeenps': $rt = 'depends'; break;
case 'cdeipt': $rt = 'depict'; break;
case 'cddeeipt': $rt = 'depicted'; break;
case 'cdeipst': $rt = 'depicts'; break;
case 'adeelnp': $rt = 'deplane'; break;
case 'addeelnp': $rt = 'deplaned'; break;
case 'adeelnps': $rt = 'deplanes'; break;
//.. lots of other cases..
}
return $rt;
}
?>
Can anybody give me a better method to use, or tell me why I can't use my current one? Or just tell me anything generally wrong. Thanks
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Re: anagram solving problem

Post by aerodromoi »

cmd wrote:I am creating a php script to solve anagrams for a certain thing. The word list contains 41611 words.
The user enters a word, and the word's letter are arranged alphabetically:
user enters 'tpdcie', the letters are rearranged like this 'cdeipt'.

Can anybody give me a better method to use, or tell me why I can't use my current one? Or just tell me anything generally wrong. Thanks
Another approach might be to compare the number of occurrences of the individual letters, starting with the most frequently used letter in the English language used in this word.

cdeipt 1e 1i 1t 1c 1d 1p

You could then search the database for all words with 6 letters and one "e", scanning these words for those with one "i" and so on...

aerodromoi

btw: 41611 words - that's what databases are made for :)
Last edited by aerodromoi on Sat May 20, 2006 1:47 pm, edited 1 time in total.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Ouch 8O

Store the list of words (originals) ina database. One problem solved right away ;)
Next, drop the whole switch() it's pointless because you perform the same operation on each word.

Instead make a little function:

Code: Select all

function reorder($word)
{
    $letter_count = strlen($word);
    $letters = array();
    for ($i = 0; $i<$letter_count; $i++)
    {
        $letters[] = $word{$i}; //Get the next letter
    }
    asort($letters); //Sort them alphabetically
    return implode('', $letters); //String them back together
}
You could change asort() for shuffle() to get a more random output.

Now you have that function you just need to call it for each word in the database to get it's anagram.
cmd
Forum Newbie
Posts: 2
Joined: Sat May 20, 2006 11:55 am

Post by cmd »

Thanks, I got it working using a database and the function you wrote.
Post Reply