Skip to content

Instantly share code, notes, and snippets.

@sasamijp
Created July 3, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sasamijp/725842a4b55648c6ffda to your computer and use it in GitHub Desktop.
Save sasamijp/725842a4b55648c6ffda to your computer and use it in GitHub Desktop.
返答器
# -*- encoding:utf-8 -*-
require 'sequel'
require 'natto'
class Responder
def initialize
@db = Sequel.connect("sqlite://./pa.db")
@natto = Natto::MeCab.new
end
def respond(data)
su = @db[:sov].where(s: data[1]).select(:s,:o,:v).all
@db[:sov].insert(s: data[0], o: data[1], v: data[2])
return respond_normal(data.join('')) if su.empty?
translate_uhouho su.sample.values.join(" ")
end
def answer(q)
p q
return nil unless q[3] == '?' or q[3] == '?'
su = @db[:sov].where(s: q[0], o: q[1]).select(:s,:o,:v).all
return respond_normal(q.join('')) if su.empty?
translate_uhouho su.sample.values.join(' ')
end
def respond_normal(text)
if text.include?('!') or text.include?('!')
'ウホウホ'*rand(1..2)+'ウホホホ'*rand(1..2)+'!!'*rand(1..4)
else if text.include?('?') or text.include?('?') or text.include?('.')
'ウホ'+'...'*rand(1..3)
else
['ウホ','ウ','ホ'].shuffle.join('')
end
end
end
def translate_uhouho(text)
p text
ret = ''
@natto.parse(text) do |n|
next if n.is_eos?
if n.feature.split(',')[-2] == ''
ret += n.surface
else
ret += n.feature.split(',')[-2] if n.feature.split(',')[0] != '助詞'
end
end
ret.gsub(/(.*?)|\*/, '')
end
end
#r = Responder.new
#p r.answer %w(ゴリラ 頭 賢い ?)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment