/*
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 TypeScript arrays, maps, and sorting.
*/
// ---------------------------------------------------------------
// Tokenize text into words (simple whitespace split)
// ---------------------------------------------------------------
function tokenize(text: string): string[] {
const words: string[] = [];
// Split on whitespace
for (let w of text.split(/\s+/)) {
// Remove punctuation at the edges
while (w && /[^\w]/.test(w[0])) {
w = w.slice(1);
}
while (w && /[^\w]/.test(w[w.length - 1])) {
w = w.slice(0, -1);
}
if (w) {
words.push(w.toLowerCase());
}
}
return words;
}
// ---------------------------------------------------------------
// Count word frequencies, skipping stopwords
// ---------------------------------------------------------------
function countWordsFrequencies(
words: string[],
stopwords: Set<string>
): Map<string, number> {
const freq = new Map<string, number>();
for (const w of words) {
if (!stopwords.has(w)) {
freq.set(w, (freq.get(w) ?? 0) + 1);
}
}
return freq;
}
// ---------------------------------------------------------------
// Extract the top N most frequent words
// ---------------------------------------------------------------
function topN(
freq: Map<string, number>,
n: number
): Array<[string, number]> {
// Convert Map to array of [word, count]
const items: Array<[string, number]> = Array.from(freq.entries());
// Sort by frequency descending, then alphabetically
items.sort((a, b) => {
if (a[1] !== b[1]) {
return b[1] - a[1]; // frequency descending
}
return a[0].localeCompare(b[0]); // alphabetical
});
return items.slice(0, n);
}
// ---------------------------------------------------------------
// Main
// ---------------------------------------------------------------
const text: string =
"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.";
const stopwords: Set<string> = new Set([
"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
const words: string[] = tokenize(text);
// Count frequencies
const freq: Map<string, number> = countWordsFrequencies(words, stopwords);
// Get top n
const n: number = 7;
const topn: Array<[string, number]> = topN(freq, n);
// Print results
console.log(`Top ${n} most frequent non-stopwords:`);
for (const [word, count] of topn) {
console.log(`${word} : ${count}`);
}
/*
run:
Top 7 most frequent non-stopwords:
c : 3
language : 2
programming : 2
systems : 2
1972 : 1
computers : 1
cpu : 1
*/