summaryrefslogtreecommitdiff
path: root/go/word-count/word_count.go
diff options
context:
space:
mode:
Diffstat (limited to 'go/word-count/word_count.go')
-rw-r--r--go/word-count/word_count.go30
1 files changed, 30 insertions, 0 deletions
diff --git a/go/word-count/word_count.go b/go/word-count/word_count.go
new file mode 100644
index 0000000..9ced5a7
--- /dev/null
+++ b/go/word-count/word_count.go
@@ -0,0 +1,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
+}