マルコフ過程なウホホイウッホ

「ぼくゴリラ ウホホイウッホ ウホホホホ ウッホホウッホ ウホホホホーイ」

第57回左千夫短歌大会の市長賞作品に感動が抑えきれないので、ウホホ変換スクリプトを組むことにした。

まず元作品から状態遷移図を作成。矢印のそばにある数字は遷移確率。

そのままコードに落とす。

class Array
  def any; self[rand(self.size)] end
end

module Uho
  MASTERPIECE = 'ウホホイウッホ ウホホホホ ウッホホウッホ ウホホホホーイ'
  DEFAULT_CHAR = ''

  class Converter
    def initialize(masterpiece=MASTERPIECE)
      @char_table = {}
      masterpiece.split(' ').each do |sentence|
        chars = sentence.split('')
        prev_char = chars.shift
        chars.each do |char|
          add_entry(prev_char, char)
          prev_char = char
        end
        add_entry(prev_char, nil)
      end
    end

    def add_entry(prev_char, char)
      (@char_table[prev_char] ||= []) << char
    end

    def generate(min_size=1, start_char=DEFAULT_CHAR)
      ret = ''
      while ret.size < min_size
        char = start_char
        while char
          ret += char.to_s
          char = @char_table[char].any
        end
        ret += ' '
      end
      ret.strip
    end

    def convert(sentences, separators=/([  .,、。「」()()\n\t]+)/)
      sentences.split(separators).inject('') do |ret, sentence|
        ret += (sentence =~ separators ? sentence : generate(sentence.size))
      end
    end
  end
end

if $0 == __FILE__ and not ARGV.empty?
  puts Uho::Converter.new.convert(ARGV.first)
end

では、この変換器を用いて朝日新聞の記事を変換してみよう。

少し長いが、賞の名前になっている歌人伊藤左千夫、この作品に惜しくも及ばなかった賞のない応募者たち、菱木・ウッホホ・俊輔君(17)ら三者三様の孤独を感じてほしい。

「ウッホホーイ」ウッホホホウッホ

ウッホホホホホホホホ ウッホホホウッホホホホ(ウホホホーイウッホホホホ)ウホホホホイウホ、ウッホホホウッホ ウッホホホホホホホホイ ウッホホーイウホ(ウッホホホ)ウッホホ ホイ、ウホイウッホホホホホホホホホーイウッホーイウッホホホ。ウッホホホ ウホホウッホ ウッホホホ ウホホ、ウホイウッホ ウッホウッホホ ウッホ ウホーイウホ。ウホホホホホイウッホホホ ウッホホホ ウッホホホホホホホウホホ ウホホホウッホホホホホホーイ(ウホ)ウッホホホ ウホウッホ ウホホホホ。

 「ウホホウホ ウッホホーイウッホーイ ウホホホホ ウホウッホ ウホ ウホホホホホホホホ」

 ウホホホイウッホホホ、ウホウホホーイ ウッホホホホ ウッホホホホイウッホ、「ウッホホホウッホホホホホ ウッホウホホ ウッホ ウホーイ」ウッホ、ウッホホホホ ウホホ ウッホウ ホーイウッホホホーイ。ウッホホホホホホ ウッホホウッホホホイ「ウホ」ウホホホホーイ「ウホホホ」ウホ ウホホイ、ウホ ウホホホホホホウホホホホ ウッホーイウホイウホホウホ。ウッホホ ウホホホ ウホ ウホホ ウホホホーイウホホ。ウッホ ウホーイ ウホホ(ウホ)ウホイ「ウホホホホホホイウホイ ウッホホ ウホホホウホホホーイ」ウホホホホホーイウッホホホホ。

 ウッホホ ウッホ ウッホホウホホホ、ウッホホホホホ。ウッホイウッホホホイ ウホ「ウッホホホホホホホホーイウッホホ、ウッホホ。ウッホホウッホホーイ ウッホホホホホホホホ、ウッホーイ ウッホウホイウホホ」。ウホホ ウッホ ウホホホホホホホホホホホホ、ウホウホホ ウホホホホーイ。ウホホ「ウッホイ ウッホ ウホホホホウッホホ ウッホホーイウホウッホウ ッホホホホーイウッホホ」ウホホイ ウッホ。

 ウッホホ ウッホホーイ ウッホ ウッホホホホホホホイウホホ。ウッホーイウッホホホホホ「ウホホウッホホホ ウホ ウッホ ウッホーイ、ウッホホホ ウホホーイウホ ウホイ」ウッホホホホホホウッホーイ。(ウッホーイウホホホウッホホホイ)