Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 计算文本中的单词频率?_Php_Algorithm_Word Frequency - Fatal编程技术网

Php 计算文本中的单词频率?

Php 计算文本中的单词频率?,php,algorithm,word-frequency,Php,Algorithm,Word Frequency,可能重复: 我希望编写一个php函数,它以字符串作为输入,将其拆分为单词,然后返回一个按每个单词出现频率排序的单词数组 在算法上最有效的方法是什么?您最好的选择是: 范例 $words = 'A string with certain words occuring more often than other words.'; print_r( array_count_values(str_word_count($words, 1)) ); 输出 Array ( [A] =&g

可能重复:

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


在算法上最有效的方法是什么?

您最好的选择是:

范例

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

我希望它取决于文本的大小。在任何情况下,都有成堆这样的解析器,最有效的编程方法是重用,而不是编写自己的解析器。不过,谷歌的“单词频率计数器php”也取决于“单词”的含义。当“'s”是所有格标记时,它算作一个词吗?那么当它是“是”的收缩时呢?其他宫缩情况如何?如果你只是想用空格或连字符分隔(就像你手机上的T9一样),那么你最好使用下面Gordon建议的内置内容。StackOverflow之前的两个问题都是关于同一主题的。应该是有用的。[计算单词在PHP中出现的频率][1][PHP:对给定字符串中的单词实例进行排序和计数][2][1]:[2]: