|
Posted by Dotan Cohen on 01/17/05 01:07
Hi all,
I am creating an app that will take any word and return an array with all the
possible ways of spelling it. I wrote a function that replaces letter(s) with
other letter(s) with similar sounds. For instance, 'pink' returns:
Array
(
[0] => pink
[1] => pinc
[2] => pynk
[3] => pync
[4] => peenk
[5] => peenc
)
The code looks like this:
<?php
print"<pre>\n";
if (!$band) { $band="Pink Floyd"; }
$band=strtolower($band);
$misspells = array($band);
function mispel ($was,$willbe) {
global $misspells;
global $newarr;
$newarr = array();
foreach ($misspells as $mis) {
$newarr[] = str_replace($was, $willbe, $mis);
}
foreach ($newarr as $new) {
$misspells[] = $new;
}
$misspells = array_unique ($misspells);
}
mispel('c','k');
mispel('c','s');
mispel('k','c');
mispel('s','c');
mispel('l','ll');
mispel('t','tt');
mispel('i','y');
mispel('i','ee');
mispel('f','ph');
$misspells = array_unique ($misspells);
print_r($misspells);
print"</pre>";
?>
I don't know how obvious it is from my code, but I'm fairly new to php and
programming in general (I certainly don't make my living this way!). My big
obstacle now is replacing letters that appear twice in the word, such as
kayak. Focusing only on the 'k's I get this array: kayak, cayac. But I need
kayak, kayac, cayak, cayac.
I have just spent the last four hours of my life getting all exited about
exploding and imploding and str*** and replacing needles in haystacks with
the php manual (and the comments!!!). But it proves far beyond my ability to
command my little compter to give me four possible spellings of kayak.
So I now turn to the popular php community for advise. What functions could be
reccommended to complete the task? Is there a better way of doing this? Was I
better off trying to do it with explode/implode or is the replace method
better? Neither of them worked for me... Maybe a completely different
approach? And ideas would be great, links to tutorials, code snippets,
sggestions as to aproaches, cups of coffee...
Thanks guys.
Dotan Cohen
Navigation:
[Reply to this message]
|