PHP 反对 Return 所有停用词及其被发现次数?

PHP Counter To Return All Stopwords & How Many Times They Were Found?

我似乎找不到任何解决以下问题的方法,我想我会寻求帮助。

我正在尝试检索一个字符串中所有停用词(包括短语匹配词)的数组,以及每个停用词被找到的次数。下面的代码是我所得到的最接近的代码,它将 return 找到的停用词总数的 $counter 值(尽管只是单个实例,而不是多个计数)并且显然没有列出这些词。

我尝试使用 preg_match_all 和各种数组输出,但都导致了 "head scratching" 错误。

如有任何帮助,我们将不胜感激。

// 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);

通过一些修改,我希望能够 return 一个数组(大概是一个关联的数组),我可以在其中回显类似于以下内容的内容:

Word/phrase 发现:"words are found",发现实例“1”

Word/phrase 发现:"times",发现实例“1”

等...

非常感谢

詹姆斯

如果有匹配项,您只会增加计数器,而不是匹配项的数量。使用preg_match_all并计算匹配结果的数量。

$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);

演示:https://eval.in/709349

你也可以 implode $stopwords| 如果那里永远不会有特殊字符,那么你就不需要 foreach

.....

或每个匹配项的计数(这也使用 implode 方法)。

$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));

演示:https://eval.in/709369

看看这个。它将 return 计算单个数组中的所有单词:

$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';


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

function counter_words($string, $stopwords) {
    $output = array();

    foreach ($stopwords as $stopword) {
        $pattern = '/\b' . $stopword . '\b/i';
        preg_match_all($pattern, $string, $matches);
        $output[$stopword] = count($matches[0]);
    }
    return $output;
}

echo '<pre>';print_r(counter_words($string, $stopwords));exit;

在这里测试https://eval.in/709375