wfphpnlp/naivebayesclassifier

PHP library for Indonesian text classification using the Naive Bayes Classifier (NBC) approach.

1.2.0 2020-07-08 14:08 UTC

This package is auto-updated.

Last update: 2024-09-08 23:16:56 UTC


README

Build Status GitHub Packagist Version

Library PHP untuk klasifikasi teks menjadi klasifikasi positif, negatif dan netral pada Bahasa Indonesia menggunakan metode Naive Bayes Classifier.

Cara Install

Via Composer

composer require wfphpnlp/naivebayesclassifier

Jika Anda masih belum memahami bagaimana cara menggunakan Composer, silahkan baca Getting Started with Composer.

Clone GitHub

git clone https://github.com/WillyFaq/Naive-Bayes-Classifier.git

Cara Penggunaan

jika menggunakan composer inisiasikan projek anda dengan vendor/autoload.php

require_once __DIR__ . '/vendor/autoload.php';
use wfphpnlp\NaiveBayes;

Berikut contoh lengkap penggunaan.

<?php
    // include composer autoloader
    require_once __DIR__ . '/vendor/autoload.php';
    use wfphpnlp\NaiveBayes;

    $data = [
                [
                    '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
    )
*/