Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,885 questions

51,811 answers

573 users

How to get the N top words of a string by occurrences in PHP

1 Answer

0 votes
function remove_word($s, $word) {
    preg_match_all('/\w+/', strtolower($s), $matches);
    $words = $matches[0];
    $new_str = implode(' ', array_filter($words, function($w) use ($word) {
        return $w !== $word;
    }));
    
    return $new_str;
}

function get_top_n_words($s, $n) {
    // Exclude stop words (commonly used words)
    $stop_words = ["is", "a", "to", "as", "now", "by", "on", "and", "the", "it", "was"];
    foreach ($stop_words as $word) {
        $s = remove_word($s, $word);
    }
    
    // Split the string into words
    preg_match_all('/\w+/', strtolower($s), $matches);
    $words = $matches[0];
    
    // Count the occurrences of each word
    $word_count = array_count_values($words);
    
    // Sort the words by their occurrences and get the top N words
    arsort($word_count);
    $top_n_words = array_slice($word_count, 0, $n, true);
    
    return $top_n_words;
}

$s = "PHP is a general-purpose scripting language geared towards 
      web development. It was originally created by Danish-Canadian programmer 
      Rasmus Lerdorf. The PHP reference implementation is now produced by the 
      PHP Group. PHP was originally an abbreviation of Personal Home Page,
      but it now stands for the recursive acronym PHP: Hypertext Preprocessor.";

$n = 5;

$top_n_words = get_top_n_words($s, $n);

foreach ($top_n_words as $key => $value) {
    echo $key . "\n";
}


/*
run:
 
php
originally
general
purpose
scripting
 
*/

 



answered Feb 2, 2025 by avibootz

Related questions

1 answer 110 views
1 answer 93 views
1 answer 98 views
1 answer 89 views
1 answer 88 views
1 answer 93 views
...