bayes
: Un classificateur Naive- bayes pour PHP bayes
prend un document (morceau de texte) et vous indique à quelle catégorie ce document appartient.
Cette bibliothèque a été portée à partir d'une bibliothèque nodejs @ https://github.com/ttezel/bayes
Vous pouvez l'utiliser pour classer n'importe quel contenu textuel dans n'importe quel ensemble arbitraire de catégories . Par exemple:
composer require niiknow/ bayes
$ classifier = new Niiknow bayes ();
// teach it positive phrases
$ classifier -> learn ( ' amazing, awesome movie!! Yeah!! Oh boy. ' , ' positive ' );
$ classifier -> learn ( ' Sweet, this is incredibly, amazing, perfect, great!! ' , ' positive ' );
// teach it a negative phrase
$ classifier -> learn ( ' terrible, shitty thing. Damn. Sucks!! ' , ' negative ' );
// now ask it to categorize a document it has never seen before
$ classifier -> categorize ( ' awesome, cool, amazing!! Yay. ' );
// => 'positive'
// serialize the classifier's state as a JSON string.
$ stateJson = $ classifier -> toJson ();
// load the classifier back from its JSON representation.
$ classifier -> fromJson ( $ stateJson );
$classifier = new Niiknow bayes ([options])
Renvoie une instance d'un classificateur Naive- bayes .
Transmettez un objet options
facultatif pour configurer l’instance. Si vous spécifiez une fonction tokenizer
dans options
, elle sera utilisée comme tokenizer de l'instance.
$classifier->learn(text, category)
Apprenez à votre classificateur à quelle category
appartient le text
. Plus vous enseignez votre classificateur, plus il devient fiable. Il utilisera ce qu'il a appris pour identifier de nouveaux documents qu'il n'a jamais vus auparavant.
$classifier->categorize(text)
Renvoie la category
il pense que text
appartient. Son jugement est basé sur ce que vous lui avez appris avec .learn() .
$classifier->probabilities(text)
Extrayez les probabilités pour chaque catégorie connue.
$classifier->toJson()
Renvoie la représentation JSON d'un classificateur.
$classifier->fromJson(jsonStr)
Renvoie une instance de classificateur à partir de la représentation JSON. Utilisez-le avec la représentation JSON obtenue à partir de $classifier->toJson()
Vous pouvez transmettre votre propre fonction tokenizer dans le constructeur. Exemple:
// array containing stopwords
$stopwords = array("der", "die", "das", "the");
// escape the stopword array and implode with pipe
$s = '~^W*('.implode("|", array_map("preg_quote", $stopwords)).')W+b|bW+(?1)W*$~i';
$options['tokenizer'] = function($text) use ($s) {
// convert everything to lowercase
$text = mb_strtolower($text);
// remove stop words
$text = preg_replace($s, '', $text);
// split the words
preg_match_all('/[[:alpha:]]+/u', $text, $matches);
// first match list of words
return $matches[0];
};
$classifier = new niiknow bayes ($options);