Back to menu
Item Recomendation 0 760

# item_recommender
## This is a simple Item Recommendation Class/Library using Euclidean distance Algorithm
=================================
You can extend it as much as you want.
================================

In mathematics, the Euclidean distance between two points in Euclidean space is the length of a line segment between the two points. It can be calculated from the Cartesian coordinates of the points using the Pythagorean theorem, therefore occasionally being called the Pythagorean distance. These names come from the ancient Greek mathematicians Euclid and Pythagoras, although Euclid did not represent distances as numbers, and the connection from the Pythagorean theorem to distance calculation was not made until the 18th century.

The distance between two objects that are not points is usually defined to be the smallest distance among pairs of points from the two objects. Formulas are known for computing distances between different types of objects, such as the distance from a point to a line. In advanced mathematics, the concept of distance has been generalized to abstract metric spaces, and other distances than Euclidean have been studied. In some applications in statistics and optimization, the square of the Euclidean distance is used instead of the distance itself.

sample_list.php page is used to create dataset
recommend_test.php is where you send user's rating data to get recommendation
recommend.php is page where all algorithm are written

Create sample_list.php

                            
                                
<?php

$dataset =  array(
                
    "phil" => array("my girl" => 2.5, 
                    "the god delusion" => 3.5,
                    "tweak" => 3, 
                    "the shack" => 4,
                    "the birds in my life" => 2.5,
                    "new moon" => 3.5),
    
    "sameer" => array("the last lecture" => 2.5, 
                    "the god delusion" => 3.5,
                      "the noble wilds" => 3, 
                      "the shack" => 3.5,
                      "the birds in my life" => 2.5, 
                      "new moon" => 1
                  ),
    
    "john" => array("chaos" => 5,"a thousand splendid suns" => 5, "the secret" => 3.5,
                    "tweak" => 1),
    
    "peter" => array("chaos" => 5, "php in action" => 3.5,"Rider man" => 2),
    
    "jill" => array("the last lecture" => 1.5, "the secret" => 2.5,
                    "the noble wilds" => 4, "the host: a novel" => 3.5,
                    "the world without end" => 2.5, "new moon" => 3.5),
    
    "bruce" => array("the last lecture" => 3, "the hollow" => 1.5,
                     "the noble wilds" => 3, "the shack" => 3.5,
                     "the appeal" => 2, "new moon" => 3),
    
    "tom" => array("chaos" => 2.5,"the last lecture" => 1.5)  
);

?>
                            
                        

Create recommend_test.php

                            
                                <?php

require_once("recommend.php");
require_once("sample_list.php");

$re = new Recommend();
echo "<pre>";
$user = 'peter';
$recommended_items = $re->getRecommendations($dataset, $user);
print_r($recommended_items);
echo "</pre>";
?>
                            
                        

Create recommend.php

                            
                                
$value)
        {
            if(array_key_exists($key, $preferences[$person2]))
                $similar[$key] = 1;
        }
        
        if(count($similar) == 0)
            return 0;
        
        foreach($preferences[$person1] as $key=>$value)
        {
            if(array_key_exists($key, $preferences[$person2]))
                $sum = $sum + pow($value - $preferences[$person2][$key], 2);
        }
        
        return  1/(1 + sqrt($sum));     
    }
    
    
    public function matchItems($preferences, $person)
    {
        $score = array();
        
            foreach($preferences as $otherPerson=>$values)
            {
                if($otherPerson !== $person)
                {
                    $sim = $this->similarityDistance($preferences, $person, $otherPerson);
                    
                    if($sim > 0)
                        $score[$otherPerson] = $sim;
                }
            }
        
        array_multisort($score, SORT_DESC);
        return $score;
    
    }
    
    
    public function transformPreferences($preferences)
    {
        $result = array();
        
        foreach($preferences as $otherPerson => $values)
        {
            foreach($values as $key => $value)
            {
                $result[$key][$otherPerson] = $value;
            }
        }
        
        return $result;
    }
    
    
    public function getRecommendations($preferences, $person)
    {
        $total = array();
        $simSums = array();
        $ranks = array();
        $sim = 0;
        
        foreach($preferences as $otherPerson=>$values)
        {
            if($otherPerson != $person)
            {
                $sim = $this->similarityDistance($preferences, $person, $otherPerson);
            }
            
            if($sim > 0)
            {
                foreach($preferences[$otherPerson] as $key=>$value)
                {
                    if(!array_key_exists($key, $preferences[$person]))
                    {
                        if(!array_key_exists($key, $total)) {
                            $total[$key] = 0;
                        }
                        $total[$key] += $preferences[$otherPerson][$key] * $sim;
                        
                        if(!array_key_exists($key, $simSums)) {
                            $simSums[$key] = 0;
                        }
                        $simSums[$key] += $sim;
                    }
                }
                
            }
        }

        foreach($total as $key=>$value)
        {
            $ranks[$key] = $value / $simSums[$key];
        }
        
    array_multisort($ranks, SORT_DESC);    
    return $ranks;
        
    }
   
}

?>