什么是阿拉伯字符密度和如何在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循环计算单词。

最新更新