# # This file is part of the b8 package # # This program is free software; you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation in version 2.1 of the License. # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public # License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with this program; if not, write to the Free Software Foundation, # Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. ### This is an example script demonstrating how b8 can be used. ### #/* # Use this code block if you want to use Berkeley DB. # The database filename is interpreted relative to the b8.php script location. $config_b8 = array( 'storage' => 'dba' ); $config_database = array( 'database' => 'wordlist.db', 'handler' => 'db4' ); #*/ /* # Use this code block if you want to use MySQL. # An existing link resource can be passed to b8 by setting # $config_database['connection'] to this link resource. # Be sure to set your database access data otherwise! $config_b8 = array( 'storage' => 'mysql' ); $config_database = array( 'database' => 'test', 'table_name' => 'b8_wordlist', 'host' => 'localhost', 'user' => '', 'pass' => '' ); */ # To be able to calculate the time the classification took $time_start = NULL; function microtimeFloat() { list($usec, $sec) = explode(" ", microtime()); return ((float) $usec + (float) $sec); } # Output a nicely colored rating function formatRating($rating) { if($rating === FALSE) return "could not calculate spaminess"; $red = floor(255 * $rating); $green = floor(255 * (1 - $rating)); return "" . sprintf("%5f", $rating) . ""; } echo << example b8 interface

example b8 interface

END; $postedText = ""; if(isset($_POST['action']) and $_POST['text'] == "") echo "

Please type in a text!

\n\n"; elseif(isset($_POST['action']) and $_POST['text'] != "") { $time_start = microtimeFloat(); # Include the b8 code require dirname(__FILE__) . "/../b8/b8.php"; # Create a new b8 instance $b8 = new b8($config_b8, $config_database); # Check if everything worked smoothly $started_up = $b8->validate(); if($started_up !== TRUE) { echo "example: Could not initialize b8. error code: $started_up"; exit; } $text = stripslashes($_POST['text']); $postedText = htmlentities($text, ENT_QUOTES, 'UTF-8'); switch($_POST['action']) { case "Classify": echo "

Spaminess: " . formatRating($b8->classify($text)) . "

\n"; break; case "Save as Spam": $ratingBefore = $b8->classify($text); $b8->learn($text, b8::SPAM); $ratingAfter = $b8->classify($text); echo "

Saved the text as Spam

\n\n"; echo "
\n"; echo "\n"; echo "\n"; echo "
Classification before learning:" . formatRating($ratingBefore) . "
Classification after learning:" . formatRating($ratingAfter) . "
\n\n"; break; case "Save as Ham": $ratingBefore = $b8->classify($text); $b8->learn($text, b8::HAM); $ratingAfter = $b8->classify($text); echo "

Saved the text as Ham

\n\n"; echo "
\n"; echo "\n"; echo "\n"; echo "
Classification before learning:" . formatRating($ratingBefore) . "
Classification after learning:" . formatRating($ratingAfter) . "
\n\n"; break; case "Delete from Spam": $b8->unlearn($text, b8::SPAM); echo "

Deleted the text from Spam

\n\n"; break; case "Delete from Ham": $b8->unlearn($text, b8::HAM); echo "

Deleted the text from Ham

\n\n"; break; } $mem_used = round(memory_get_usage() / 1048576, 5); $peak_mem_used = round(memory_get_peak_usage() / 1048576, 5); $time_taken = round(microtimeFloat() - $time_start, 5); } echo <<
END; if($time_start !== NULL) { echo <<
Memory used: $mem_used MB
Peak memory used:$peak_mem_used MB
Time taken: $time_taken sec
END; } ?>