PHP code example of aldeebhasan / laravelcf

1. Go to this page and download the library: Download aldeebhasan/laravelcf library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

aldeebhasan / laravelcf example snippets


use \Aldeebhasan\LaravelCF\Facades\Recommender;
  
Recommender::addRating      ('user_1', 'product_1', 5);
Recommender::addCartAddition('user_1', 'product_1', 2); // like the quantity
Recommender::addPurchase    ('user_1', 'product_1', 5); // like the quantity
Recommender::addBookmark    ('user_1', 'product_1', 5);

use \Aldeebhasan\LaravelCF\Facades\Recommender;
use \Aldeebhasan\LaravelCF\Enums\RelationType;

/* you cann also use any of RelationType::PURCHASE,RelationType::CART_ACTION,RelationType::BOOKMARK*/
Recommender::getItemBasedRecommender(RelationType::RATE); // to recommend similar products
//OR
Recommender::getUserBasedRecommender(RelationType::RATE);// to recommend similar users

use \Aldeebhasan\LaravelCF\Facades\Recommender;
use \Aldeebhasan\LaravelCF\Enums\RelationType;

/* you cann also use any of RelationType::PURCHASE,RelationType::CART_ACTION,RelationType::BOOKMARK*/
Recommender::getItemBasedRecommender(RelationType::RATE)
            ->setSimilarityFunction(Cosine::class)
            ->train()
            ->recommendTo('user_1');

use \Aldeebhasan\LaravelCF\Facades\Recommender;
use \Aldeebhasan\LaravelCF\Enums\RelationType;
use \Aldeebhasan\LaravelCF\Enums\MissingValue;
use \Aldeebhasan\LaravelCF\Similarity;

Recommender::getItemBasedRecommender(RelationType::RATE)
            // use Weighted cosine algorithm and replace the missing values with zero 
            ->setSimilarityFunction(CosineWeighted::class,MissingValue::ZERO,true)
             // use SlopeOne algorithm and replace the missing values with the mean 
            ->setSimilarityFunction(SlopeOne::class,MissingValue::MEAN,true)
            

use \Aldeebhasan\LaravelCF\Facades\Recommender;
use \Aldeebhasan\LaravelCF\Enums\RelationType;
use \Aldeebhasan\LaravelCF\Enums\MissingValue;
use \Aldeebhasan\LaravelCF\Similarity;

Recommender::addRating(1, 'squid', 1);
Recommender::addRating(2, 'squid', 1);
Recommender::addRating(3, 'squid', 0.2);
Recommender::addRating(1, 'cuttlefish', 0.5);
Recommender::addRating(3, 'cuttlefish', 0.4);
Recommender::addRating(4, 'cuttlefish', 0.9);
Recommender::addRating(1, 'octopus', 0.2);
Recommender::addRating(2, 'octopus', 0.5);
Recommender::addRating(3, 'octopus', 1);
Recommender::addRating(4, 'octopus', 0.4);
Recommender::addRating(1, 'nautilus', 0.2);
Recommender::addRating(3, 'nautilus', 0.4);
Recommender::addRating(4, 'nautilus', 0.5);
$results = Recommender::getItemBasedRecommender(RelationType::RATE)
    ->setSimilarityFunction(CosineWeighted::class, MissingValue::MEAN)
    ->train()
    ->recommendTo('squid');


/**
recommendation results sorted by similarity: 
[ 
  "cuttlefish" => 0.89
  "nautilus" => 0.75
  "octopus" => 0.5
]
**/
bash
php artisan migrate