Advertisement
ozh

random_pronounceable_word

ozh
Oct 17th, 2012
977
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.64 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Generate random pronounceable words
  5.  *
  6.  * @param int $length Word length
  7.  * @return string Random word
  8.  */
  9. function random_pronounceable_word( $length = 6 ) {
  10.    
  11.     // consonant sounds
  12.     $cons = array(
  13.         // single consonants. Beware of Q, it's often awkward in words
  14.         'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm',
  15.         'n', 'p', 'r', 's', 't', 'v', 'w', 'x', 'z',
  16.         // possible combinations excluding those which cannot start a word
  17.         'pt', 'gl', 'gr', 'ch', 'ph', 'ps', 'sh', 'st', 'th', 'wh',
  18.     );
  19.    
  20.     // consonant combinations that cannot start a word
  21.     $cons_cant_start = array(
  22.         'ck', 'cm',
  23.         'dr', 'ds',
  24.         'ft',
  25.         'gh', 'gn',
  26.         'kr', 'ks',
  27.         'ls', 'lt', 'lr',
  28.         'mp', 'mt', 'ms',
  29.         'ng', 'ns',
  30.         'rd', 'rg', 'rs', 'rt',
  31.         'ss',
  32.         'ts', 'tch',
  33.     );
  34.    
  35.     // wovels
  36.     $vows = array(
  37.         // single vowels
  38.         'a', 'e', 'i', 'o', 'u', 'y',
  39.         // vowel combinations your language allows
  40.         'ee', 'oa', 'oo',
  41.     );
  42.    
  43.     // start by vowel or consonant ?
  44.     $current = ( mt_rand( 0, 1 ) == '0' ? 'cons' : 'vows' );
  45.    
  46.     $word = '';
  47.        
  48.     while( strlen( $word ) < $length ) {
  49.    
  50.         // After first letter, use all consonant combos
  51.         if( strlen( $word ) == 2 )
  52.             $cons = array_merge( $cons, $cons_cant_start );
  53.  
  54.          // random sign from either $cons or $vows
  55.         $rnd = ${$current}[ mt_rand( 0, count( ${$current} ) -1 ) ];
  56.        
  57.         // check if random sign fits in word length
  58.         if( strlen( $word . $rnd ) <= $length ) {
  59.             $word .= $rnd;
  60.             // alternate sounds
  61.             $current = ( $current == 'cons' ? 'vows' : 'cons' );
  62.         }
  63.     }
  64.    
  65.     return $word;
  66. }
  67.  
  68. // http://planetozh.com/blog/2012/10/generate-random-pronouceable-words/ for more info
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement