package main
import (
"fmt"
"sort"
"strings"
"unicode"
)
/*
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 Go slices, maps, and sorting.
*/
// ---------------------------------------------------------------
// Tokenize text into words (simple whitespace split)
// ---------------------------------------------------------------
func tokenize(text string) []string {
words := []string{}
// Split on whitespace
for _, w := range strings.Fields(text) {
// Remove punctuation at the edges
for len(w) > 0 && unicode.IsPunct(rune(w[0])) {
w = w[1:]
}
for len(w) > 0 && unicode.IsPunct(rune(w[len(w)-1])) {
w = w[:len(w)-1]
}
if w != "" {
words = append(words, strings.ToLower(w))
}
}
return words
}
// ---------------------------------------------------------------
// Count word frequencies, skipping stopwords
// ---------------------------------------------------------------
func countWordsFrequencies(words []string, stopwords map[string]bool) map[string]int {
freq := make(map[string]int)
for _, w := range words {
if !stopwords[w] {
freq[w]++
}
}
return freq
}
// ---------------------------------------------------------------
// Extract the top N most frequent words
// ---------------------------------------------------------------
func topN(freq map[string]int, n int) [][2]interface{} {
items := make([][2]interface{}, 0, len(freq))
// Convert map to list of [word, count]
for word, count := range freq {
items = append(items, [2]interface{}{word, count})
}
// Sort by frequency descending, then alphabetically
sort.Slice(items, func(i, j int) bool {
ci := items[i][1].(int)
cj := items[j][1].(int)
wi := items[i][0].(string)
wj := items[j][0].(string)
if ci != cj {
return ci > cj
}
return wi < wj
})
if len(items) > n {
return items[:n]
}
return items
}
// ---------------------------------------------------------------
// Main
// ---------------------------------------------------------------
func 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 := map[string]bool{
"the": true, "is": true, "a": true, "to": true, "how": true,
"after": true, "but": true, "this": true, "for": true, "by": true,
"in": true, "and": true, "can": true, "content": true, "be": true,
"you": true, "yes": true, "no": true, "next": true, "about": true,
"used": true, "access": true, "been": true, "continues": true,
}
// Tokenize
words := tokenize(text)
// Count frequencies
freq := countWordsFrequencies(words, stopwords)
// Get top n
n := 7
topn := topN(freq, n)
// Print results
fmt.Printf("Top %d most frequent non-stopwords:\n", n)
for _, item := range topn {
fmt.Printf("%s : %d\n", item[0].(string), item[1].(int))
}
}
/*
run:
Top 7 most frequent non-stopwords:
c : 3
language : 2
programming : 2
systems : 2
1972 : 1
computers : 1
cpu : 1
*/