Strike
06-28-2002, 04:05 PM
The discussion started - http://www.coderforums.net/showthread.php?s=&threadid=633 - there, but I didn't want to hijack the thread, and since GnuVince (rightly) suggested that I post my proposed algorithm here, I thought it'd best go in this forum.
Anyway, the algorithm assumes (not unreasonably) that you have a word file to use as a benchmark of "what is a word?" for your anagrams. Anyway, the basic algorithm goes like this:
(this is assuming you work on an in-memory copy of the file, so if you are dealing with the actual file, make a copy of it at the beginning and use that copy for this)
1. You get the word/phrase to be made into other words
2. Step through each word in the word file (sequentially), doing the following:
3. If the length of the word is longer than the phrase, delete it from the word list (copy) and go to the next word (step 2)
4. If the first letter isn't even in the phrase, delete the word and go to the next (back to step 2)
5. Otherwise, iterate over the word, popping letters off a copy of the phrase string until you get one that doesn't match.
6. If you iterate over the whole word, keep it. Otherwise, delete it.
7. Go to the next word (back to step 2)
8. When all the words in the file are done, you will have a list of words that contain letters in your phrase. From here, I'll leave the algorithm up to you. It definitely shrinks the search space though. :)
Anyway, the algorithm assumes (not unreasonably) that you have a word file to use as a benchmark of "what is a word?" for your anagrams. Anyway, the basic algorithm goes like this:
(this is assuming you work on an in-memory copy of the file, so if you are dealing with the actual file, make a copy of it at the beginning and use that copy for this)
1. You get the word/phrase to be made into other words
2. Step through each word in the word file (sequentially), doing the following:
3. If the length of the word is longer than the phrase, delete it from the word list (copy) and go to the next word (step 2)
4. If the first letter isn't even in the phrase, delete the word and go to the next (back to step 2)
5. Otherwise, iterate over the word, popping letters off a copy of the phrase string until you get one that doesn't match.
6. If you iterate over the whole word, keep it. Otherwise, delete it.
7. Go to the next word (back to step 2)
8. When all the words in the file are done, you will have a list of words that contain letters in your phrase. From here, I'll leave the algorithm up to you. It definitely shrinks the search space though. :)