Skip to content

Instantly share code, notes, and snippets.

@hanachin
Forked from kimihito/data.yml
Last active December 14, 2015 03:08
Show Gist options
  • Save hanachin/5018463 to your computer and use it in GitHub Desktop.
Save hanachin/5018463 to your computer and use it in GitHub Desktop.
Lisa_Rose:
Lady_in_the_Water: 2.5
Snakes_on_a_Plane : 3.5
Just_My_Luck: 3.0
Superman_Returns: 3.5
You_Me_and_Dupree: 2.5
The_Night_Litener: 3.0
Gene_Seymour:
Lady_in_the_Water: 3.0
Snakes_on_a_Plane : 3.5
Just_My_Luck: 1.5
Superman_Returns: 5.0
You_Me_and_Dupree: 3.5
The_Night_Litener: 3.0
Michael_Phillips:
Lady_in_the_Water: 2.5
Snakes_on_a_Plane : 3.0
Superman_Returns: 3.5
The_Night_Litener: 4.0
Claudia_Puig:
Snakes_on_a_Plane : 3.5
Just_My_Luck: 3.0
Superman_Returns: 4.0
You_Me_and_Dupree: 2.5
The_Night_Litener: 4.5
Mick_LaSalle:
Lady_in_the_Water: 3.0
Snakes_on_a_Plane : 4.0
Just_My_Luck: 2.0
Superman_Returns: 3.0
You_Me_and_Dupree: 2.0
The_Night_Litener: 3.0
Jack_Matthews:
Lady_in_the_Water: 3.0
Snakes_on_a_Plane : 4.0
Superman_Returns: 5.0
You_Me_and_Dupree: 3.5
The_Night_Litener: 3.0
Toby:
Snakes_on_a_Plane : 4.5
You_Me_and_Dupree: 1.0
Superman_Returns: 4.0
#!/usr/bin/env ruby
#-*- coding: utf-8 -*-
require "yaml"
data = YAML.load_file "data.yml"
def sim_distance(prefs, person1, person2)
both_reviewed_item = prefs[person1].keys & prefs[person2].keys
return 0 if both_reviewed_item.empty?
sum_of_squares = both_reviewed_item.map {|item|
(prefs[person1][item] - prefs[person2][item]) ** 2
}.inject(&:+)
1 / (1 + sum_of_squares)
end
puts sim_distance(data, "Lisa_Rose","Gene_Seymour")
# encoding: utf-8
require './recomendations'
describe 'sim_distance' do
context 'もしも評価の値が整数でも落ちないでほしいなぁ' do
let(:aさんの評価) { {コーラ: 2} }
let(:bさんの評価) { {コーラ: 5} }
let(:みんなの評価){ {a: aさんの評価, b: bさんの評価} }
subject {
sim_distance(みんなの評価, :a, :b)
}
# データベースには整数なんかかきません
xit { subject.should eq 0.1 }
end
context 'AさんとBさんが共に評価してるアイテムが1つある場合' do
let(:aさんの評価) { {コーラ: 2.0} }
let(:bさんの評価) { {コーラ: 5.0} }
let(:みんなの評価){ {a: aさんの評価, b: bさんの評価} }
subject {
sim_distance(みんなの評価, :a, :b)
}
it "aさんの評価2からbさんの評価5を引いて差は-3、-3×-3で9、1/(1+9)で0.1になること" do
subject.should eq 0.1
end
end
context 'AさんとBさんが共に評価してるアイテムが複数ある場合' do
let(:aさんの評価) { {コーラ: 2.0, ペプシ: 5.0} }
let(:bさんの評価) { {コーラ: 5.0, ペプシ: 1.0} }
let(:みんなの評価){ {a: aさんの評価, b: bさんの評価} }
subject {
sim_distance(みんなの評価, :a, :b)
}
it "1/(1+(コーラの差の平方9+ペプシの差の平方16))で小数点第5位でroundして0.03846なること" do
subject.round(5).should eq 0.03846
end
end
context 'AさんとBさんが共通して評価しているアイテムの差だけを考慮する' do
let(:aさんの評価) { {コーラ: 2.0, ペプシ: 5, 焼肉: 100.0} }
let(:bさんの評価) { {コーラ: 5.0, ペプシ: 1, 山菜: 100.0} }
let(:みんなの評価){ {a: aさんの評価, b: bさんの評価} }
subject {
sim_distance(みんなの評価, :a, :b)
}
it "1/(1+(コーラの差の平方9+ペプシの差の平方16))で小数点第5位でroundして0.03846なること" do
subject.round(5).should eq 0.03846
end
end
context 'AさんとBさんの評価がぴたりと一致しているとき' do
let(:aさんの評価) { {コーラ: 5.0, ペプシ: 10.0} }
let(:bさんの評価) { aさんの評価 }
let(:みんなの評価){ {a: aさんの評価, b: bさんの評価} }
subject {
sim_distance(みんなの評価, :a, :b)
}
it { should eq 1 }
end
context 'AさんとBさんが共に評価してるアイテムがない場合' do
let(:aさんの評価) { {コーラ: 1.0} }
let(:bさんの評価) { {ペプシ: 5.0} }
let(:みんなの評価){ {a: aさんの評価, b: bさんの評価} }
subject {
sim_distance(みんなの評価, :a, :b)
}
it { should be_zero }
end
end
@kimihito
Copy link

テストコードの参考になります。ありがとうございます。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment