计数词频的文本?词频、文本

2023-09-10 23:39:17 作者:对自己的心说声对不起

可能重复:   php:排序和计数在给定的字符串文字的实例

我期待写一个PHP函数,它接受一个字符串作为输入,分割成单词,然后返回字排序每个字的occurence频率的数组。

I am looking to write a php function which takes a string as input, splits it into words and then returns an array of words sorted by the frequency of occurence of each word.

什么是解决这个问题的最算法有效的方式?

What's the most algorithmically efficient way of accomplishing this ?

推荐答案

您最好的选择是这些:

str_word_count - 返回的信息有关在字符串中使用的话 array_count_values​​ - 计算所有数组的值 str_word_count — Return information about words used in a string array_count_values — Counts all the values of an array

示例

$words = 'A string with certain words occuring more often than other words.';
print_r( array_count_values(str_word_count($words, 1)) );

输出

Array
(
    [A] => 1
    [string] => 1
    [with] => 1
    [certain] => 1
    [words] => 2
    [occuring] => 1
    [more] => 1
    [often] => 1
    [than] => 1
    [other] => 1
)

标记CW,因为问题是包含了相同的答案至少有两个问题,一个重复的

 
精彩推荐
图片推荐