From: Martin DeMello Date: 2009-11-17T20:58:55+09:00 Subject: Re: [QUIZ] Found Senryu (#224) --001636e0a5a5adc4b004788fd9fa Content-Type: text/plain; charset=ISO-8859-1 Here's my solution (code both inlined below and attached). It uses the cmu pronouncing dictionary directly, since I didn't know about Lingua::EN. The code is inefficient in theory, but runs fast enough :) Since there were way too many results (most of them uninteresting), I added the refinement that the senryu should be a complete sentence (the word just before it, and the last word, should both end with a full stop). I used Wodehouse's "Right Ho, Jeeves" [http://www.gutenberg.org/etext/10554] to test it; these are three of the more amusing ones it found: You can't get away from the facts. Somebody stole her from me at Cannes. "How? Could Lloyd George do it, could Winston do it, could Baldwin do it? No. "What?" I staggered, and the left pedal came up and caught me on the shin. # ------------------------------------------------------------------------------------------------------------------------------- # 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 end end --001636e0a5a5adc4b004788fd9fa Content-Type: application/x-ruby; name="senryu.rb" Content-Disposition: attachment; filename="senryu.rb" Content-Transfer-Encoding: base64 X-Attachment-Id: f_g24m2c2j0 IyBnZXQgZGljdGlvbmFyeSBmcm9tIGh0dHA6Ly93d3cuc3BlZWNoLmNzLmNtdS5lZHUvY2dpLWJp bi9jbXVkaWN0Cgp0ZXh0ID0gSU8ucmVhZChBUkdWWzBdKS5z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WxzZQogICAgc2Vucnl1ID0gW10KICAgIGNvdW50ID0gMAogICAgcHJldiA9ICcn CiAgZW5kCmVuZAo= --001636e0a5a5adc4b004788fd9fa--