PHP code example of wfphpnlp / naivebayesclassifier

1. Go to this page and download the library: Download wfphpnlp/naivebayesclassifier 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/ */

    

wfphpnlp / naivebayesclassifier example snippets



use wfphpnlp\NaiveBayes;


    // include composer autoloader
                    [
                    'text' => 'produknya keren kualitasnya bagus awet dan tahan lama',
                    'class' => 'positif'
                ],
                [
                    'text' => 'barangnya bagus mudah digunakan',
                    'class' => 'positif'
                ],
                [
                    'text' => 'barangnya cepat rusak kualitas buruk, tidak bisa digunakan sama sekali',
                    'class' => 'negatif'
                ],
                [
                    'text' => 'produknya jelek tidak sesuai harapan',
                    'class' => 'negatif'
                ],
                [
                    'text' => 'produk sudah cukup baik, cara penggunaanya juga cukup mudah',
                    'class' => 'netral'
                ],
            ];
			
    $nb = new NaiveBayes();
    // mendefinisikan class target sesuai dengan yang ada pada data training.
    $nb->setClass(['positif', 'negatif', 'netral']);

    // proses training
    $nb->training($data);

    // pengujian
    $result = $nb->predict('produknya buruk tidak keren'); // output "negatif"
    
    print_r($result);
/*
    
    //hasil output
    Array
    (
        [positif] => Array
            (
                [computed] => Array
                    (
                        [0] => 0.038461538461538
                        [1] => 0.019230769230769
                        [2] => 0.019230769230769
                        [3] => 0.038461538461538
                    )

                [result] => 0.023076923076923
            )

        [negatif] => Array
            (
                [computed] => Array
                    (
                        [0] => 0.037037037037037
                        [1] => 0.037037037037037
                        [2] => 0.055555555555556
                        [3] => 0.018518518518519
                    )

                [result] => 0.02962962962963
            )

        [netral] => Array
            (
                [computed] => Array
                    (
                        [0] => 0.021739130434783
                        [1] => 0.021739130434783
                        [2] => 0.021739130434783
                        [3] => 0.021739130434783
                    )

                [result] => 0.017391304347826
            )

        [hasil] => negatif
    )
*/