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 Classifier のインスタンスを返します。
オプションの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);