func countWordsInLine(line string) int64 { // Break line into words and count each valid word. words := strings.Fields(line) count := int64(0) for _, word := range words { if _, err := cleaner.Clean(word); err == nil { count++ } } return count }
// readAndCount reads the reader given and counts the words into a map which // is returned. func readAndCount(reader *bufio.Reader) map[string]int64 { m := make(map[string]int64) for { // Read lines. line, err := reader.ReadString('\n') if err != nil { if err != io.EOF { fmt.Fprintf(os.Stderr, "in wordcount main, error reading: %v\n", err) } break } // Break line into words and count them in the map. words := strings.Fields(line) for _, word := range words { if cleanWord, err := cleaner.Clean(word); err == nil { m[cleanWord]++ } } } return m }