実装したコードを中心に書いていきます。
・感情クラスの実装(抜粋)
class Emotion
MOOD_MIN = -15
MOOD_MAX = 15
MOOD_RECOVERY = 0.5
def initialize(dictionary)
@dictionary = dictionary
@mood = 0
end
def update(input)
# ユーザの入力と、パターン辞書をマッチングし、機嫌値を変動させる
@dictionary.pattern.each do |pattern_item|
if pattern_item.match(input)
adjust_mood(pattern_item.modify)
break
end
end
# 略
end
def adjust_mood(val)
# 略
end
attr_reader :mood
end
・回答パターンを管理するクラス(抜粋)
class PatternItem
SEPARATOR = /^((-?\d+)##)?(.*)$/
def initialize(pattern, phrases)
SEPARATOR =~ pattern
@modify, @pattern = $2.to_i, $3
@phrases = []
phrases.split('|').each do |phrase|
SEPARATOR =~ phrase
@phrases.push({'need'=>$2.to_i, 'phrase'=>$3})
end
end
def match(input)
return input.match(@pattern);
end
def choice(mood)
choices = []
@phrases.each do |phrase|
choices.push(phrase['phrase']) if suitable?(phrase['need'], mood)
end
return (choices.empty?)? nil : Utils.select_random(choices)
end
def suitable?(need, mood)
return true if need == 0
if need > 0
return mood > need
else
return mood < need
end
end
attr_reader :modify, :pattern, :phrases
end
この実装に合わせて辞書の形式を「{感情変動値}##パターン\t{必要感情値}##返答1|{必要感情値}##返答2」
という形式に変更しました。
これによって、感情値に合わせた回答を返す処理を実現できます。
また、自動的に感情値を調整する仕組みを入れることで、感情値が0(ニュートラルな状態)に向かって調整されるようになります。

0 コメント:
コメントを投稿