gpt4 book ai didi

php - 计算单词在 PHP 文本中出现的频率

转载 作者:可可西里 更新时间:2023-11-01 12:46:37 25 4
gpt4 key购买 nike

在 php 中,我需要加载一个文件并获取所有单词并回显单词以及每个单词在文本中出现的次数,(我还需要它们按最常用词的降序显示在顶部)★✩

最佳答案

这是一个例子:

$text = "A very nice únÌcÕdë text. Something nice to think about if you're into Unicode.";

// $words = str_word_count($text, 1); // use this function if you only want ASCII
$words = utf8_str_word_count($text, 1); // use this function if you care about i18n

$frequency = array_count_values($words);

arsort($frequency);

echo '<pre>';
print_r($frequency);
echo '</pre>';

输出:

Array
(
[nice] => 2
[if] => 1
[about] => 1
[you're] => 1
[into] => 1
[Unicode] => 1
[think] => 1
[to] => 1
[very] => 1
[únÌcÕdë] => 1
[text] => 1
[Something] => 1
[A] => 1
)

还有 utf8_str_word_count() 函数,如果您需要的话:

function utf8_str_word_count($string, $format = 0, $charlist = null)
{
$result = array();

if (preg_match_all('~[\p{L}\p{Mn}\p{Pd}\'\x{2019}' . preg_quote($charlist, '~') . ']+~u', $string, $result) > 0)
{
if (array_key_exists(0, $result) === true)
{
$result = $result[0];
}
}

if ($format == 0)
{
$result = count($result);
}

return $result;
}

关于php - 计算单词在 PHP 文本中出现的频率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2123236/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com