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);