summaryrefslogtreecommitdiff
path: root/go/word-count/word_count.go
blob: 9ced5a7ea1682fd1c5e28c52859ae3b6cd7bd697 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package wordcount

import (
	"strings"
	"unicode"
)

const testVersion = 2

// Use this return type.
type Frequency map[string]int

func normalize(s string) string {
	var ret []rune
	for _, r := range s {
		if unicode.In(r, unicode.Letter, unicode.Number, unicode.Space) {
			ret = append(ret, unicode.ToLower(r))
		}
	}
	return string(ret)
}

// Just implement the function.
func WordCount(phrase string) Frequency {
	wc := make(Frequency)
	for _, v := range strings.Fields(normalize(phrase)) {
		wc[v]++
	}
	return wc
}