]> git.mxchange.org Git - friendica.git/blob - library/spam/example/index.php
another one
[friendica.git] / library / spam / example / index.php
1 <?php
2
3 #   Copyright (C) 2006-2010 Tobias Leupold <tobias.leupold@web.de>
4 #
5 #   This file is part of the b8 package
6 #
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.
10 #
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.
15 #
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.
19
20
21 ### This is an example script demonstrating how b8 can be used. ###
22
23 #/*
24
25 # Use this code block if you want to use Berkeley DB.
26
27 # The database filename is interpreted relative to the b8.php script location.
28
29 $config_b8 = array(
30         'storage' => 'dba'
31 );
32
33 $config_database = array(
34         'database' => 'wordlist.db',
35         'handler'  => 'db4'
36 );
37
38 #*/
39
40 /*
41
42 # Use this code block if you want to use MySQL.
43
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!
47
48 $config_b8 = array(
49         'storage' => 'mysql'
50 );
51
52 $config_database = array(
53         'database'   => 'test',
54         'table_name' => 'b8_wordlist',
55         'host'       => 'localhost',
56         'user'       => '',
57         'pass'       => ''
58 );
59
60 */
61
62 # To be able to calculate the time the classification took
63
64 $time_start = NULL;
65
66 function microtimeFloat()
67 {
68         list($usec, $sec) = explode(" ", microtime());
69         return ((float) $usec + (float) $sec);
70 }
71
72 # Output a nicely colored rating
73
74 function formatRating($rating)
75 {
76
77         if($rating === FALSE)
78                 return "<span style=\"color:red\">could not calculate spaminess</span>";
79
80         $red   = floor(255 * $rating);
81         $green = floor(255 * (1 - $rating));
82
83         return "<span style=\"color:rgb($red, $green, 0);\"><b>" . sprintf("%5f", $rating) . "</b></span>";
84
85 }
86
87 echo <<<END
88 <?xml version="1.0" encoding="UTF-8"?>
89
90 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
91    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
92
93 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
94
95 <head>
96
97 <title>example b8 interface</title>
98
99 <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
100
101 <meta name="dc.creator" content="Tobias Leupold" />
102 <meta name="dc.rights" content="Copyright (c) by Tobias Leupold" />
103
104 </head>
105
106 <body>
107
108 <div>
109
110 <h1>example b8 interface</h1>
111
112
113 END;
114
115 $postedText = "";
116
117 if(isset($_POST['action']) and $_POST['text'] ==  "")
118         echo "<p style=\"color:red;\"><b>Please type in a text!</b></p>\n\n";
119
120 elseif(isset($_POST['action']) and $_POST['text'] !=  "") {
121
122         $time_start = microtimeFloat();
123
124         # Include the b8 code
125         require dirname(__FILE__) . "/../b8/b8.php";
126
127         # Create a new b8 instance
128         $b8 = new b8($config_b8, $config_database);
129
130         # Check if everything worked smoothly
131
132         $started_up = $b8->validate();
133
134         if($started_up !== TRUE) {
135                 echo "<b>example:</b> Could not initialize b8. error code: $started_up";
136                 exit;
137         }
138
139         $text = stripslashes($_POST['text']);
140         $postedText = htmlentities($text, ENT_QUOTES, 'UTF-8');
141
142         switch($_POST['action']) {
143
144                 case "Classify":
145                         echo "<p><b>Spaminess: " . formatRating($b8->classify($text)) . "</b></p>\n";
146                         break;
147
148                 case "Save as Spam":
149
150                         $ratingBefore = $b8->classify($text);
151                         $b8->learn($text, b8::SPAM);
152                         $ratingAfter = $b8->classify($text);
153
154                         echo "<p>Saved the text as Spam</p>\n\n";
155
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";
160
161                         break;
162
163                 case "Save as Ham":
164
165                         $ratingBefore = $b8->classify($text);
166                         $b8->learn($text, b8::HAM);
167                         $ratingAfter = $b8->classify($text);
168
169                         echo "<p>Saved the text as Ham</p>\n\n";
170
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";
175
176                         break;
177
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";
181                         break;
182
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";
186                         break;
187
188         }
189
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);
193
194 }
195
196 echo <<<END
197 <div>
198 <form action="{$_SERVER['PHP_SELF']}" method="post">
199 <div>
200 <textarea name="text" cols="50" rows="16">$postedText</textarea>
201 </div>
202 <table>
203 <tr>
204 <td><input type="submit" name="action" value="Classify" /></td>
205 </tr>
206 <tr>
207 <td><input type="submit" name="action" value="Save as Spam" /></td>
208 <td><input type="submit" name="action" value="Save as Ham" /></td>
209 </tr>
210 <tr>
211 <td><input type="submit" name="action" value="Delete from Spam" /></td>
212 <td><input type="submit" name="action" value="Delete from Ham" /></td>
213 </tr>
214 </table>
215 </form>
216 </div>
217
218 </div>
219
220 END;
221
222 if($time_start !== NULL) {
223
224 echo <<<END
225 <div>
226 <table border="0">
227 <tr><td>Memory used:     </td><td>$mem_used&thinsp;MB</td></tr>
228 <tr><td>Peak memory used:</td><td>$peak_mem_used&thinsp;MB</td></tr>
229 <tr><td>Time taken:      </td><td>$time_taken&thinsp;sec</td></tr>
230 </table>
231 </div>
232
233 END;
234
235 }
236
237 ?>
238
239 </body>
240
241 </html>