camspiers/statistical-classifier

Complement Naive Bayes 和 SVM 统计分类器的 PHP 实现,包括构建其他分类器、多个数据源和多个缓存后端的结构

0.8.0 2014-01-05 22:29 UTC

This package is auto-updated.

Last update: 2024-09-19 14:01:50 UTC


README

Build Status Latest Stable Version

PHP 分类器使用 语义版本控制,目前处于主版本 0,因此公共 API 不得视为稳定。

这是什么?

PHP 分类器是一个专注于重用、可定制性和性能的文本分类库。分类器可用于许多目的,但在检测垃圾邮件方面特别有用。

功能

  • Complement Naive Bayes 分类器
  • SVM (libsvm) 分类器
  • 高度可定制(轻松修改或构建自己的分类器)
  • 通过单独的库(phar 归档)提供命令行界面
  • 多种 数据导入类型 将数据导入分类器(文件目录、数据库查询、Json、序列化数组)
  • 多种类型的 模型缓存
  • 与 HipHop VM 兼容

安装

$ composer require camspiers/statistical-classifier

SVM 支持

对于 SVM 支持,需要 libsvm 和 php-svm。有关安装说明,请参阅 php-svm

使用方法

无缓存 Naive Bayes

use Camspiers\StatisticalClassifier\Classifier\ComplementNaiveBayes;
use Camspiers\StatisticalClassifier\DataSource\DataArray;

$source = new DataArray();
$source->addDocument('spam', 'Some spam document');
$source->addDocument('spam', 'Another spam document');
$source->addDocument('ham', 'Some ham document');
$source->addDocument('ham', 'Another ham document');

$classifier = new ComplementNaiveBayes($source);
$classifier->is('ham', 'Some ham document'); // bool(true)
$classifier->classify('Some ham document'); // string "ham"

无缓存 SVM

use Camspiers\StatisticalClassifier\Classifier\SVM;
use Camspiers\StatisticalClassifier\DataSource\DataArray;

$source = new DataArray()
$source->addDocument('spam', 'Some spam document');
$source->addDocument('spam', 'Another spam document');
$source->addDocument('ham', 'Some ham document');
$source->addDocument('ham', 'Another ham document');

$classifier = new SVM($source);
$classifier->is('ham', 'Some ham document'); // bool(true)
$classifier->classify('Some ham document'); // string "ham"

缓存模型

缓存模型需要 maximebf/CacheCache,可以通过 packagist 安装。可以轻松集成其他缓存系统。

缓存 Naive Bayes

use Camspiers\StatisticalClassifier\Classifier\ComplementNaiveBayes;
use Camspiers\StatisticalClassifier\Model\CachedModel;
use Camspiers\StatisticalClassifier\DataSource\DataArray;

$source = new DataArray();
$source->addDocument('spam', 'Some spam document');
$source->addDocument('spam', 'Another spam document');
$source->addDocument('ham', 'Some ham document');
$source->addDocument('ham', 'Another ham document');

$model = new CachedModel(
	'mycachename',
	new CacheCache\Cache(
		new CacheCache\Backends\File(
			array(
				'dir' => __DIR__
			)
		)
	)
);

$classifier = new ComplementNaiveBayes($source, $model);
$classifier->is('ham', 'Some ham document'); // bool(true)
$classifier->classify('Some ham document'); // string "ham"

缓存 SVM

use Camspiers\StatisticalClassifier\Classifier\SVM;
use Camspiers\StatisticalClassifier\Model\SVMCachedModel;
use Camspiers\StatisticalClassifier\DataSource\DataArray;

$source = new DataArray();
$source->addDocument('spam', 'Some spam document');
$source->addDocument('spam', 'Another spam document');
$source->addDocument('ham', 'Some ham document');
$source->addDocument('ham', 'Another ham document');

$model = new Model\SVMCachedModel(
	__DIR__ . '/model.svm',
	new CacheCache\Cache(
		new CacheCache\Backends\File(
			array(
				'dir' => __DIR__
			)
		)
	)
);

$classifier = new SVM($source, $model);
$classifier->is('ham', 'Some ham document'); // bool(true)
$classifier->classify('Some ham document'); // string "ham"

单元测试

statistical-classifier/ $ composer install --dev
statistical-classifier/ $ phpunit