Example #1
0
 public function action_index()
 {
     $B8 = B8::factory();
     // Check for action
     if (isset($_POST['action'])) {
         // Check for text
         if (empty($_POST['text'])) {
             $message = '<p style="color:red;"><b>Please enter some text</b></p>';
         } else {
             // Process action
             switch ($_POST['action']) {
                 case 'Classify':
                     $probability = $B8->classify($_POST['text']);
                     $message = '<p>Classification: ' . $this->format_probability($probability) . ' (' . $B8->classify($_POST['text'], 'const') . ')</p>';
                     break;
                 case 'Learn as HAM':
                     $probability_before = $B8->classify($_POST['text']);
                     $B8->learn($_POST['text'], B8::HAM);
                     $probability_after = $B8->classify($_POST['text']);
                     $message = '<p>Classification before learning as HAM: ' . $this->format_probability($probability_before) . '</p>';
                     $message .= '<p>Classification after learning as HAM: ' . $this->format_probability($probability_after) . '</p>';
                     break;
                 case 'Learn as SPAM':
                     $probability_before = $B8->classify($_POST['text']);
                     $B8->learn($_POST['text'], B8::SPAM);
                     $probability_after = $B8->classify($_POST['text']);
                     $message = '<p>Classification before learning as SPAM: ' . $this->format_probability($probability_before) . '</p>';
                     $message .= '<p>Classification after learning as SPAM: ' . $this->format_probability($probability_after) . '</p>';
                     break;
                 case 'Unlearn as HAM':
                     $probability_before = $B8->classify($_POST['text']);
                     $B8->unlearn($_POST['text'], B8::HAM);
                     $probability_after = $B8->classify($_POST['text']);
                     $message = '<p>Classification before unlearning as HAM: ' . $this->format_probability($probability_before) . '</p>';
                     $message .= '<p>Classification after unlearning as HAM: ' . $this->format_probability($probability_after) . '</p>';
                     break;
                 case 'Unlearn as SPAM':
                     $probability_before = $B8->classify($_POST['text']);
                     $B8->unlearn($_POST['text'], B8::SPAM);
                     $probability_after = $B8->classify($_POST['text']);
                     $message = '<p>Classification before unlearning as SPAM: ' . $this->format_probability($probability_before) . '</p>';
                     $message .= '<p>Classification after unlearning as SPAM: ' . $this->format_probability($probability_after) . '</p>';
                     break;
             }
         }
     }
     $this->response->body(View::factory('b8')->bind('message', $message));
 }
Example #2
0
 /**
  * Perform classification changes
  */
 protected function classify()
 {
     Kohana::$log->add(Kohana::DEBUG, 'Executing Controller_Comments_Core::classify');
     $id = 0;
     $comment = NULL;
     if (isset($_POST['classify_id'])) {
         $id = $_POST['classify_id'];
         $comment = Sprig::factory($this->model, array('id' => $id))->load();
         // If comment is invalid
         if (!$comment->loaded()) {
             $this->request->response = FALSE;
             return;
         }
     }
     // Perform Bayesian filter teaching
     if (isset($_POST['classify_option']) and $_POST['classify_option'] != 'nop') {
         $option = $_POST['classify_option'];
         Kohana::$log->add(Kohana::DEBUG, 'Performing classification change, ' . $option);
         $B8 = B8::factory();
         try {
             switch ($option) {
                 case 'learn_ham':
                     $probability_before = $B8->classify($comment->text);
                     $B8->learn($comment->text, B8::HAM);
                     $probability_after = $B8->classify($comment->text);
                     Kohana::$log->add(Kohana::INFO, 'Comment learned as ham.  Probability before=' . $probability_before . ', after=' . $probability_after);
                     break;
                 case 'learn_spam':
                     $probability_before = $B8->classify($comment->text);
                     $B8->learn($comment->text, B8::SPAM);
                     $probability_after = $B8->classify($comment->text);
                     Kohana::$log->add(Kohana::INFO, 'Comment learned as spam.  Probability before=' . $probability_before . ', after=' . $probability_after);
                     break;
                 case 'unlearn_ham':
                     $probability_before = $B8->classify($comment->text);
                     $B8->unlearn($comment->text, B8::HAM);
                     $probability_after = $B8->classify($comment->text);
                     Kohana::$log->add(Kohana::INFO, 'Comment unlearned as ham.  Probability before=' . $probability_before . ', after=' . $probability_after);
                     break;
                 case 'unlearn_spam':
                     $probability_before = $B8->classify($comment->text);
                     $B8->unlearn($comment->text, B8::SPAM);
                     $probability_after = $B8->classify($comment->text);
                     Kohana::$log->add(Kohana::INFO, 'Comment unlearned as spam.  Probability before=' . $probability_before . ', after=' . $probability_after);
                     break;
             }
         } catch (Exception $e) {
             Kohana::$log->add(Kohana::ERROR, 'Error occured classifying comment, ' . $option);
             $this->request->response = FALSE;
             return;
         }
     }
     // Approve the comment
     if (isset($_POST['classify_ham'])) {
         Kohana::$log->add(Kohana::DEBUG, 'Approving comment, id=' . $id);
         try {
             $comment->state = 'ham';
             $comment->update();
         } catch (Exception $e) {
             Kohana::$log->add(Kohana::ERROR, 'Error occured approving comment, id=' . $id);
             $this->request->response = FALSE;
             return;
         }
     }
     // Mark the comment as spam
     if (isset($_POST['classify_spam'])) {
         Kohana::$log->add(Kohana::DEBUG, 'Marking comment as spam, id=' . $id);
         try {
             $comment->state = 'spam';
             $comment->update();
         } catch (Exception $e) {
             Kohana::$log->add(Kohana::ERROR, 'Error occured marking comment as spam, id=' . $id);
             $this->request->response = FALSE;
             return;
         }
     }
 }