Search code examples
phpsearch-enginecode-search-engine

How can I find frequency of query terms in a document in PHP


Please help me someone as to how to find frequency of each query term in a specific document in PHP. e.g. we have 2 files:

Query.txt contains data "to be not"

Data.txt contains data "to be or not to be. what to be. everything else to be."

And I need to read the file query.txt and collect terms from that file that are {"to","be"} and find the frequency of these terms in the file data.txt and if there is way to retrieve their positions too.

Result would be probably like that:

"to" appeared 4 times "be" appeared 4 times "not" appeared 1 times

Regards,


Solution

  • I believe this is what you want.

    PHP:

    <?php
    
    $words = array('to','be','not');
    
    $str = "to be or not to be. what to be. everything else to be.";
    $values = array_count_values(str_word_count($str, 1));
    
    foreach($words as $word){
        echo '"'.$word.'" appeared ';
        if(isset($values[$word])){ echo $values[$word]; }else{ echo '0'; }
        echo ' times';
    }
    
    ?>
    

    PHP with Highlight:

    <?php
    
        $words = array('to','be','not');
    
        $str = "to be or not to be. what to be. everything else to be.";
        $nStr = $str;
    
        $values = array_count_values(str_word_count($str, 1));
    
        foreach($words as $word){
            $nStr = str_replace($word,"<span style='background-color:#FEEFB3;'>".$word."</span>",$nStr);        
            echo '"'.$word.'" appeared ';
            if(isset($values[$word])){ echo $values[$word]; }else{ echo '0'; }
            echo ' times ';
        }
    
        echo '<br/>'. $nStr;
    
    ?>