From: "Simon Kröger" Date: 2006-04-10T05:04:54+09:00 Subject: [QUIZ][SOLUTION] Markov Chains (#74) 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 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 [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