PHP code example of mbeurel / php-liblinear

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

    

mbeurel / php-liblinear example snippets



use PhpLiblinear\Classification\LibLinear;
$data = [
  ["French", "Ceci est un texte dans la langue française."],
  ["French", "Bonjour, comment allez vous ?"],
  ["French", "Bonjour, je m'appelle Jean !!!"],
  ["English", "This is a english language text."],
  ["English", "Hello, How are you ?"],
  ["English", "Hello, my name is Jean !!!"],
];
try {
  
  // Init library
  $libLinear = new LibLinear("instanceName", __DIR__."/var", array(
        "type"      =>  0,            // Liblinear type, view the liblinear documentation
        "cost"      =>  1.0,          
        "epsilon"   =>  0.1,
        "debug"     =>  false
      )
    );

  // Liblinear train
  $libLinear->train($data);
  
  // Save model
  $libLinear->save();

  // Load model
  $libLinear->load();
  
  // Liblinear predict : String or Array parameters, to array => ["Bonjour, je m'appelle Louis", "Comment allez vous ?"]
  $result = $libLinear->predict("Bonjour, je m'appelle Louis");
  
  // View result
  var_dump($result);

  //  $result = array(
  //    0  =>  array(
  //      "value"        =>  "French",
  //      "percentage"   =>  0.763259,
  //      "percentages"  =>  array(
  //        "French"        => 0.763259,
  //        "English"       => 0.236741
  //      )
  //    )
  //  )

} catch(\Exception $e) {
  echo $e;
}

composer