bayes
: مُصنف Naive- bayes لـ PHP يأخذ 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])
إرجاع مثيل لمصنف Naive bayes .
قم بتمرير كائن options
اختيارية لتكوين المثيل. إذا قمت بتحديد وظيفة tokenizer
في options
، فسيتم استخدامه كرمز مميز للمثيل.
$classifier->learn(text, category)
قم بتعليم المصنف الخاص بك category
التي ينتمي إليها text
. كلما قمت بتعليم مصنفك أكثر، أصبح أكثر موثوقية. وسوف يستخدم ما تعلمه لتحديد المستندات الجديدة التي لم يرها من قبل.
$classifier->categorize(text)
إرجاع category
التي يعتقد أن text
ينتمي إليها. يعتمد حكمه على ما علمته إياه .learn() .
$classifier->probabilities(text)
استخراج الاحتمالات لكل فئة معروفة.
$classifier->toJson()
إرجاع تمثيل JSON للمصنف.
$classifier->fromJson(jsonStr)
إرجاع مثيل مصنف من تمثيل JSON. استخدم هذا مع تمثيل JSON الذي تم الحصول عليه من $classifier->toJson()
يمكنك تمرير وظيفة الرمز المميز الخاصة بك في المُنشئ. مثال:
// 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);