dtiu94034 2017-01-04 14:13 采纳率: 0%
浏览 23
已采纳

PHP计数器返回所有停用词及找到多少次?

I can't seem to find anything that solves the following and thought i would ask for help.

I am trying to retrieve an Array of all the stopwords (including phrase matched words) within a string and also how many times each has been found. The following code is the nearest i have got to, which will return a $counter value for the total number of stopwords found (single instance only though, not multiple counts) and obviously does not list the words.

I have tried using preg_match_all and various Array outputs and all have resulted in "head scratching" errors.

Any help would be appreciated.

// test string
$string = 'a string to see how many times all stopwords words are found, must include phrases and return an array of all stopwords and how many times each was found';

// test stopwords
$stopwords = array('all','times','words are found');

function counter_words($string, $stopwords) {

$counter = 0;   

foreach ($stopwords as $stopword) {

    $pattern = '/\b' . $stopword . '\b/i';              
    if (preg_match($pattern, $string)) {
        $counter++;
    }
}

return $counter;
}

// test - output counter only
echo counter_words($string, $stopwords);

With some modification, I am hoping to be able to return an array (presumably an associated array) where i can echo out something similar to:

Word/phrase found: "words are found", instances found "1"

Word/phrase found: "times", instances found "1"

etc...

Many Thanks

James

  • 写回答

2条回答 默认 最新

  • duang8642 2017-01-04 14:18
    关注

    You are only increasing the counter if there is a match, not on the number of matches. Use preg_match_all and count the number of matched results.

    $string = 'a string to see how many times all stopwords words are found, must include phrases and return an array of all stopwords and how many times each was found';

    // test stopwords
    $stopwords = array('all','times','words are found');
    
    function counter_words($string, $stopwords) {
    
    $counter = 0;   
    
    foreach ($stopwords as $stopword) {
        $pattern = '/\b' . $stopword . '\b/i';              
            if (preg_match_all($pattern, $string, $matches)) {
                 $counter += count($matches[0]);
            }
        }
        return $counter;
    }
    
    // test - output counter only
    echo counter_words($string, $stopwords);
    

    Demo: https://eval.in/709349

    You also could implode the $stopwords with | if there will never be a special character in there, then you'd not need the foreach.

    ....

    or for a count of each matched term (this also uses the implode approach).

    $string = 'a string to see how many times all stopwords words are found, must include phrases and return an array of all stopwords and how many times each was found';

    // test stopwords
    $stopwords = array('all','times','words are found');
    
    function counter_words($string, $stopwords) {
        $pattern = '/\b' . implode('|', $stopwords) . '\b/i';
        preg_match_all($pattern, $string, $matches);
        return !empty($matches) ? array_count_values($matches[0]) : 'No matches found';
    }
    
    // test - output counter only
    print_r(counter_words($string, $stopwords));
    

    Demo: https://eval.in/709369

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)