// Package wordcount counts words in text.
package wordcount
import (
"strings"
"unicode"
)
// Frequency counts the occurance of each word.
type Frequency map[string]int
// WordCount returns the Frequency of words in input.
func WordCount(input string) Frequency {
ret := make(Frequency)
input = strings.ReplaceAll(input, "$", " ")
input = strings.ReplaceAll(input, "^", " ")
input = strings.ToLower(input)
for _, word := range strings.FieldsFunc(input, sepFunc) {
ret[strings.Trim(word, "'")]++
}
return ret
}
func sepFunc(ch rune) bool {
if unicode.IsSpace(ch) || unicode.IsPunct(ch) && ch != '\'' {
return true
}
return false
}