// Return the word frequencies of the text argument.
// Split load optimally across processor cores.
func WordCount(text string) map[string]int {
var wg sync.WaitGroup
// wg := new(sync.WaitGroup)
var mu sync.Mutex
freq := make(map[string]int)
text = regexp.MustCompile(`[^a-zA-Z0-9 ]+`).ReplaceAllString(text, "")
text = strings.ToLower(text)
arr := strings.Fields(text)
wg.Add(len(arr))
for _, v := range arr {
// wg.Add(1)
// defer wg.Done()
mu.Lock()
go func(v string) { freq[v]++ }(v)
wg.Done()
mu.Unlock()
}
wg.Wait()
return freq
}
func main() {
// read in DataFile as a string called data
data, err := os.ReadFile(DataFile)
if err != nil {
log.Fatal(err)
}
numRuns := 100
runtimeMillis := benchmark(string(data), numRuns)
printResults(runtimeMillis, numRuns)
}
So basically this is how it looks like, I have 2 other functions called (printResults and benchMark). So anyways the question is about using concurrency to count the number of words in a map or honestly I don't even know where I should use concurrency and I'd really appreciate it if someone could help me out here.