Natural language generation in PHP

I woke up last night with a thought in my head: can PHP be used to generate random words that sound natural? (Like verses by Lorem ipsum).

  • Words that are one letter: 'a, e, i, o, u'
  • Double-letter words: any combination of vowels and consonants.
  • The maximum word length is six letters.

The goal is to fill in the space on website templates, instead, instead of "Lorem ipsum", or send test emails for specific PHP scripts to make sure mail () works.

But my thoughts on how this will work is that PHP generates random word lengths of 1-6 letters, and some “do not do this” rules, like “there are no two single-letter words next to each other,” or “there are no three vowels in a row” or “there are no three consonants in a row” and automatically add punctuation and capital letters between 4 and 8 words in a sentence.

Will it be possible at all, and if so, are there any previously existing classes or functions that I could implement?

+5
source share
1 answer

You can use a context-free grammar approach: http://en.wikipedia.org/wiki/Context-free_grammar

<word> := <vowel> | <consonant><remaining word following consonant> | <vowel><remaining word following vowel>
<vowel> := a|e|i|o|u
<consonant> := b|c|d|f|g|...
<word following vowel> := <consonant><remaining word following consonant>
...and so on

( C PHP), .

- PHP, :

+2

All Articles