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?

7
  • 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, 2012 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, 2012 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, 2012 at 15:29
  • 1
    You could generate words like whole text: haykranen.nl/2008/09/21/markov
    – biziclop
    Jun 27, 2012 at 15:29
  • @biziclop - thanks, I will definitely check a look at that.
    – ionFish
    Jun 27, 2012 at 15:31

1 Answer 1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.