/*
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
*/