什么是阿拉伯字符密度以及如何在 PHP 中创建脚本?

What is Arabic characters Density and how to create a script in PHP?

我想显示阿拉伯语文本中的单词密度。以下代码适用于英文字符,但不支持阿拉伯文本。如何指定文本中阿拉伯语单词的密度?

<?php
$str = "I am working on a project where I have to find out the keyword density of the page on the basis of URL of that page. But I am not aware actually what \"keyword Density of a page\" actually means? and also please tell me how can we create a PHP script which will fetch the keyword density of a web page.";

// str_word_count($str,1) - returns an array containing all the words found inside the string
$words = str_word_count(strtolower($str),1);
$numWords = count($words);

// array_count_values() returns an array using the values of the input array as keys and their frequency in input as values.
$word_count = (array_count_values($words));
arsort($word_count);

foreach ($word_count as $key=>$val) {
    echo "$key = $val. Density: ".number_format(($val/$numWords)*100)."%<br/>\n";
}
?>

示例输出:

of = 5. Density: 8%
a = 4. Density: 7%
density = 3. Density: 5%
page = 3. Density: 5%
...

问题是 str_word_count 不将阿拉伯字符计为“单词字符”。您可以将所需的“单词字符”作为第三个参数传递,也可以只传递 explode 字符串并使用 for 循环计算单词数。