プログラミング下手くそなので至らぬところもたくさんあるのですが、一応簡単なbotが出来ました。
ruby raprto_bot.rb (ログファイル)
で2秒毎に戯言を出力します。
エンコーディングのこととかよくわかんないので文字化けするかも。
#-*-encoding:utf-8-*- def learn(file) dic = {} line_number = 0 log = open(file) puts "ログファイルを読み込むです" log.each{|line| line_number += 1 if line_number % 1000 == 0 then puts line_number.to_s + "行目" end word = line.encode("UTF-8").split("") 3.times{ word.unshift("STX") word.push("ETX") } for num in 0..10000 do if word[num+3].nil? then break end gram = word[num,4] last = gram.pop if dic[gram] dic[gram].push(last) else dic[gram] = [] dic[gram].push(last) end end } puts "読み込み終わり" dic end def make_sentence(dic) inDic = dic sentence = ["STX","STX","STX"] while inDic[sentence.last(3)] != ["ETX","ETX","ETX"] ntc = inDic[sentence.last(3)] if ntc != nil then sentence.push(ntc.sample) else break end end length = sentence.length utterance = sentence[3,(length-6)] utterance.join end inFile = ARGV[0] dic = learn(inFile) while true post = make_sentence(dic) if !post.include?("@") && post.count("「") == post.count("」") puts post sleep(2) end end
ちょう頭の悪い実装に思えてきた。