PHP code example of mr4-lc / recommendation
1. Go to this page and download the library: Download mr4-lc/recommendation 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/ */
mr4-lc / recommendation example snippets
return [
'featureWeight' => 1,
'categoryWeight' => 1,
'priceWeight' => 1,
'priceHighRange' => 1000,
'chunkSize' => 1000,
'perPage' => 5,
'output' => storage_path('app/private/recommendation/data'),
'mapping' => [
'tables' => [
// Sample config
'table_name' => [
'data_version' => '0.0.1',
'output' => 'table_name.json',
// SQL WHERE condition
'status' => [
'status' => 1,
],
// SQL ORDER BY
'order' => 'id',
// Customize model response
'model' => \App\Models\SomeModel::class,
// Load relatíonhip
'model_with' => ['tags'],
// Customize similarity
'class' => \App\Similarities\PostSimilarity::class,
'map' => [
'id' => 'id',
'price' => 'price',
'categories' => ['categories', 'group'], // Array or string column name,
'features' => ['color', 'type'], // Array or string column name,
],
'map_type' => [
'categories' => 'merge', // String data
'features' => 'object', // Object data
],
],
],
],
];
namespace App\Similarities;
use Mr4Lc\Recommendation\Similarity\Similarity;
class PostSimilarity extends \Mr4Lc\Recommendation\Similarity\ItemSimilarity
{
protected function calculateSimilarityScore($productA, $productB)
{
$productAFeatures = implode('', get_object_vars($productA->features));
$productBFeatures = implode('', get_object_vars($productB->features));
return array_sum([
(Similarity::hamming($productAFeatures, $productBFeatures) * $this->featureWeight),
(Similarity::jaccard($productA->categories, $productB->categories) * $this->categoryWeight)
]) / ($this->featureWeight + $this->categoryWeight);
}
public static function GetSimilarityPosts($product_id, $page = null, $perPage = null, $pagePrefix = 'page')
{
$products = static::GetSimilarityItems('posts', $product_id, $page, $perPage, $pagePrefix);
return $products;
}
}
$fields = request()->validate([
'item_name' => ['ilarity::GetSimilarityItems($fields['item_name'], $fields['item_id']);
return new JsonResponse($result, 200);
bash
php artisan recommendation:export {tableName} {--chunkSize=}
bash
php artisan recommendation:create {tableName} {--id=}