GithubHelp home page GithubHelp logo

silverstripe-classifierbridge's Introduction

SilverStripe Classifier Bridge

This library helps integrate classification services within SilverStripe sites.

Installation (with composer)

$ composer require camspiers/silverstripe-classifierbridge:dev-master

Usage

Integration via DataList and DataObject

  1. Implement the Document interface on your DataObject
use Camspiers\StatisticalClassifier\SilverStripe\Document;

class MyDataObject extends DataObject implements Document
{

	private static $db = array(
		'Content' => 'Text'
		'Spam' => 'Boolean'
	);
	
	
	public function getCategories()
	{
		return array($this->Spam ? 'spam' : 'ham');
	}

	public function getDocument()
	{
		return $this->Content;
	}

}
  1. Use a DataList to retrieve the existing DataObjects and classify a new DataObject
use Camspiers\StatisticalClassifier\Classifier\ComplementNaiveBayes;
use Camspiers\StatisticalClassifier\SilverStripe\DataSource;
use Camspiers\StatisticalClassifier\SilverStripe\Document;

// This DataObject could have been just populate via a form (e.g. $form->saveInto($myDataObject))
$dataObjectToClassify = new MyDataObject(
	array(
		'Content' => 'Some content'
	)
);

try {
	// A DataList is passed into a DataSource and then passed into the classifier
	$classifier = new ComplementNaiveBayes(new DataSource(MyDataObject::get()));
	if ($classifier->is('spam', $dataObjectToClassify->getDocument())) {
		// The document is spam
		// Perhaps set Spam = true on the DataObject and save it
	} else {
		// The document isn't spam
	}
} catch (Exception $e) {
	// Do something with the exception
}

Integration via SQLQuery

Using SQLQuery can improve memory usage and execution time, because it bypasses the creation of DataObjects for each record

use Camspiers\StatisticalClassifier\Classifier\ComplementNaiveBayes;
use Camspiers\StatisticalClassifier\DataSource\Grouped;
use Camspiers\StatisticalClassifier\SilverStripe\SQLQueryDataSource;
use Camspiers\StatisticalClassifier\SilverStripe\Document;

$spamQuery = new SQLQuery("Content, Spam", "MyDataObject", "Spam = 1");
$hamQuery = new SQLQuery("Content, Spam", "MyDataObject", "Spam = 0");

try {
	// Create the classifier by using a Grouped data source
	$classifier = new ComplementNaiveBayes(
		new Grouped(
			array(
				new SQLQueryDataSource("spam", $spamQuery, "Content"),
				new SQLQueryDataSource("ham", $hamQuery, "Content")
			)
		)
	);

	if ($classifier->is('spam', "Some content to classify")) {
		// The document is spam
		// Perhaps set Spam = true on the DataObject and save it
	} else {
		// The document isn't spam
	}
} catch (Exception $e) {
	// Do something with the exception
}

See PHP Classifier for documentation around caching and more advanced topics.

silverstripe-classifierbridge's People

Contributors

camspiers avatar

Stargazers

Milan Jelicanin avatar Frank Mullenger avatar Loz Calver avatar

Watchers

 avatar James Cloos avatar

Forkers

helpfulrobot

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.