计数词频的文本? [英] Count word frequency in a text?

查看:163
本文介绍了计数词频的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  <一href="http://stackoverflow.com/questions/2984786/php-sort-and-count-instances-of-words-in-a-given-string">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 — 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,因为问题是包含了相同的答案至少有两个问题,一个重复

这篇关于计数词频的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆