3 # Copyright (C) 2006-2010 Tobias Leupold <tobias.leupold@web.de>
5 # This file is part of the b8 package
7 # This program is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU Lesser General Public License as published by
9 # the Free Software Foundation in version 2.1 of the License.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 # License for more details.
16 # You should have received a copy of the GNU Lesser General Public License
17 # along with this program; if not, write to the Free Software Foundation,
18 # Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
21 ### This is an example script demonstrating how b8 can be used. ###
25 # Use this code block if you want to use Berkeley DB.
27 # The database filename is interpreted relative to the b8.php script location.
33 $config_database = array(
34 'database' => 'wordlist.db',
42 # Use this code block if you want to use MySQL.
44 # An existing link resource can be passed to b8 by setting
45 # $config_database['connection'] to this link resource.
46 # Be sure to set your database access data otherwise!
52 $config_database = array(
54 'table_name' => 'b8_wordlist',
55 'host' => 'localhost',
62 # To be able to calculate the time the classification took
66 function microtimeFloat()
68 list($usec, $sec) = explode(" ", microtime());
69 return ((float) $usec + (float) $sec);
72 # Output a nicely colored rating
74 function formatRating($rating)
78 return "<span style=\"color:red\">could not calculate spaminess</span>";
80 $red = floor(255 * $rating);
81 $green = floor(255 * (1 - $rating));
83 return "<span style=\"color:rgb($red, $green, 0);\"><b>" . sprintf("%5f", $rating) . "</b></span>";
88 <?xml version="1.0" encoding="UTF-8"?>
90 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
91 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
93 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
97 <title>example b8 interface</title>
99 <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
101 <meta name="dc.creator" content="Tobias Leupold" />
102 <meta name="dc.rights" content="Copyright (c) by Tobias Leupold" />
110 <h1>example b8 interface</h1>
117 if(isset($_POST['action']) and $_POST['text'] == "")
118 echo "<p style=\"color:red;\"><b>Please type in a text!</b></p>\n\n";
120 elseif(isset($_POST['action']) and $_POST['text'] != "") {
122 $time_start = microtimeFloat();
124 # Include the b8 code
125 require dirname(__FILE__) . "/../b8/b8.php";
127 # Create a new b8 instance
128 $b8 = new b8($config_b8, $config_database);
130 # Check if everything worked smoothly
132 $started_up = $b8->validate();
134 if($started_up !== TRUE) {
135 echo "<b>example:</b> Could not initialize b8. error code: $started_up";
139 $text = stripslashes($_POST['text']);
140 $postedText = htmlentities($text, ENT_QUOTES, 'UTF-8');
142 switch($_POST['action']) {
145 echo "<p><b>Spaminess: " . formatRating($b8->classify($text)) . "</b></p>\n";
150 $ratingBefore = $b8->classify($text);
151 $b8->learn($text, b8::SPAM);
152 $ratingAfter = $b8->classify($text);
154 echo "<p>Saved the text as Spam</p>\n\n";
156 echo "<div><table>\n";
157 echo "<tr><td>Classification before learning:</td><td>" . formatRating($ratingBefore) . "</td></tr>\n";
158 echo "<tr><td>Classification after learning:</td><td>" . formatRating($ratingAfter) . "</td></tr>\n";
159 echo "</table></div>\n\n";
165 $ratingBefore = $b8->classify($text);
166 $b8->learn($text, b8::HAM);
167 $ratingAfter = $b8->classify($text);
169 echo "<p>Saved the text as Ham</p>\n\n";
171 echo "<div><table>\n";
172 echo "<tr><td>Classification before learning:</td><td>" . formatRating($ratingBefore) . "</td></tr>\n";
173 echo "<tr><td>Classification after learning:</td><td>" . formatRating($ratingAfter) . "</td></tr>\n";
174 echo "</table></div>\n\n";
178 case "Delete from Spam":
179 $b8->unlearn($text, b8::SPAM);
180 echo "<p style=\"color:green\">Deleted the text from Spam</p>\n\n";
183 case "Delete from Ham":
184 $b8->unlearn($text, b8::HAM);
185 echo "<p style=\"color:green\">Deleted the text from Ham</p>\n\n";
190 $mem_used = round(memory_get_usage() / 1048576, 5);
191 $peak_mem_used = round(memory_get_peak_usage() / 1048576, 5);
192 $time_taken = round(microtimeFloat() - $time_start, 5);
198 <form action="{$_SERVER['PHP_SELF']}" method="post">
200 <textarea name="text" cols="50" rows="16">$postedText</textarea>
204 <td><input type="submit" name="action" value="Classify" /></td>
207 <td><input type="submit" name="action" value="Save as Spam" /></td>
208 <td><input type="submit" name="action" value="Save as Ham" /></td>
211 <td><input type="submit" name="action" value="Delete from Spam" /></td>
212 <td><input type="submit" name="action" value="Delete from Ham" /></td>
222 if($time_start !== NULL) {
227 <tr><td>Memory used: </td><td>$mem_used MB</td></tr>
228 <tr><td>Peak memory used:</td><td>$peak_mem_used MB</td></tr>
229 <tr><td>Time taken: </td><td>$time_taken sec</td></tr>