Created
August 6, 2014 17:21
-
-
Save sasamijp/67de96c5a246224d7e27 to your computer and use it in GitHub Desktop.
天海春香12号
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
require './wakati.rb' | |
require './db_manager.rb' | |
class Intelligence | |
def initialize | |
@wakati = Wakati.new | |
db = DbManager.new | |
@responds = db.read("amami.db") | |
@responds.delete_if{|v|v[:in_reply_to].length > 70} | |
end | |
def respond(str) | |
hitwords = [] | |
if @wakati.extract_noun_verb(str) != [] | |
input = @wakati.extract_noun_verb(str) | |
p input | |
responds = @responds.map{|v|{serif: v[:serif], in_reply_to: @wakati.extract_noun_verb(v[:in_reply_to])}} | |
hitwords << responds.select{ |value| words_match(value[:in_reply_to], input) != 0 } | |
end | |
input = @wakati.extract(str) | |
p input | |
return nil if input.nil? | |
responds = @responds.map{|v|{serif: v[:serif], in_reply_to: @wakati.extract(v[:in_reply_to])}} | |
hitwords << responds.select{ |value| words_match(value[:in_reply_to], input) != 0 } | |
sorted = hitwords.flatten.sort_by{ |value| words_match(value[:in_reply_to], input) } | |
sorted[0..4].sample[:serif] | |
end | |
private | |
def words_match(words1, words2) | |
return 0 if words1.length == 0 or words2.length == 0 | |
match = 0 | |
longer = [words1, words2].max_by{|v|v.length} | |
shorter = [words1, words2].delete_if{|v|v == longer}.flatten! | |
return 0 if shorter.nil? | |
longer.each do |wordA| | |
shorter.each do |wordB| | |
if wordA == wordB | |
match += wordA.length + wordB.length | |
end | |
end | |
end | |
(match/longer.length.to_f)*100.to_f | |
end | |
end | |
#i = Intelligence.new | |
#puts i.respond(gets.chomp) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment