# get dictionary from http://www.speech.cs.cmu.edu/cgi-bin/cmudict text = IO.read(ARGV[0]).split(" ") dict = IO.readlines('cmudict.0.7a') $syllables = {} dict.each {|line| word, pron = line.split(/\s+/, 2) syl = pron.gsub(/[^\d]/, '').length $syllables[word] = syl } senryu = [] count = 0 prev = '' text.each do |word| w = word.upcase.gsub(/\W/, '') syl = $syllables[w] if syl senryu.push([word, syl]) count += syl if count == 17 five = twelve = nil n = 0 senryu.each_with_index {|(w, i), ix| n += i five = ix if (n == 5) twelve = ix if (n == 12) } if five and twelve and senryu[-1][0] =~ /\.$/ and prev =~ /\.$/ [0..five, (five+1)..twelve, (twelve+1)..-1].each {|i| puts senryu[i].map(&:first).join(" ") } puts end end if count >= 17 prev, i = senryu.shift count -= i end else senryu = [] count = 0 prev = '' end end