function check($text) { global $PROB_ALLSAFE, $PROB_ALLSPAM; $arr = decompose($text); $result = sql("SELECT * FROM wordlist WHERE word = '*'", 1); if ($result->num_rows > 0) { $row = $result->fetch_assoc(); $totalspam = $row['spam']; $totalsafe = $row['safe']; } else { die('Not initialized!'); } $probpos = 1; $probneg = 1; foreach ($arr as $word) { $result = sql("SELECT * FROM wordlist WHERE word = '{$word}'", 1); if ($result->num_rows > 0) { $row = $result->fetch_assoc(); $sp = $row['spam']; $sf = $row['safe']; if ($sp == 0 || $sf == 0) { continue; } $probspam = $sp / $totalspam; $probsafe = $sf / $totalsafe; $prob = $probspam * $PROB_ALLSPAM / ($probspam * $PROB_ALLSPAM + $probsafe * $PROB_ALLSAFE); $probpos *= $prob; $probneg *= 1 - $prob; } else { continue; } } $prob = $probpos / ($probpos + $probneg); return $prob; }
/** * Decompose a sequence recusively * * @param array $decomp_map Decomposition mapping, passed by reference * @param string $decomp_seq Decomposition sequence as decimal codepoints separated with a space * @return string Decomposition sequence, fully decomposed */ function decompose(&$decomp_map, $decomp_seq) { $ret = array(); foreach (explode(' ', $decomp_seq) as $cp) { if (isset($decomp_map[$cp])) { $ret[] = decompose($decomp_map, $decomp_map[$cp]); } else { $ret[] = $cp; } } return implode(' ', $ret); }