2章 その1

集合知プログラミング
これをRubyにする


元データを作っておく
p.8より
recommendations.rb

class Critics
  def users
    return @users
  end
  
  def initialize
    @users = { 
      'Lisa Rose' => {
        'Lady in the Water' => 2.5,
        'Snake on a Plane' => 3.5,
        'Just My Luck' => 3.0,
        'Superman Returns' => 3.5,
        'You, Me and Dupree' => 2.5,
        'The Night Listener'=>3.0
      },
      'Gene Seymour' => { 
        'Lady in the Water' => 3.0,
        'Snake on a Plane' => 3.5,
        'Just My Luck' => 1.5,
        'Superman Returns' => 5.0,
        'The Night Listener' => 3.0,
        'You, Me and Dupree' => 3.5
      },
      'Michael Phillips' => { 
        'Lady in the Water' => 2.5,
        'Snake on a Plane' => 3.0,
        'Superman Returns' => 3.5,
        'The Night Listener' => 4.0
      },
      'Claudia Puig' => { 
        'Snake on a Plane' => 3.5,
        'Just My Luck' => 3.0,
        'The Night Listener' => 4.5,
        'Superman Returns' => 4.0,
        'You, Me and Dupree' => 2.5
      },
      'Mick LaSalle' => { 
        'Lady in the Water' => 3.0,
        'Snake on a Plane' => 4.0,
        'Just My Luck' => 2.0,
        'Superman Returns' => 3.0,
        'The Night Listener' => 3.0,
        'You, Me and Dupree' => 2.0
      },
      'Jack Matthews' => { 
        'Lady in the Water' => 3.0,
        'Snake on a Plane' =>4.0,
        'The Night Listener' => 3.0,
        'Superman Returns' => 5.0,
        'You, Me and Dupree' => 3.5
      },
      'Toby' => { 
        'Snake on a Plane' => 4.5,
        'You, Me and Dupree' => 1.0,
        'Superman Returns' => 4.0
      }
    }
  end
  
end

p.9より
irbでrecommendation.rbを読み込んで対話式にプログラムを実行

irb -r recommendations.rb


p.9より

>> c = Critics.new
=> #<Critics:0x69f690 @users={"Jack Matthews"=>{"The Night Listener"=>3.0, "Superman Returns"=>5.0, "Lady in the Water"=>3.0, "Snake on a Plane"=>4.0, "You, Me and Dupree"=>3.5}, "Gene Seymour"=>{"The Night Listener"=>3.0, "Superman Returns"=>5.0, "Lady in the Water"=>3.0, "Snake on a Plane"=>3.5, "You, Me and Dupree"=>3.5, "Just My Luck"=>1.5}, "Mick LaSalle"=>{"The Night Listener"=>3.0, "Superman Returns"=>3.0, "Lady in the Water"=>3.0, "Snake on a Plane"=>4.0, "You, Me and Dupree"=>2.0, "Just My Luck"=>2.0}, "Toby"=>{"Superman Returns"=>4.0, "Snake on a Plane"=>4.5, "You, Me and Dupree"=>1.0}, "Claudia Puig"=>{"The Night Listener"=>4.5, "Superman Returns"=>4.0, "Snake on a Plane"=>3.5, "You, Me and Dupree"=>2.5, "Just My Luck"=>3.0}, "Lisa Rose"=>{"The Night Listener"=>3.0, "Superman Returns"=>3.5, "Lady in the Water"=>2.5, "Snake on a Plane"=>3.5, "You, Me and Dupree"=>2.5, "Just My Luck"=>3.0}, "Michael Phillips"=>{"The Night Listener"=>4.0, "Superman Returns"=>3.5, "Lady in the Water"=>2.5, "Snake on a Plane"=>3.0}}>
>> c.users['Lisa Rose']['Lady in the Water']
=> 2.5
>> c.users['Toby']['Snake on a Plane'] = 4.5
=> 4.5
>> c.users['Toby']
=> {"Superman Returns"=>4.0, "Snake on a Plane"=>4.5, "You, Me and Dupree"=>1.0}


平面上にプロットした時趣向が近い人同士を距離で表す。なつかしい三平方の定理を使う
p.10より

>> Math::sqrt((5-4)*(5-4)+(4-1)*(4-1))
=> 3.16227766016838


このままでは、重なる点の時0になってしまうので分子分母に1を足す

>> 1/(1+Math::sqrt((5-4)*(5-4)+(4-1)*(4-1)))
=> 0.240253073352042

0~1の間の値が返る。値が1に近いほど趣味が同じ。