计算文字中的单词频率?

时间:2011-01-12 15:19:52

标签: php algorithm word-frequency

  

可能重复:
  php: sort and count instances of words in a given string

我希望编写一个php函数,它将一个字符串作为输入,将其拆分为单词,然后返回按每个单词出现频率排序的单词数组。

实现此目的的算法效率最高的方法是什么?

1 个答案:

答案 0 :(得分:24)

你最好的选择是:

实施例

$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,因为问题是至少包含相同答案的其他两个问题的重复