/*
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 Kotlin collections and sorting.
*/
// ---------------------------------------------------------------
// Tokenize text into words (simple whitespace split)
// ---------------------------------------------------------------
fun tokenize(text: String): List<String> {
val words = mutableListOf<String>()
// Split on whitespace
for (raw in text.split(Regex("\\s+"))) {
var w = raw
// Remove punctuation at the edges
while (w.isNotEmpty() && w.first().isLetterOrDigit().not()) {
w = w.drop(1)
}
while (w.isNotEmpty() && w.last().isLetterOrDigit().not()) {
w = w.dropLast(1)
}
if (w.isNotEmpty()) {
words += w.lowercase()
}
}
return words
}
// ---------------------------------------------------------------
// Count word frequencies, skipping stopwords
// ---------------------------------------------------------------
fun countWordsFrequencies(
words: List<String>,
stopwords: Set<String>
): Map<String, Int> {
val freq = mutableMapOf<String, Int>()
for (w in words) {
if (w !in stopwords) {
freq[w] = (freq[w] ?: 0) + 1
}
}
return freq
}
// ---------------------------------------------------------------
// Extract the top N most frequent words
// ---------------------------------------------------------------
fun topN(freq: Map<String, Int>, n: Int): List<Pair<String, Int>> {
val items = freq.toList()
// Sort by frequency descending, then alphabetically
val sorted = items.sortedWith(
compareByDescending<Pair<String, Int>> { it.second }
.thenBy { it.first }
)
return sorted.take(n)
}
// ---------------------------------------------------------------
// Main
// ---------------------------------------------------------------
fun main() {
val 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."
val stopwords = setOf(
"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
val words = tokenize(text)
// Count frequencies
val freq = countWordsFrequencies(words, stopwords)
// Get top n
val n = 7
val topn = topN(freq, n)
// Print results
println("Top $n most frequent non-stopwords:")
for ((word, count) in topn) {
println("$word : $count")
}
}
/*
run:
Top 7 most frequent non-stopwords:
c : 3
language : 2
programming : 2
systems : 2
1972 : 1
computers : 1
cpu : 1
*/