The easiest solution? Guess and check. Generate a sentence of random words and reject those which don't pass mu
Sounds like a bogosort to me.
To sort a deck of cards:
- Shuffle deck randomly.
- If not sorted, go to 1.
Efficiency in the order of O(lots and lots and lots and....)
-Andrew.
| [reply] [Watch: Dir/Any] |
Close, but not quite. It depends on the ratio of "sensible" vs. "nonsensical" phrases, which in turn depends on how much leeway you're willing to give the program. If you want to mimic a rational human, then yes, you're right. But if you're willing to take something less stringent - say, your average irrational person, or worse, a suit - then you might be able to pull it off this way.
I did something like this with a music-composing program. I randomly generated tones, intervals, and durations, and filtered out the obvious dissonances and silly combinations. What was left was OK - not exactly music, but close to Musak. It would have passed muster in an elevator, but not at a concert.
| [reply] [Watch: Dir/Any] |
Er. That probably could have been more clear. Don't generate a long random sentance and then check it, build it up a piece at a time.
while(...)
{
my @sentence = ();
for (1..$random_sentence_length)
{
do
{
$next_word = random_word_generator();
} while(not grammar_correct(@sentance, $next_word);
push @sentance, $next_word;
}
print @sentance;
}
| [reply] [Watch: Dir/Any] [d/l] |
Check out Dave "Pragmatic" Thomas's blog entry Kata14 where you'll be challenged to generate sensical text by using "trigrams"...
I've tried his solution, and with a varied enough text base to feed it, it generates some impressive texts with so little effort... I guess you can improve it to check longer strings as well, but you'd end up with a Markov Chain (de|re)generator, which could be interesting on itself, but...
(suggestion: the Gutenberg Project is a great place to get good base texts for feeding the database :-)
Link: PragDave's Kata Fourteen
Good luck,
-- our $Perl6 is Fantastic;
| [reply] [Watch: Dir/Any] [d/l] |