exercism/go/word-count/word_count.go

29 lines
609 B
Go
Raw Normal View History

2016-08-13 23:20:14 +00:00
package wordcount
import (
"regexp"
"strings"
)
// TestVersion is an exercism thing
const TestVersion = 1
// Frequency is the return type
type Frequency map[string]int
// WordCount returns how many of each word in 'phrase'
func WordCount(phrase string) Frequency {
ret := make(Frequency)
words := strings.Split(phrase, " ")
cleanUp, _ := regexp.Compile("[^A-Za-z0-9]")
for _, v := range words {
// The test cases assume that we're stripping non-alphanumeric runes
// and making it lower case
v = strings.ToLower(cleanUp.ReplaceAllString(v, ""))
if v != "" {
ret[v]++
}
}
return ret
}