Hi all,

here is my solution. Its a rather stupid approach, but as many simple
and short solutions its better than i had expected.

It uses some gsubs to simplify the input text and surround each word
and punctuation by exactly one space character.

Scan is used to find occurrences of the last <order> words and
store the next words respectively. (even multiple times)

From this array one is choose randomly (which preserves the original
frequency of occurrence because the words are stored multiple times
in the array, increasing the chance of being picked)

Thats it. It works well for files of several MB and gets even faster
if the order is higher.

usage: quiz74.rb <order> [input files]
--------------------------------------------------------------------
before, line, order = ['.'], '', ARGV.shift.to_i

txt = ARGF.read.tr('"/*\\()[]', ' ').downcase
txt = txt.gsub(/[^'\w]/, ' \0 ').gsub(/\s+/, ' ')

500.times do
  ary = txt.scan(/ #{before.join(' ')} (\S+)/).transpose.first
  before << ary[rand(ary.size)]
  before.shift if before.length > order
  (puts line; line = '') if line.length > 50 &&  /\w/ =~ before.last
  line << ( /\w/ =~ before.last && !line.empty? ? ' ' : '') << before.last
end
--------------------------------------------------------------------

cheers

Simon