Sha256: 066c44398ad1a7db532289baacae70fbc39952a4ad543538dd045317f3c37d6f
Contents?: true
Size: 622 Bytes
Versions: 122
Compression:
Stored size: 622 Bytes
Contents
package wordcount import ( "regexp" "strings" ) // 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
122 entries across 122 versions & 1 rubygems