 Count inverse document frequency (idf) in every documents
<p>I wanna count idf, the formula is <code>IDF=log(D/df)</code> that <code>D</code> is total data and <code>df</code> is many data that contain the searched words.
from the tables :
1. <code>tb_stemming</code></p>

<pre><code> ===========================================================================
 |stem_id | stem_before | stem_after | stem_freq | sentence_id |document_id|
 ===========================================================================
 |    1   |    Data     |    Data    |     1      |      0     |     1     |
 |    2   |   Discuss   |   Discuss  |     1      |      1     |     1     |
 |    3   |   Mining    |    Min     |     1      |      0     |     2     |
 ===========================================================================
</code></pre>

<p>here's the code :</p>

<pre><code>countIDF($total_sentence,$doc_id);
</code></pre>

<p>that <code>$total_sentence</code> is 
<code>Array ( [0] =&gt; 644 [1] =&gt; 79 [2] =&gt; 264 [3] =&gt; 441 [4] =&gt; 502 [5] =&gt; 18 [6] =&gt; 352 [7] =&gt; 219 [8] =&gt; 219 )</code> </p>

<pre><code>function countIDF($total_sentence, $doc_id) {
    foreach ($total_sentence as $doc_id =&gt; $total_sentences){
       $idf = 0;
       $query1 = mysql_query("SELECT document_id, DISTINCT(stem_after) AS unique_token FROM tb_stemming group by stem_after where document_id='$doc_id'  ' ");
       while ($row = mysql_fetch_array($query)) {
           $token  = $row['unique_token'];
           $doc_id = $row['document_id'];
           $ndw    = countNDW($token);

           $idf = log($total_sentences / $ndw)+1;
           $q   = mysql_query("INSERT INTO tb_idf VALUES ('','$doc_id','$token','$ndw','$idf') ");
        }
     }
}
</code></pre>

<p>and the function of countNDW is :</p>

<pre><code>function countNDW ($word) {
    $query = mysql_query("SELECT stem_after, COUNT( DISTINCT sentence_id ) AS ndw FROM `tb_stemming` WHERE stem_after = '$word' GROUP BY stem_after");
    while ($row = mysql_fetch_array($query)) {
        $ndw = $row['ndw'];
    }
    return $ndw;
}
</code></pre>

<p>It can't worked well, especially in call from database. All I need is to count in every <code>document_id</code>. How to define it in my code ? please, help me.. thank you so much :)</p>
