bayes
1.1.4
bayes
:PHP 的朴素bayes分类器bayes
接受一个文档(一段文本),并告诉您该文档属于哪个类别。
该库是从nodejs lib @ https://github.com/ttezel/bayes 移植的
您可以使用它来将任何文本内容分类到任意一组类别中。例如:
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])
返回朴素bayes分类器的实例。
传入可选options
对象来配置实例。如果您在options
中指定tokenizer
函数,它将用作实例的分词器。
$classifier->learn(text, category)
教你的分类器text
属于哪个category
。你教你的分类器越多,它就会变得越可靠。它将利用所学到的知识来识别以前从未见过的新文档。
$classifier->categorize(text)
返回它认为text
所属的category
。它的判断基于你用.learn()教给它的内容。
$classifier->probabilities(text)
提取每个已知类别的概率。
$classifier->toJson()
返回分类器的 JSON 表示形式。
$classifier->fromJson(jsonStr)
从 JSON 表示形式返回分类器实例。将此与从$classifier->toJson()
获取的 JSON 表示形式一起使用
您可以在构造函数中传入您自己的分词器函数。例子:
// 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);