[ Index ] |
|
Code source de LifeType 1.2.4 |
1 <?php 2 3 lt_include( PLOG_CLASS_PATH."class/dao/model.class.php" ); 4 lt_include( PLOG_CLASS_PATH."class/dao/bayesiantoken.class.php" ); 5 6 /** 7 * Takes care of dealing with fetching filtered contents from the database 8 * \ingroup DAO 9 */ 10 class BayesianTokens extends Model 11 { 12 13 function BayesianTokens() 14 { 15 $this->Model(); 16 } 17 18 /** 19 * -- Add function info here -- 20 */ 21 function getBayesianTokenFromId($id) 22 { 23 $query = "SELECT * FROM " . $this->getPrefix() . "bayesian_tokens WHERE id = $id"; 24 25 return $this->getBayesianTokenFromQuery( $query ); 26 } 27 28 /** 29 * -- Add function info here -- 30 */ 31 function getBayesianTokenFromToken($blogId, $token) 32 { 33 $query = "SELECT * FROM " . $this->getPrefix() . "bayesian_tokens WHERE token = '" . addSlashes($token) . "' AND blog_id = '$blogId'"; 34 35 return $this->getBayesianTokenFromQuery($query, $token); 36 } 37 38 /** 39 * -- Add function info here -- 40 */ 41 function getBayesianTokensFromArray($blogId, $tokens, $degenerate = true) 42 { 43 $bayesianTokens = array(); 44 45 foreach ($tokens as $token) 46 { 47 $bayesianToken = $this->getBayesianTokenFromToken($blogId, $token); 48 49 if (!$bayesianToken) 50 { 51 if ($degenerate) 52 { 53 $bayesianToken = $this->getFarthestToken($blogId, BayesianToken::degenerate($token)); 54 } 55 else 56 { 57 $bayesianToken = new BayesianToken($blogId, $token, 0, 0, TOKEN_DEFAULT_PROBABILITY); 58 } 59 } 60 61 array_push($bayesianTokens, $bayesianToken); 62 } 63 64 return $bayesianTokens; 65 } 66 67 /** 68 * -- Add function info here -- 69 */ 70 function getFarthestToken($blogId, $tokens) 71 { 72 $bayesianTokens = new BayesianTokens(); 73 $tokens = $bayesianTokens->getBayesianTokensFromArray($blogId, $tokens, false); 74 75 $tempArray = array(); 76 77 foreach ($tokens as $token) 78 { 79 array_push($tempArray, abs($token->getProb() - 0.5)); 80 } 81 82 arsort($tempArray); 83 $keys = array_keys($tempArray); 84 $key = $keys[0]; 85 86 return $tokens[$key]; 87 } 88 89 /** 90 * -- Add function info here -- 91 */ 92 function updateFromQuery($query) 93 { 94 $result = $this->Execute( $query ); 95 96 if( !$result ) 97 return false; 98 99 return true; 100 } 101 102 /** 103 * -- Add function info here -- 104 */ 105 function getBayesianTokenFromQuery($query, $token = false) 106 { 107 $result = $this->Execute($query); 108 109 if( !$result ) // return an empty array if error 110 return false; 111 112 if ($token === false) 113 { 114 $row = $result->FetchRow(); 115 $result->Close(); 116 117 if(!$row) 118 return false; 119 120 return $this->_mapRowToObject($row); 121 } 122 else 123 { 124 while ($row = $result->FetchRow()) 125 { 126 if ($row["token"] == $token) 127 { 128 $result->Close(); 129 return $this->_mapRowToObject($row); 130 } 131 } 132 $result->Close(); 133 134 return false; 135 } 136 } 137 138 /** 139 * -- Add function info here -- 140 */ 141 function _mapRowToObject($row) 142 { 143 return new BayesianToken($row["blog_id"], $row["token"], $row["spam_occurrences"], $row["nonspam_occurrences"], $row["prob"], $row["id"]); 144 } 145 146 /** 147 * -- Add function info here -- 148 */ 149 function _calcProb($spamOccurrences, $nonSpamOccurrences, $totalSpam, $totalNonSpam) 150 { 151 return ($spamOccurrences / $totalSpam) / (2 * ($nonSpamOccurrences / $totalNonSpam) + ($spamOccurrences / $totalSpam)); 152 } 153 154 /** 155 * -- Add function info here -- 156 */ 157 function incSpamOccurrencesFromTokensArray($blogId, $tokens, $totalSpam, $totalNonSpam) 158 { 159 $this->updateOccurrencesFromTokensArray($blogId, $tokens, 1, 0, $totalSpam, $totalNonSpam); 160 } 161 162 /** 163 * -- Add function info here -- 164 */ 165 function incNonSpamOccurrencesFromTokensArray($blogId, $tokens, $totalSpam, $totalNonSpam) 166 { 167 $this->updateOccurrencesFromTokensArray($blogId, $tokens, 0, 1, $totalSpam, $totalNonSpam); 168 } 169 170 /** 171 * -- Add function info here -- 172 */ 173 function decSpamOccurrencesFromTokensArray($blogId, $tokens, $totalSpam, $totalNonSpam) 174 { 175 $this->updateOccurrencesFromTokensArray($blogId, $tokens, -1, 0, $totalSpam, $totalNonSpam ); 176 } 177 178 /** 179 * -- Add function info here -- 180 */ 181 function decNonSpamOccurrencesFromTokensArray($blogId, $tokens, $totalSpam, $totalNonSpam) 182 { 183 $this->updateOccurrencesFromTokensArray($blogId, $tokens, 0, -1, $totalSpam, $totalNonSpam); 184 } 185 186 /** 187 * -- Add function info here -- 188 */ 189 function updateOccurrencesFromTokensArray($blogId, $tokens, $spamAddition, $nonSpamAddition, $totalSpam, $totalNonSpam ) 190 { 191 foreach ($tokens as $token) 192 { 193 $result = $this->updateOccurrences($blogId, $token, $spamAddition, $nonSpamAddition, $totalSpam, $totalNonSpam ); 194 195 if (!$result) 196 { 197 throw(new Exception("BayesianTokens::updateOccurrencesFromTokensArray: Cannot update occurrences of token '$token'.")); 198 die(); 199 } 200 } 201 } 202 203 /** 204 * -- Add function info here -- 205 */ 206 function updateOccurrences($blogId, $token, $spamAddition, $nonSpamAddition, $totalSpam, $totalNonSpam, $insert = true) 207 { 208 $prob = 0; 209 210 if ($tk = $this->getBayesianTokenFromToken($blogId, $token)) 211 { 212 $id = $tk->getId(); 213 $spamOccurrences = $tk->getSpamOccurrences() + $spamAddition; 214 $nonSpamOccurrences = $tk->getNonSpamOccurrences() + $nonSpamAddition; 215 216 if ($spamOccurrences == 0 && $nonSpamOccurrences == 0) 217 { 218 return $this->delete($id); 219 } 220 221 if ($totalSpam == 0 || $totalNonSpam == 0) 222 { 223 if ($spamAddition == 1) 224 { 225 $prob = 0.99; 226 } 227 else 228 { 229 $prob = 0.01; 230 } 231 } 232 else 233 { 234 $prob = $this->_calcProb($spamOccurrences, $nonSpamOccurrences, $totalSpam, $totalNonSpam); 235 236 if ($prob == 1) 237 { 238 $prob = 0.99; 239 } 240 else if ($prob == 0) 241 { 242 $prob = 0.01; 243 } 244 } 245 246 $query = "UPDATE " . $this->getPrefix() . "bayesian_tokens SET " . 247 "spam_occurrences = $spamOccurrences, " . 248 "nonspam_occurrences = $nonSpamOccurrences, " . 249 "prob = $prob" . 250 " WHERE id=$id;"; 251 252 return $this->updateFromQuery($query); 253 } 254 else if ($insert) 255 { 256 if ($spamAddition == 1) 257 { 258 return $this->insert($blogId, $token, 1, 0, 0.99); 259 } 260 else 261 { 262 return $this->insert($blogId, $token, 0, 1, 0.01); 263 } 264 } 265 } 266 267 /** 268 * -- Add function info here -- 269 */ 270 function updateProbabilities($blogId, $totalSpam, $totalNonSpam) 271 { 272 $query = "UPDATE " . $this->getPrefix() . "bayesian_tokens SET " . 273 "prob = (spam_occurrences / $totalSpam) / (2 * (nonspam_occurrences / $totalNonSpam) + (spam_occurrences / $totalSpam))" . 274 " WHERE blog_id = $blogId;"; 275 276 if (!$this->updateFromQuery($query)) 277 { 278 return false; 279 } 280 281 $query = "UPDATE " . $this->getPrefix() . "bayesian_tokens SET " . 282 "prob = 0.99" . 283 " WHERE prob = 1 AND blog_id = $blogId;"; 284 285 if (!$this->updateFromQuery($query)) 286 { 287 return false; 288 } 289 290 $query = "UPDATE " . $this->getPrefix() . "bayesian_tokens SET " . 291 "prob = 0.01" . 292 " WHERE prob = 0 AND blog_id = $blogId;"; 293 294 return $this->updateFromQuery($query); 295 } 296 297 /** 298 * -- Add function info here -- 299 */ 300 function insert($blogId, $token, $spamOccurrences, $nonSpamOccurrences, $prob) 301 { 302 $token = addSlashes($token); 303 $query = "INSERT INTO " . $this->getPrefix() . "bayesian_tokens (blog_id, token, spam_occurrences, nonspam_occurrences, prob) VALUES " . 304 "('$blogId', '$token', '$spamOccurrences', '$nonSpamOccurrences', '$prob')"; 305 306 $result = $this->Execute($query); 307 308 if(!$result) 309 return false; 310 311 return true; 312 } 313 314 /** 315 * -- Add function info here -- 316 */ 317 function delete($id) 318 { 319 $query = "DELETE FROM " . $this->getPrefix() . "bayesian_tokens WHERE id=$id"; 320 321 $result = $this->Execute($query); 322 323 if(!$result) 324 return false; 325 326 return true; 327 } 328 329 /** 330 * -- Add function info here -- 331 */ 332 function deleteBayesianTokensByBlogId( $blogId ) 333 { 334 $query = "DELETE FROM " . $this->getPrefix() . "bayesian_tokens WHERE blog_id=$blogId"; 335 336 $result = $this->Execute($query); 337 338 if(!$result) 339 return false; 340 341 return true; 342 } 343 } 344 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Mon Nov 26 21:04:15 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |