javascript - 从 PHP 中的拆分文本中获取出现次数最多的前 5 个内容

标签 javascript php

我对这段代码有疑问。我想在 php 中获取分割文本中计数最多的单词,有人可以帮助我吗?代码显示它给出了分割文本的输出,我使用 php 的 count 函数来获取每个特定单词的准确计数,问题是我想获得计数最多的单词,至少有前 5 个单词,因为我将使用此函数用于制作图表或分析图

Picture of expected output

<?php
$str = "one two three four five six set one two three four five six set one three four five six set one two three four five six set";
$a = preg_split("/[\s]/", $str);
foreach($a as $arr)
{
	echo $arr."<br>";
}


?>

<?php
$text = $str;

// $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>';


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

$interesting = array($arr);
$string = "one two three four five six six six six six set one two three four five six set one three four five six set one two three four five six set one one one one";

// Explode into array
$array = explode(" ", $string);

// Group the values 
$count = array_count_values($array); 

// Sort the grouping by highest occurence to lowest
arsort($count);

// Get the keys of the most occurring
$keys = array_keys($count);

// compare key against the $interesting array for what you're interested in
$most_occurring = '';
foreach ($keys as $i) {
  if (in_array($i, $interesting, true)) {
      $most_occurring = $i;
      break;
  }
}

// Print output
echo "Most occurring $most_occurring, $count[$most_occurring] occurences.";

?>

最佳答案

这样的事情怎么样?

  1. explode 将字符串转换为数组
  2. array_count_values 计算受骗者的数量
  3. arsort 对数组进行排序
  4. array_slice 分块获得前 5 名

代码:

$str = "one two three four five six set one two three four five six set one three four five six set one two three four five six set";
$arr = explode(' ', $str);
$vals = array_count_values($arr);
arsort($vals);
$top_5 = array_slice($vals, 0, 5); 
print_r($top_5);

关于javascript - 从 PHP 中的拆分文本中获取出现次数最多的前 5 个内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46009707/

相关文章:

javascript - 同时运行 webpack-dev-server 时出现 WDS : Disconnected! 循环

javascript - 主选项卡需要仅使用 html、css 和 javascript 嵌套的子选项卡

php - 在 null 上调用成员函数 helper()

javascript - 在 Koa 发送响应后运行代码

javascript - 有条件的 ng-click

javascript - Shiny :使用shinyjs禁用tabPanel()

javascript - PHP FTP 上传 BLOB TEMP 图像

php - date()中减去n值

php - 页面导航的快速代码

PHP 反射 : get constant's doc comment