Generate every possible word combination.

Discussion in 'Web Development and Programming' started by Sefrotox, May 26, 2011.

  1. Sefrotox

    Sefrotox Regular Member

    Joined:
    May 26, 2011
    Messages:
    16
    Likes Received:
    9
    This code will generate every possible 4 letter English word:
    PHP:
    // possible symbols in word
    $ch = array(
        
    'a',
        
    'b',
        
    'c',
        
    'd',
        
    'e',
        
    'f',
        
    'g',
        
    'h',
        
    'i',
        
    'j',
        
    'k',
        
    'l',
        
    'm',
        
    'n',
        
    'o',
        
    'p',
        
    'q',
        
    'r',
        
    's',
        
    't',
        
    'u',
        
    'v',
        
    'w',
        
    'x',
        
    'y',
        
    'z'
    );

    // word length
    $wordlength 4;

    $f fopen('_words_.txt''a');
    for(
    $i $i pow(count($ch), $wordlength) ; $i++)
    {
        
    $word '';
        
        for(
    $l $l $wordlength $l++)
        {
            if(!isset(
    $posc[$l]))
                
    $posc[$l] = 0;
            
            
    $word .= $ch[$posc[$l]];
        }
        
        
    fwrite($f$word."\t");
        
        for(
    $p $wordlength $p >= $p--)
        {
            
    $posc[$p]++;
            if(
    $posc[$p] >= count($ch))
                
    $posc[$p] = 0;
            else
                break;
        }
    }
    Note that the possible amount of combinations for a word is 26^wordlength Increasing the wordlength by a multiple of two, or 8 would generate not twice the amount but 26^(8-4) = 456976 TIMES as many words. That means with a four letter word there is 456976 combinations, but with an 8 letter word, there is 208827064576 combinations! So in other words, if you double the length, you square the amount possible.

    Warning: Yes, this does also generate those naughty words.... :p
     
    Brandon Sheley likes this.
  2. lucasbytegenius

    lucasbytegenius Regular Member

    Joined:
    May 8, 2011
    Messages:
    256
    Likes Received:
    50
    But won't it generate words that aren't words as well?
    Thanks for the share by the way :)
     
  3. Sefrotox

    Sefrotox Regular Member

    Joined:
    May 26, 2011
    Messages:
    16
    Likes Received:
    9
    Yes, I did not say it would not generate the other combinations too, but that all possible English 4 letter words would show up, at least. To generate only real English words, you would have to implement several different rules that I can't begin to imagine. Basically all this is doing is creating every possible combination with the given characters over 4 sets.

    Thanks by the way. :)
     
  4. Colour

    Colour Regular Member

    Joined:
    Jun 3, 2011
    Messages:
    3
    Likes Received:
    0
    It's a lot easier to create a script that will add letters together than to input all the words in the dictionary for him to do that he would have to edit the letters into actual words and have them just displayed since you can't create a script that knows English only one that can output it (can be taken from other sources)
     
  5. lucasbytegenius

    lucasbytegenius Regular Member

    Joined:
    May 8, 2011
    Messages:
    256
    Likes Received:
    50
    Alright. Thanks for clarifying :)
     
Similar Threads
Loading...

Share This Page