Improved word count

This commit is contained in:
Meutel 2017-07-07 16:13:45 +02:00
parent 675b497a70
commit 8b525e218e

View File

@ -2,19 +2,18 @@ package main
import "fmt" import "fmt"
import "io" import "io"
import "io/ioutil"
import "log" import "log"
import "os" import "os"
import "bufio"
import "strings" import "strings"
func wc(r io.Reader) map[string]uint { func wc(r io.Reader) map[string]uint {
words := make(map[string]uint) words := make(map[string]uint)
if text, err := ioutil.ReadAll(r); err != nil { s := bufio.NewScanner(r)
log.Fatalln(err) s.Split(bufio.ScanWords)
} else { for s.Scan() {
for _, word := range strings.Fields(string(text)) { word := strings.ToLower(s.Text())
words[word]++ words[word]++
}
} }
return words return words
} }