title: Naive Bayes in PHP
body: <p>I’m trying to implement a <a href="http://en.wikipedia.org/wiki/Naive_Bayes_classifier" rel="nofollow">Naive Bayes classifier</a> in PHP, and I found <a href="https://github.com/gburtini/Learning-Library-for-PHP/blob/master/lib/parametric/naivebayes.php" rel="nofollow">this script</a>.</p>
  
  <p>I’m running the script on a standard LAMP stack (php-fpm), and I am getting a an error:</p>
  
  <blockquote>
    <p>Fatal error: Call to undefined function 3184791920() in
    /location/file.php on line 73</p>
  </blockquote>
  
  <p>But I can't figure out what is causing this, since there is no <code>3184791920()</code> function. I assume it has something to do with the hashing:</p>
  
  <pre><code>define("LL_NB_HASH_FUNCTION", "crc32");
  </code></pre>
  
  <p>Here is the full text of my implementation:</p>
  
  <pre><code>&lt;?php
  
  global $LL_NB_STOP_WORDS;
  $LL_NB_STOP_WORDS = array("a", "about", "above", "...");
  define("LL_NB_HASH_FUNCTION", "crc32");// crc32 is the fastest built in hash function.
  
  // $xs is a bunch of "strings" and ys are their labels.
  function ll_naivebayes($xs, $ys, $testStrings) {
     $topicWords = array();
     foreach($xs as $i=&gt;$x) {
        if(isset($topicWords[$ys[$i]]))
           $topicWords[$ys[$i]] .= $x;
        else
           $topicWords[$ys[$i]] = $x;
     }
     $topicWords = _ll_computeWordCounts($topicWords);   // get the number of each word, by topic.
  
     $probWordsGivenTopic = array();   // probability of each word in a given topic.
     $countTopics = array();
  
     foreach($topicWords as $topicIndex=&gt;$xWordCounts) {
        $totalWordsTopic = array_sum($xWordCounts);
        $countTopics[$topicIndex] = $total_wordsTopic;
  
        foreach($xCount as $hash=&gt;$count) {
           $probWordsGivenTopic[$topicIndex][$hash] = ($count/$totalWordsTopic);
        }
     }
  
     $probTopics = array(); // probability of a given topic (number of words / total words), i.e., relative frequency of topics in terms of words
     foreach($countTopics as $i=&gt;$topicCount) {
        $probTopics[$i] = ($topicCount/$totalWords);
     }
  
     if(!is_array($testStrings))
        $testStrings = array($testStrings);
  
     // process the input testStrings array
     $return = array();
     foreach($testStrings as $i=&gt;$string) {
        $testStringWords = _ll_computeWordCount($string);
        $topicsPosterior = array();
  
        foreach($probTopics as $key=&gt;$probTopic) {
           $p = $probTopic;
  
           foreach($testStringWords as $hash=&gt;$count) {
              if(isset($probWordsGivenTopic[$key][$hash]))
                 $p *= $probWordsGivenTopic[$key][$hash] * $count;
           }
           $topicsPosterior[$key] = $p;
        }
        sort($topicsPosterior);
        $return[$i] = $topicsPosterior;
     }
     return $return;
  }
  
  function _ll_computeWordCounts($strings) {
     $wcs = array();
     foreach($strings as $string) {
        $wcs[] = _ll_computeWordCount($string);
     }
     return $wcs;
  }
  
  function _ll_computeWordCount($string) {
        $string = trim($string);
        $string = explode(' ', $string);
        natcasesort($string);
        $hash = LL_NB_HASH_FUNCTION;
  
        $words = array();
        for($i=0, $count = count($string); $i&lt;$count; $i++) {
           $word = trim($string[$i]);
           if(preg_match('/[^a-zA-Z\']/', $word))
              continue;
  
           $hash = (string) $hash($word);
           if(!isset($words[$hash]))
              $words[$hash] = 1; //$words[$hash] = array('word'=&gt;$word, 'count'=&gt;1);
           else
              $words[$hash]++; //$words[$hash]['count']++;
        }
  
        return $words;
  }
  
  $output = ll_naivebayes(array("Will I marry John", "Marriage is cool", "A string about Windows XP"), array("marriage", "marriage", "windows"), array("this is about marriage"));
  
  ?&gt;
  </code></pre>