Fast Markov chains in ~20 lines of sh, grep, cut and Awk
> At first, mrkwords.sh will pick a random line from the model and pick the first word of the pair as the first word of our output message.<p>In my opinion, this is not the best way to do it. This will generate sentences that start with words that can never start sentences.<p>The best thing to do is to define a "sentinel" to go at the start and end of a sentence. For example, a "$" sign, or a NUL byte. Then to generate a sentence you set the initial state to your sentinel value, and generate forwards from there until you have reached another sentinel, at which point the sentence ends. When building your dictionary, you prepend (and append) a sentinel value to each sentence you add.<p>It might not sound like it, but this is a big improvement in the quality of sentence generation. For example, it will work out on its own that sentences start with capital letters and end with full stops, and it also tends to reduce the amount of obvious "fragment" sentences that get generated. An example of a "fragment" sentence would be, e.g. "own that sentences start with capital letters and" - all of the state transitions are valid, but the start and end of the sentence are not actually start and end states.<p>(I acknowledge that the linked project actually does <i>end</i> sentences at valid end states, but failing to do so is another common mistake of the same type).