Grammatically correct is a pretty fuzzy area, since grammar is not to strictly defined as one might think. What you really want here though, is a part-of-speech tagger
, and a markov chain.
Specifically a markov chain says that given a certain state (the first word for instance) there's just a certain chance of moving on to another state (the next word). They are relatively easy to write from scracth, but I've got a gist here in python that shows how they work if you want an example.
Once you've got that I would suggest a part-of-speech-based markov chain, combined with just checking to see if words are constructed from your desired character set. In general the algorithm would go something like this:
- Pick first word at random, checking that it is constructed solely from your desired set of characters
- Use the Markov Chain to predict the next word
- Check if that word is an appropriate part of speech, and that it conforms to the desired character set.
- If not, predict another word until it is the case.
- If so, then repeat starting at 2 to completion.
Hope that's what you're looking for. Let me know if you have any more questions.