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

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

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

Disclosure: My content contains affiliate links.

43,102 questions

55,976 answers

573 users

How to find the N most frequent non‑stopwords in a text in PHP

1 Answer

0 votes
/*
    This program finds the N most frequently appearing words in a text
    after removing stopwords. It demonstrates clean structure, clear
    comments, and efficient use of PHP arrays and sorting.
*/

// ---------------------------------------------------------------
// Tokenize text into words (simple whitespace split)
// ---------------------------------------------------------------
function tokenize(string $text): array {
    $words = [];

    // Split on whitespace
    foreach (preg_split('/\s+/', $text) as $w) {

        // Remove punctuation at the edges
        while ($w !== '' && preg_match('/[[:punct:]]/', $w[0])) {
            $w = substr($w, 1);
        }
        while ($w !== '' && preg_match('/[[:punct:]]/', $w[strlen($w) - 1])) {
            $w = substr($w, 0, -1);
        }

        if ($w !== '') {
            $words[] = strtolower($w);
        }
    }

    return $words;
}

// ---------------------------------------------------------------
// Count word frequencies, skipping stopwords
// ---------------------------------------------------------------
function count_words_frequencies(array $words, array $stopwords): array {
    $freq = [];

    foreach ($words as $w) {
        if (!in_array($w, $stopwords, true)) {
            if (!isset($freq[$w])) {
                $freq[$w] = 0;
            }
            $freq[$w]++;
        }
    }

    return $freq;
}

// ---------------------------------------------------------------
// Extract the top N most frequent words
// ---------------------------------------------------------------
function top_n(array $freq, int $n): array {
    // Convert associative array to list of [word, count]
    $items = [];
    foreach ($freq as $word => $count) {
        $items[] = [$word, $count];
    }

    // Sort by frequency descending, then alphabetically
    usort($items, function ($a, $b) {
        if ($a[1] !== $b[1]) {
            return $b[1] <=> $a[1]; // frequency descending
        }
        return $a[0] <=> $b[0];     // alphabetical
    });

    return array_slice($items, 0, $n);
}

// ---------------------------------------------------------------
// Main
// ---------------------------------------------------------------
$text =
    "C is a general-purpose programming language created in 1972 by " .
    "Dennis Ritchie. C gives programmers direct access to the features " .
    "of CPU. It has been and continues to be used to implement " .
    "operating systems (especially kernels) and device " .
    "drivers. C programming language used on computers ranging from " .
    "supercomputers to microcontrollers and embedded systems.";

$stopwords = [
    "the","is","a","to","how","after","but","this","for","by","in",
    "and","can","content","be","you","yes","no","next","about","used",
    "access", "been", "continues"
];

// Tokenize
$words = tokenize($text);

// Count frequencies
$freq = count_words_frequencies($words, $stopwords);

// Get top n
$n = 7;
$topn = top_n($freq, $n);

// Print results
echo "Top $n most frequent non-stopwords:\n";
foreach ($topn as [$word, $count]) {
    echo "$word : $count\n";
}



/*
run:

Top 7 most frequent non-stopwords:
c : 3
language : 2
programming : 2
systems : 2
1972 : 1
computers : 1
cpu : 1

*/

 



answered Aug 30 by avibootz
...