Sha256: bcba72943a82a90ee3f8f440e4245d2afa7f513fded19831991b2e5573cc1e92
Contents?: true
Size: 645 Bytes
Versions: 217
Compression:
Stored size: 645 Bytes
Contents
package wordcount import ( "regexp" "strings" ) const testVersion = 3 // Frequency is a map of the frequency of occurrence keyed to the unique word. type Frequency map[string]int // WordCount returns the map of frequency of words based on the input phrase. func WordCount(phrase string) Frequency { freq := Frequency{} for _, word := range strings.Fields(normalize(phrase)) { word = strings.Trim(word, "'") freq[word]++ } return freq } func normalize(phrase string) string { // Allow for apostrophes in words. r, _ := regexp.Compile(`[^\w|']`) phrase = strings.ToLower(phrase) return r.ReplaceAllLiteralString(phrase, " ") }
Version data entries
217 entries across 217 versions & 1 rubygems