Pular para o conteúdo principal

Installation

Requirements

RequirementVersion
PHP>=8.3 <8.6
Composerany
ext-dbaOnly for GDBM storage

Install via Composer

composer require byjg/text-classifier

Storage backend requirements

The library supports multiple storage backends. Choose based on your use case:

BackendEngineExtra requirement
Storage\RdbmsBinaryClassifier (spam filter)None — uses byjg/micro-orm
Storage\DbaBinaryClassifier (spam filter)ext-dba PHP extension
NaiveBayes\Storage\RdbmsNaiveBayesNone
NaiveBayes\Storage\MemoryNaiveBayesNone

Enable ext-dba (GDBM only)

# Ubuntu / Debian
sudo apt-get install php-dba

# Verify
php -m | grep dba

Verify installation

<?php
require 'vendor/autoload.php';

use ByJG\TextClassifier\BinaryClassifier;
use ByJG\TextClassifier\ConfigBinaryClassifier;
use ByJG\TextClassifier\Lexer\StandardLexer;
use ByJG\TextClassifier\Lexer\ConfigLexer;
use ByJG\TextClassifier\Degenerator\StandardDegenerator;
use ByJG\TextClassifier\Degenerator\ConfigDegenerator;
use ByJG\TextClassifier\Storage\Rdbms;
use ByJG\Util\Uri;

$storage = new Rdbms(
new Uri('sqlite:///tmp/classifier_verify.db'),
new StandardDegenerator(new ConfigDegenerator())
);
$storage->createDatabase();

$classifier = new BinaryClassifier(new ConfigBinaryClassifier(), $storage, new StandardLexer(new ConfigLexer()));

$result = $classifier->classify('hello world');
echo $result->score; // prints 0.5 (no training yet)

Next steps