public
Description: My take at converting the python code from the O'Reilly book by Toby Segaran to my language of choice
Homepage: http://www.oreilly.com/catalog/9780596529321/index.html
Clone URL: git://github.com/halfbyte/programming_collective_intelligence_in_ruby.git
housekeeping, refactoring
halfbyte (author)
Sat Apr 19 11:08:27 -0700 2008
commit  e78748975e86736b084532f5d2d9f3ed09f50cf8
tree    6d9f741038cc0b25f78a69a5dc32a7603e913825
parent  727e1bc3f043ed51a697890951aef6019dbb7191
...
2
3
4
 
 
 
 
5
6
7
...
2
3
4
5
6
7
8
9
10
11
0
@@ -2,6 +2,10 @@ This is my attemt to port the code examples from "Programming Collective Intelli
0
 
0
 I try not to stupidly port the stuff, but also try to use ruby idioms and best practices wherevever possible.
0
 
0
+h2. Changelog
0
+
0
+[19.04.2008] - Did some housekeeping. First of all, to make the code as accessible as possible, I converted the python shell stuff into unit tests. I also tried to set up a clever way to load monkey patches such as my Array.sum stuff. YMMV.
0
+
0
 h2. Status
0
 
0
 * Chapter 2
...
1
2
3
4
5
 
 
 
6
7
8
9
10
11
12
13
14
15
16
17
18
 
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 
35
36
37
38
39
40
41
42
43
 
 
 
 
 
44
45
46
47
48
49
50
 
51
...
 
 
 
 
 
1
2
3
4
 
 
 
 
5
 
 
 
 
 
 
 
6
7
8
9
10
11
 
12
13
14
15
 
 
 
 
 
 
16
17
 
 
 
 
 
 
 
 
18
19
20
21
22
23
24
25
26
 
 
 
27
28
0
@@ -1,50 +1,27 @@
0
-#!/usr/bin/env ruby
0
-require 'yaml'
0
-#
0
-# NOTE: I put the critics data into a yaml file to make it easier to base other language examples on it.
0
-#
0
+require '../lib/examples_helper'
0
+
0
+# TODO: This probably belongs into a module
0
 
0
-#
0
-# INFO: Both the code and the result of the original python version from the book are
0
-# broken, so don't expect the sim_distance function to return the result stated in the book.
0
-#
0
 def sim_distance(prefs, person1, person2)
0
- si = []
0
- prefs[person1].each do |k,v|
0
- if prefs[person2].has_key?(k)
0
- si << k
0
- end
0
- end
0
-
0
+ si = prefs[person1].keys & prefs[person2].keys
0
   return 0 if si.size == 0
0
   
0
   sum_of_squares = si.inject(0) do |sum, item|
0
     sum + ((prefs[person1][item] - prefs[person2][item]) ** 2)
0
   end
0
- puts sum_of_squares
0
   1/(1 + Math.sqrt(sum_of_squares))
0
 end
0
 
0
 def sim_pearson(prefs, person1, person2)
0
- si = []
0
- prefs[person1].each do |k,v|
0
- if prefs[person2].has_key?(k)
0
- si << k
0
- end
0
- end
0
+ si = prefs[person1].keys & prefs[person2].keys
0
   return 0 if si.size == 0
0
-
0
- sum1 = si.inject(0) { |sum, k| sum + prefs[person1][k] }
0
- sum2 = si.inject(0) { |sum, k| sum + prefs[person2][k] }
0
- sum1_sq = si.inject(0) { |sum, k| sum + (prefs[person1][k] ** 2) }
0
- sum2_sq = si.inject(0) { |sum, k| sum + (prefs[person2][k] ** 2) }
0
-
0
- p_sum = si.inject(0) { |sum, k| sum + (prefs[person1][k] * prefs[person2][k])}
0
-
0
+ sum1 = si.sum {|k| prefs[person1][k] }
0
+ sum2 = si.sum {|k| prefs[person2][k] }
0
+ sum1_sq = si.sum {|k| prefs[person1][k] ** 2 }
0
+ sum2_sq = si.sum {|k| prefs[person2][k] ** 2 }
0
+ p_sum = si.sum { |k| (prefs[person1][k] * prefs[person2][k])}
0
   num = p_sum - (sum1 * sum2 / si.size)
0
   den = Math.sqrt((sum1_sq - (sum1 ** 2) / si.size) * (sum2_sq-(sum2 ** 2)/ si.size))
0
   return 0 if den == 0
0
   num/den
0
-end
0
-
0
-@critics = YAML.load_file('recommendations.yml')
0
+end
0
\ No newline at end of file

Comments

    No one has commented yet.