5

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

  1. Words being single letter: 'a,e,i,o,u'
  2. Words being double letter: any combination of vowel and consonant.
  3. Maximum word length would be I think six letters.

The purpose would be to fill space on website templates with this instead of 'Lorem ipsum', or send test emails for certain PHP scripts to make sure mail() works.

But my thoughts on how it would work are that PHP would generate random length words, 1-6 letters each, with a few "don't do this" rules like "no two single-letter words next to each other" or "no three-vowels in a row" or "no three-consonants in a row" and automatically add punctuation and capitalization after between 4 and 8 words to a sentence.

Would this be at all possible, and if so, are there any pre-existing classes or functions I could implement?

ionFish
  • 1,004
  • 1
  • 8
  • 20
  • 1
    I don't see anything in your suggestion that wouldn't be possible. So, the answer is likely "Yes, it is possible" -- but is that really your question? – ametren Jun 27 '12 at 15:27
  • possible? sure. factible? i wouldnt be so sure. maybe it can be done with a simple prolog script(or another logical languaje) can solve your problem more easily. then you can call it from php.(just my toughs, do whatever you want) – Jarry Jun 27 '12 at 15:28
  • @ametren - Edited question, more looking for a place to start rather than start a blank PHP document and attempt to spill my mind on it. – ionFish Jun 27 '12 at 15:29
  • 1
    You could generate words like whole text: http://www.haykranen.nl/2008/09/21/markov/ – biziclop Jun 27 '12 at 15:29
  • @biziclop - thanks, I will definitely check a look at that. – ionFish Jun 27 '12 at 15:31
  • @biziclop That link plus a little context would definitely make a good answer. Logical model (Markov), implementation in the desired language (PHP) and an open license (MIT). It's all there, nice! – pixelistik Jun 27 '12 at 15:38
  • I just googled it, it was probably the first match :) I'm not an expert in Markov models. Googling QuarkXPress' Jabberwocky plugin also might worth a shot, which can generate lorem ipsum, klingon, english, etc. – biziclop Jun 27 '12 at 16:54

1 Answers1

2

You can take the 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

Implement that grammar in any procedural language (C and PHP included), then start generating words based on the grammar.

I don't know of any generic PHP parsing frameworks but you can look at best practices for writing them: Best practices for writing a programming language parser

Community
  • 1
  • 1
Rey Gonzales
  • 836
  • 1
  • 8
  • 17