Generate words from specified rules
Word solver class can generate words or anagrams using provided rules, for example, possible errors, placement of specific letter, which letters should be used together, or using any character from specified alphabet.
This class can be used to generate solutions to scrabble, crosswords, anagrams and other word games
Contents
- Download
- Example codes
- Examples in action
- Method list
- Extending class
- Possible error messages
- Latest changes
Download
Example codes
<?php //it can take some time set_time_limit(0); //declaring class instance include("./word_solver.php"); $ws = new word_solver(); //use all provided letters $ws->not_all(); //setting rules for words $ws->set_letters("a(cl)s[s]*e"); //getting results $arr = $ws->get_words(); echo "<pre>"; print_r($arr); echo "</pre>"; ?>
Examples in action
Example scripts provided with package in action:
Method list
- Create instance
- Set rules
- Set idiom
- Set alphabet
- Use all letters
- Don't use all letters
- Get errors
- Get anagrams
- Get words
Create instance
| Method name | new word_solver() |
| Description | Create class instance |
Set rules
| Method name | set_rules($rules) |
| Description | Provide a set of rules using which words or anagrams will be generated |
| Input parameters | string $rules - set of rules for words or anagrams Solving anagramsSimply provide all possible letters, for example $ws->set_rules("acelsss"), meaning, this word contains one "a" letter, one "c" letter, one "e" letter, one "l" letter and three "s" letters Some letters needs to be in specific positionEnclose that letter in [] brackets at specific position, $ws->set_rules("ce[a]lsss"), meaning, this word contains one "a" letter in a third position and one "c" letter, one "e" letter, one "l" letter, three "s" letters in any position except third Some letters needs to be togetherEnclose those letters in () brackets, $ws->set_rules("ce(las)ss"), meaning, this word contains one "las" string, one "c" letter, one "e" letter and two "s" letters in any positions Don't know the right letterProvide any char symbol "*", $ws->set_rules("acel*ss"), meaning, this word contains one "a" letter, one "c" letter, one "e" letter, one "l" letter, two "s" letters and one unknown letter, which could be any letter from alphabet provided by set_alphabet method. |
Set idiom
| Method name | set_idiom($idiom) |
| Description | Provide and idiom to use in dictionary |
| Input parameters | string $idiom - idiom for generating valid words |
Set alphabet
| Method name | set_alphabet($abc) |
| Description | 0 |
| Input parameters | array $abc - array with letters as elements |
Use all letters
| Method name | use_all() |
| Description | Use all letters when generating words |
Don't use all letters
| Method name | use_all() |
| Description | May not use all letters when generating words |
Get errors
| Method name | get_errors() |
| Description | Returns an array of occured errors. Possible error values and explanations can be found in Possible error messagessection |
Get anagrams
| Method name | get_anagrams() |
| Description | Returns an array of anagrams generated from specified rules |
Get words
| Method name | get_words() |
| Description | Returns an array of valid words generated from specified rules and checked by dictionary |
Extending class
If you want to use custom dicionary, then you'll have to extend this class and override three functions
Dictionary init
| Method name | dictionary_init() |
| Description | Initialize dictionary. Do anything you need to do before using dicionary |
Check word
| Method name | dictionary_check($word) |
| Description | Check if word is valid |
| Input parameters | string $word - word that needs to be checked |
| Returns | Return true if word is valid or false if invalid |
Test string
| Method name | dictionary_test($string) |
| Description | Check if it is possible to generate valid word using provided string as begining of the word |
| Input parameters | string $string - generated begining of the candidate word |
| Returns | Return true if word can be generated or false if it can't be. |
Possible error messages
List of all errors and meanings
| Error text | Meaning | Solution |
| No valid rules provided | You haven't provided any valid rule to generate words | Provide a valid rule using set_rule method |
| Caught exception: Probably illegal idiom | Incorrect idiom was provided | Check dictionary for supported idioms (depends on hosting settings) |
| Pspell isn't enabled. Please enable pspell or use get_anagram method | PSspell is disabled | Enable PSpell, use custom dictionary or generate anagrams without checking for validity of word |
| Dictionary isn't initialized | Couldn't initialaize dictionary | Check if PSpell is working correctly, or use custom dictionary |
Latest changes
- 13. May, 2012 - Fixed bug with unknown symbol "*" and added example for custom dictionary
You may also be interested in:
Powered by BlogAlike.com










