第10回北海道開発オフ:@dabesaを改変

当初は単純なマルコフ連鎖、昨年からDBを使ったマルコフ連鎖で動いていた@dabesa。@dabesaの稼働場所を自宅サーバに移転したのを契機に、また単純マルコフ連鎖に戻した。TL.txtはfriend_timelineを別のスクリプトで取得している。3文字確認するだけの、本当に単純な仕組みなので、改善は今後検討する。


#! /usr/bin/ruby -Ku

require 'rubygems'
gem 'twitter4r'
require 'time'
require 'twitter'
require 'pit'

require 'uri'
require 'net/http'

require 'cgi'
class Hash
def to_http_str
self.map{|key,value| "#{key}=#{CGI.escape(value.to_s)}"}.join("&")
end
end

begin
lines = Array.new
post = Array.new
match = Array.new

l=0

open("TL.txt") do |file|
while line = file.gets
lines.push(line)
l = l+1
end
end

post = lines[rand(l)].split(//)

mess = post.first(2).to_s
word = post[1]

while word != nil do
post = Array.new
match = Array.new
list = Array.new
hash = Hash.new
l=0

lines.each do |line|
if /#{word}/ =~ line then
match.push(line)
l = l+1
end
end

i=0
post = match[rand(l)].split(//)
while i < post.length do
if /#{word}/ =~ post[i] then
hash = {"1"=>post[i+1],"2"=>post[i+2],"3"=>post[i+3]}
list.push(hash)
end
i=i+1
end

idx = rand(list.length)

if list[idx]["1"] != nil then
mess.concat(list[idx]["1"])
end
if list[idx]["2"] != nil then
mess.concat(list[idx]["2"])
end
if list[idx]["3"] != nil then
mess.concat(list[idx]["3"])
end
word = list[idx]["3"]

end

config = Pit.get("dabesa")
cl = Twitter::Client.new(config)
cl.status(:post,mess)

wmess = 'status=' + URI.escape(mess)
req = Net::HTTP::Post.new("/statuses/update.json?")
req.basic_auth(config["username"],config["password"])
Net::HTTP.start('api.wassr.jp',80) {|http|
response = http.request(req,wmess)
}

rescue
print "RuntimeError: ", $!, "\n";
end

このブログの人気の投稿

転職のお知らせ

40代の減量戦略 〜体重-14kg、体脂肪率-12%を実現した具体的な方法〜

45歳になりました