Created
May 30, 2009 08:20
-
-
Save riaf/120439 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| <?php | |
| /** | |
| * なんかよくわからんが PHP 版 | |
| */ | |
| $critics = array( | |
| 'Lisa Rose' => array( | |
| '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 Listener' => 3.0, | |
| ), | |
| 'Gene Seymour' => array( | |
| 'Lady in the Water' => 3.0, | |
| 'Snakes 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' => array( | |
| 'Lady in the Water' => 2.5, | |
| 'Snakes on a Plane' => 3.0, | |
| 'Superman Returns' => 3.5, | |
| 'The Night Listener' => 4.0, | |
| ), | |
| 'Claudia Puig' => array( | |
| 'Snakes 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' => array( | |
| 'Lady in the Water' => 3.0, | |
| 'Snakes 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' => array( | |
| 'Lady in the Water' => 3.0, | |
| 'Snakes on a Plane' => 4.0, | |
| 'The Night Listener' => 3.0, | |
| 'Superman Returns' => 5.0, | |
| 'You, Me and Dupree' => 3.5, | |
| ), | |
| 'Toby' => array( | |
| 'Snakes on a Plane' => 4.5, | |
| 'You, Me and Dupree' => 1.0, | |
| 'Superman Returns' => 4.0, | |
| ), | |
| ); | |
| class Critic | |
| { | |
| static public function sim_distance($prefs, $person1, $person2){ | |
| if(self::mutual_keys_count($prefs[$person1], $prefs[$person2]) == 0){ | |
| return 0.0; | |
| } | |
| $sum_of_squares = 0.0; | |
| foreach(array_keys($prefs[$person1]) as $k){ | |
| if(isset($prefs[$person2][$k])){ | |
| $sum_of_squares += pow($prefs[$person1][$k] - $prefs[$person2][$k], 2); | |
| } | |
| } | |
| return 1 / (1 + $sum_of_squares); | |
| } | |
| static public function sim_pearson($prefs, $person1, $person2){ | |
| $n = self::mutual_keys_count($prefs[$person1], $prefs[$person2]); | |
| if($n == 0){ | |
| return 0.0; | |
| } | |
| $sum1 = array_sum($prefs[$person1]); | |
| $sum2 = array_sum($prefs[$person2]); | |
| $sum1sq = self::sum_sq($prefs[$person1]); | |
| $sum2sq = self::sum_sq($prefs[$person2]); | |
| $p_sum = 0.0; | |
| foreach($prefs[$person1] as $k => $v){ | |
| if(isset($prefs[$person2][$k])) | |
| $p_sum += $v * $prefs[$person2][$k]; | |
| } | |
| $num = $p_sum - ($sum1 * $sum2 / $n); | |
| $den = sqrt(($sum1sq - (pow($sum1, 2) / $n)) * ($sum2sq - (pow($sum2, 2) / $n))); | |
| if($den == 0){ | |
| return 0.0; | |
| } | |
| return $num / $den; | |
| } | |
| static private function mutual_keys_count($arr1, $arr2){ | |
| $r = 0; | |
| $keys = array_keys($arr1); | |
| foreach(array_keys($arr2) as $k){ | |
| if(in_array($k, $keys)) $r++; | |
| } | |
| return $r; | |
| } | |
| static private function sum_sq($arr){ | |
| $ret = 0.0; | |
| foreach($arr as $v){ | |
| $ret += pow($v, 2); | |
| } | |
| return $ret; | |
| } | |
| } | |
| /* | |
| $r = Critic::sim_distance($critics, 'Lisa Rose', 'Gene Seymour'); | |
| var_dump($r); | |
| $r = Critic::sim_pearson($critics, 'Lisa Rose', 'Gene Seymour'); | |
| var_dump($r); | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment