Passengerで作ったしりとり


DreamhostがPassenger(mod_rails)を標準にしました、というので以前作ったぶたさんのしりとりを作ってみました。

http://shiri.bp7e.com/shiri/tori

プログラムは以下の2つだけ。こんな簡単にアイデアをホームページにできることに感動。Rails万歳!

#shiri_controller.rb
class ShiriController < ApplicationController
require 'kconv'
$KCODE='u'
def hello
end

def tori
@dic = []
File.open('buta014.dic') { |f|
 f.each do |word|
   word.strip!
   @dic.push(word)
 end
}

def getlastchar(str)
  komoji = {"ぁ"=>"あ","ぃ"=>"い","ぅ"=>"う","ぇ"=>"え","ぉ"=>"お","ゃ"=>"や",">ゅ"=>"ゆ","ょ"=>"よ","ゎ"=>"わ","っ"=>"つ"}
 last=str.split(//).last
 if komoji.has_key?(last) then
   last = komoji[last]
 end
 if (last == "ー" || last=="ん") then
   last = str.split(//)[-2]
   if komoji.has_key?(last) then
     last = komoji[last]
     last = komoji[last]
   end
 end
 return last
end

@size = @dic.size
def thinkword(before)
 word = @dic[rand(@size)] until /\A#{getlastchar(before)}\w*/=== word
 return word
end

@data = []
@data.push(@dic[rand(@size)])
p @data
while @data.size <= 5
  @data.push(thinkword(@data.last))
end
end
end
//tori.html.erb
<html>
<head>
<title>butasan_shiritori</title>
</head>
<body>
<% @data.each do |word| %>
<%= word %><br />
<% end %>
</body>
</html>