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 Classifier
ส่งผ่านออบเจ็กต์ options
เลือกเสริมเพื่อกำหนดค่าอินสแตนซ์ หากคุณระบุฟังก์ชัน tokenizer
ใน options
ฟังก์ชันนั้นจะถูกใช้เป็นโทเค็นของอินสแตนซ์
$classifier->learn(text, category)
สอนตัวแยกประเภทของคุณว่า text
อยู่ใน category
ใด ยิ่งคุณสอนตัวแยกประเภทมากเท่าไร มันก็ยิ่งน่าเชื่อถือมากขึ้นเท่านั้น โดยจะใช้สิ่งที่เรียนรู้เพื่อระบุเอกสารใหม่ที่ไม่เคยเห็นมาก่อน
$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);