【问题标题】:How can I find frequency of query terms in a document in PHP [closed]如何在 PHP 文档中找到查询词的频率 [关闭]
【发布时间】:2014-06-20 05:33:37
【问题描述】:

请帮助我了解如何在 PHP 的特定文档中查找每个查询词的频率。例如我们有 2 个文件:

Query.txt 包含“不应该”的数据

Data.txt 包含数据“成为或不成为。要成为什么。要成为的一切。”

我需要读取文件 query.txt 并从该文件中收集 {"to","be"} 的术语,并在文件 data.txt 中找到这些术语的频率,以及是否有办法检索他们的立场也是。

结果大概是这样的:

“to”出现了 4 次 “be”出现了4次 "not" 出现了 1 次

问候,

【问题讨论】:

  • “查询条件”?你到底什么意思?你能给我们一些这些文件的样本数据,以及预期的结果吗?
  • 数据不足 - 我们无能为力。
  • 你需要看看str_word_count()
  • str_word_count() 将查找文档中所有单词的频率,但我想提取一些单词。

标签: php search-engine code-search-engine


【解决方案1】:

我相信这就是你想要的。

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:

<?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;

?>

【讨论】:

  • 谢谢朋友,这对我有很大帮助:)
  • 没问题,乐于助人!
猜你喜欢
  • 2014-05-18
  • 1970-01-01
  • 2015-12-31
  • 1970-01-01
  • 1970-01-01
  • 2018-08-22
  • 2013-06-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多