Current Location: Home> Latest Articles> How to use statistical results to generate word cloud graph data?

How to use statistical results to generate word cloud graph data?

M66 2025-06-07

In many application scenarios, we need to analyze the frequency of each word appearing in a piece of text. The common practice is to count word frequency and generate word cloud maps. Word cloud map is an intuitive way of displaying that can help us quickly identify the most common keywords in text. This article will introduce how to use PHP's array_count_values ​​function to count word frequency and generate the data required for word cloud graphs.

1. Basic concepts

First, we need to understand several concepts:

  • Word frequency statistics : that is, count the number of times each word appears in the text.

  • Word cloud diagram : Different font sizes and colors represent the frequency of the word. The higher the frequency, the larger the font, the brighter the color.

In PHP, the array_count_values ​​function can easily count the number of times each value appears in an array. This makes it very suitable for stating word frequency.

2. Code implementation

2.1 Get text and split into words

First, let's say we have a piece of text that we can split using PHP's built-in string processing function to generate an array of words. For example, we can use the str_word_count function to extract words in text.

 $text = "PHPIt is a widely used open source scripting language。PHPThe main feature is simplicity、flexible、Easy to learn。";
$words = str_word_count($text, 1);  // 1 Indicates that return word array

str_word_count splits the text into single words and returns an array containing all words.

2.2 Statistical word frequency

Next, we can use array_count_values ​​to count the number of times each word appears in the array.

 $word_count = array_count_values($words);
print_r($word_count);

array_count_values ​​returns an associative array, the key is the word, and the value is the number of times the word appears in the text. For example:

 Array
(
    [PHP] => 2
    [yes] => 2
    [A sort of] => 1
    [widely] => 1
    [use] => 1
    [of] => 2
    [Open source] => 1
    [Script] => 1
    [language] => 1
    [main] => 1
    [Features] => 1
    [concise] => 1
    [flexible] => 1
    [easy] => 1
    [study] => 1
)
2.3 Prepare word cloud map data

The data of a word cloud map usually contains words and their frequency. In order to generate a word cloud map, we need to organize the words and their frequency information. This data can be stored in JSON format for interaction with the front-end.

 $word_cloud_data = [];
foreach ($word_count as $word => $count) {
    $word_cloud_data[] = [
        'text' => $word,
        'weight' => $count
    ];
}

$json_data = json_encode($word_cloud_data);

json_encode will convert the array to JSON format, and the returned data can be used by front-end JavaScript code to generate word cloud diagrams.

2.4 Rendering word cloud diagram

Next, we can pass the generated word frequency data to the front-end through the API, which uses JavaScript libraries such as WordCloud.js to render word cloud maps. Assuming that you have returned the data from the PHP backend to the frontend through the interface, the frontend JavaScript can process the data in this way and generate a word cloud diagram:

 <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Word cloud diagram display</title>
    <script src="https://m66.net/wordcloud.js"></script>
</head>
<body>
    <div id="wordcloud"></div>

    <script>
        const wordCloudData = <?php echo $json_data; ?>;

        WordCloud(document.getElementById('wordcloud'), {
            list: wordCloudData.map(item => [item.text, item.weight])
        });
    </script>
</body>
</html>

In this example, WordCloud.js generates a word cloud graph based on word frequency data obtained from the PHP backend. The frequency of occurrence of each word determines its display size in the figure.

3. Summary

By using PHP's array_count_values ​​function, we can easily count word frequency in text and convert the statistics into a data format suitable for word cloud graphs. By combining the front-end WordCloud.js and other libraries, we can display word cloud graph data, providing users with an intuitive text analysis tool.

I hope this article can help you quickly get started with word frequency statistics in PHP and generate related operations for word cloud diagrams. If you have any questions, please leave a message to discuss!