8 666
داشتم مقاله مینوشتم وقتی تمام شد فهمیدم کیبورد انگلیسی بوده
13 9月 2024, 16:13
14 9月 2024, ~12:13
bash <(curl -Ls https://raw.githubusercontent.com/Salarvand-Education/Server-Defender/main/install.sh)
در اخر که کارش تموم میشه بهتون یک عدد 4 رقمی میده این رو نگه دارید چون پورت سرورتون به این عدد تغییر میکنهphp-ai/php-ml استفاده میکنیم.
1. نصب کتابخانه
اول باید این کتابخانه رو نصب کنیم. توی ترمینال این دستور رو وارد کنین:
composer require php-ai/php-ml
2. نوشتن کد
حالا یه فایل PHP بسازید و کد زیر رو توش بزنید:
<?php
require 'vendor/autoload.php';
use Phpml\Classification\NaiveBayes;
use Phpml\Tokenization\WhitespaceTokenizer;
$samples = [
['I love this product', 'positive'],
['This is an amazing movie', 'positive'],
['I hate waiting', 'negative'],
['The food was terrible', 'negative'],
['I am so happy with the service', 'positive'],
['This is the worst experience ever', 'negative']
];
$labels = ['positive', 'positive', 'negative', 'negative', 'positive', 'negative'];
$tokenizer = new WhitespaceTokenizer();
$samples = array_map(function($sample) use ($tokenizer) {
return $tokenizer->tokenize($sample[0]);
}, $samples);
$classifier = new NaiveBayes();
$classifier->train($samples, $labels);
$testSample = 'I am extremely satisfied with the purchase';
$testSample = $tokenizer->tokenize($testSample);
$prediction = $classifier->predict($testSample);
echo 'پیشبینی: ' . $prediction;
?>
توضیحات
1. دادههای آموزشی: یه سری جمله داریم که با برچسب مثبت یا منفی مشخص کردیم. اینها رو برای آموزش مدل استفاده میکنیم.
2. پردازش متن: جملهها رو به کلمات تقسیم میکنیم.
3. آموزش مدل: مدل NaiveBayes رو با دادههای آموزشی آموزش میدیم.
4. پیشبینی: حالا یه جمله جدید رو به مدل میدیم و میبینیم چه برچسبی بهش میده.
این کد رو اجرا کنین و نتیجه پیشبینی مدل رو ببینین. امیدوارم براتون جالب باشه!