Recommended:

  • phpclasses.org
  • jsclasses.org
  • jsmag.com
  • siteapps.com
  • View our reviews on Hot Scripts
  • JS Tutorial
  • scripts.com
  • securesignup.com




Recent Comments

Powered by Disqus




Back to articles

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

<?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

Back to method list

Method namenew word_solver()
Description

Create class instance

Set rules

Back to method list

Method nameset_rules($rules)
DescriptionProvide a set of rules using which words or anagrams will be generated
Input parameters

string $rules - set of rules for words or anagrams

Solving anagrams

Simply 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 position

Enclose 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 together

Enclose 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 letter

Provide 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

Back to method list

Method nameset_idiom($idiom)
DescriptionProvide and idiom to use in dictionary
Input parameters

string $idiom - idiom for generating valid words

Set alphabet

Back to method list

Method nameset_alphabet($abc)
Description0
Input parameters

array $abc - array with letters as elements

Use all letters

Back to method list

Method nameuse_all()
Description

Use all letters when generating words

Don't use all letters

Back to method list

Method namenot_all()
Description

May not use all letters when generating words

Get errors

Back to method list

Method nameget_errors()
DescriptionReturns an array of occured errors. Possible error values and explanations can be found in Possible error messagessection

Get anagrams

Back to method list

Method nameget_anagrams()
DescriptionReturns an array of anagrams generated from specified rules

Get words

Back to method list

Method nameget_words()
DescriptionReturns 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

Back to method list

Method namedictionary_init()
Description

Initialize dictionary. Do anything you need to do before using dicionary

Check word

Back to method list

Method namedictionary_check($word)
DescriptionCheck 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

Back to method list

Method namedictionary_test($string)
DescriptionCheck 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 textMeaningSolution
No valid rules providedYou haven't provided any valid rule to generate wordsProvide a valid rule using set_rule method
Caught exception: Probably illegal idiomIncorrect idiom was providedCheck dictionary for supported idioms (depends on hosting settings)
Pspell isn't enabled. Please enable pspell or use get_anagram methodPSspell is disabledEnable PSpell, use custom dictionary or generate anagrams without checking for validity of word
Dictionary isn't initializedCouldn't initialaize dictionaryCheck 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

blog comments powered by Disqus